【发布时间】:2018-10-18 09:22:51
【问题描述】:
应用程序的其余部分正在运行,但无法下载绘图。我想在我的应用程序中包含一个用于绘图的下载按钮 - 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()
}
)
}
【问题讨论】:
-
你在尝试 Shiny 浏览器中的下载按钮吗?仅当您在浏览器中打开应用程序时,Shiny 的下载按钮才有效。
-
这是小姐,我在浏览器中尝试了相同的代码,效果很好。