我无法弄清楚发生了什么 - 一切似乎都有效但我的应用程序没有生成文件 - 尽管看起来确实如此.我在Windows上运行它,在RStudio 0.98.125上,我使用以下行运行它:runApp()下面是一个非常简单的可重现示例:
shinyUI(pageWithSidebar(
headerPanel("My App"),
sidebarPanel(
numericInput('NumRuns','Number of runs',value=3,min=3,max=10,step=1),
actionButton(inputId="goButton","Run!"),
textInput("downloadData","Save My Data Frame:",value="Data Frame 1"),
downloadButton('downloadData','Save my file!')
),
mainPanel(
tabPanel("Some Text",
h4(textOutput("caption2")),
tableOutput("mydf"),
value=3))
))
Run Code Online (Sandbox Code Playgroud)
shinyServer(function(input,output){
# Creating files for download at the end
myout = reactive({
if(input$goButton==0) return(NULL)
nrruns=input$NumRuns
mylist=NULL
for(i in 1:nrruns){
mylist[[i]]<-data.frame(a=rnorm(10),b=runif(10))
names(mylist)[i]<-paste("dataframe",i,sep="")
}
return(mylist)
})
output$mydf <- renderTable({
if(input$goButton==0) return(NULL)
input$goButton
isolate(
myout()$dataframe1
)
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$downloadData, " ",Sys.Date(),".csv",sep="") },
content = function(file) {
write.csv(myout()$dataframe1,file,row.names=F)
}
)
})
Run Code Online (Sandbox Code Playgroud)
Mus*_*iaz 53
请注意,下载按钮在RStudio查看器中不起作用.您的朋友可能正在使用RStudio查看器来查看该应用程序.如果是这种情况,请在外部Web浏览器中打开应用程序("运行应用程序"按钮右侧有一个下拉列表:在窗口中运行,在查看器窗格中运行,运行外部;选择最后一个).
提供的示例适用于在我的测试中下载CSV(如果它来自webbrowser,即使用RStudio中的Run app确实导致同样的问题)
请注意,如果您从下载按钮而不是下载的内容中继续获得类似"download.html"的内容,则必须确保downloadButton("myIdHere",...)中的ID与输出$ myIdHere = downloadHandler("输出"匹配. csv",...)
还要注意,如果你使用闪亮的模块(你可能会知道你是否使用它),那么你想使用downloadButton(ns("myIdHere"),...)然后你仍然有输出$ myIdHere