该应用程序的其余部分正在运行,但无法下载图表。我想在我的应用程序中包含用于图-png和pdf文件的下载按钮。我有以下代码,当我按下下载按钮时,我没有选择以所需格式保存的选项。谁能看到错误在哪里:
带有下载按钮的ui.R部分:
library(shiny)
shinyUI(fluidPage(
titlePanel(title = h4("Business Intelligence & Analytics", align="center")),
sidebarLayout(
sidebarPanel(("Make Selections Here"),
textInput("name","Enter your name",""),
textInput("Age","Enter Age",""),
radioButtons("Gender","Enter Gender",list("Male","Female")),
sliderInput("Slider","Select The Value", min = 0, max = 100, value = 4, animate = T,
step = 5),
selectInput("State","Name Of The States",c("California","Arizona",
"Chicago","Rosemont","Dallas")
,selected = "Dallas",selectize = TRUE, multiple = TRUE ),
selectizeInput("Variable","Select the Var",
c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Width"=3)),
sliderInput("Bins","Select The Number Of Bins",min = 5, max=20,value = 2),
radioButtons("Colour","Select The Colour",list("Green","Yellow","Red","Black"),
selected = "Yellow"),
radioButtons("Download Option", "Select the Option", list("png","jpeg","pdf"))),
mainPanel(
tabsetPanel(type="tab", #adding tab sheets
tabPanel("Summary",verbatimTextOutput("Cat9")),
tabPanel("Structure", verbatimTextOutput("Cat8")), #verbatim TextOutput used to show output of render print
tabPanel("Data",tableOutput("Cat7")),
tabPanel("Plot", plotOutput("Cat6"))
),
textOutput("Cat1"),
textOutput("Cat2"),
textOutput("Cat3"),
textOutput("Cat4"),
textOutput("Cat5"),
plotOutput("hist"),
downloadButton(outputId = "Cat10", label = "Download The Plot")
)
)
)
)
Server.R部分的代码:
shinyServer( function(input, output){ output$Cat1 <-
renderText(input$name) output$Cat2 <- renderText(input$Age)
output$Cat3<- renderText(input$Gender)
output$Cat4 <- renderText(paste("You Selected The Value:", input$Slider))
output$Cat5 <- renderText(input$State)
output$Cat6<- renderPlot({
colm <- as.numeric(input$Variable)
hist(iris[,colm], breaks =seq(0,max(iris[,colm]),l=input$Bins+1),
col = input$Colour, main = "Histogram Of Iris",xlab =
names(iris[colm]))
output$Cat7 = renderTable({
head(iris,4)
})
output$Cat8 = renderPrint({
str(iris3)
}) output$Cat9 <- renderPrint({summary(iris3)
})
output$Cat10<- downloadHandler(
#Specify The File Name
filename = function() {
paste("iris",input$`Download Option`,sep= ".")
#here we are downloading in format we have mentoned in download option in
ui.r #this could be pdf png etc and paste iris means the output will create
a file which will have the name of the dataset in our case iris followed by
the format we want it to be in and their naming convention is seperated by a
.},
content = function(file){
# open the format of file which needs to be downloaded ex: pdf, png etc.
if (input$`Download Option`== "png")
png(file)
#else if (input$`Download Option`== "pdf")
#pdf(file)
else jpeg(file)
hist(iris[,colm], breaks =seq(0,max(iris[,colm]),l=input$Bins+1),
col = input$Colour, main = "Histogram Of Iris",xlab =
names(iris[colm]))
dev.off()
}
)
}
答案 0 :(得分:0)
您在渲染函数中分配了变量“ colm”,并尝试在downloadHandler中对其进行访问。这对我有用:
library(shiny)
ui <- fluidPage(
titlePanel(title = h4("Business Intelligence & Analytics", align="center")),
sidebarLayout(
sidebarPanel(("Make Selections Here"),
textInput("name","Enter your name",""),
textInput("Age","Enter Age",""),
radioButtons("Gender","Enter Gender",list("Male","Female")),
sliderInput("Slider","Select The Value", min = 0, max = 100, value = 4, animate = T,
step = 5),
selectInput("State","Name Of The States",c("California","Arizona",
"Chicago","Rosemont","Dallas")
,selected = "Dallas",selectize = TRUE, multiple = TRUE ),
selectizeInput("Variable","Select the Var",
c("Sepal.Length"=1,"Sepal.Width"=2,"Petal.Width"=3)),
sliderInput("Bins","Select The Number Of Bins",min = 5, max=20,value = 2),
radioButtons("Colour","Select The Colour",list("Green","Yellow","Red","Black"),
selected = "Yellow"),
radioButtons("Download Option", "Select the Option", list("png","jpeg","pdf"))),
mainPanel(
tabsetPanel(type="tab", #adding tab sheets
tabPanel("Summary",verbatimTextOutput("Cat9")),
tabPanel("Structure", verbatimTextOutput("Cat8")), #verbatim TextOutput used to show output of render print
tabPanel("Data",tableOutput("Cat7")),
tabPanel("Plot", plotOutput("Cat6"))
),
textOutput("Cat1"),
textOutput("Cat2"),
textOutput("Cat3"),
textOutput("Cat4"),
textOutput("Cat5"),
plotOutput("hist"),
downloadButton(outputId = "Cat10", label = "Download The Plot")
)
)
)
server <- function(input, output) {
colm <- reactive({as.numeric(input$Variable)})
output$Cat1 <- renderText(input$name)
output$Cat2 <- renderText(input$Age)
output$Cat3<- renderText(input$Gender)
output$Cat4 <- renderText(paste("You Selected The Value:", input$Slider))
output$Cat5 <- renderText(input$State)
output$Cat6<- renderPlot({
hist(iris[,colm()], breaks =seq(0,max(iris[,colm()]),l=input$Bins+1),
col = input$Colour, main = "Histogram Of Iris",xlab =
names(iris[colm()]))
})
output$Cat7 = renderTable({
head(iris,4)
})
output$Cat8 = renderPrint({
str(iris3)
})
output$Cat9 <- renderPrint({summary(iris3)
})
output$Cat10<- downloadHandler(
#Specify The File Name
filename = function() {
paste("iris",input$`Download Option`,sep= ".")},
content = function(file){
# open the format of file which needs to be downloaded ex: pdf, png etc.
if (input$`Download Option`== "png"){
png(file)
} else if (input$`Download Option`== "pdf"){
pdf(file)
} else {
jpeg(file)
}
hist(iris[,colm()], breaks =seq(0,max(iris[,colm()]),l=input$Bins+1),
col = input$Colour, main = "Histogram Of Iris",xlab =
names(iris[colm()]))
dev.off()
}
)
}
shinyApp(ui = ui, server = server)