R 如何在一个闪亮的应用程序中下载同一文件中的两个不同数据帧
我有一个基本的闪亮应用程序,我想在其中下载一个文件,其中包含两个不同的数据帧iris,mtcars。这可能吗?我不在乎哪一个会先显示还是第二个。在下面的vesrion中,文件被破坏了,但我这样添加它是为了更清楚地说明我想要什么R 如何在一个闪亮的应用程序中下载同一文件中的两个不同数据帧,r,shiny,R,Shiny,我有一个基本的闪亮应用程序,我想在其中下载一个文件,其中包含两个不同的数据帧iris,mtcars。这可能吗?我不在乎哪一个会先显示还是第二个。在下面的vesrion中,文件被破坏了,但我这样添加它是为了更清楚地说明我想要什么 ### ui.R library(shiny) pageWithSidebar( headerPanel('Iris k-means clustering'), sidebarPanel( uiOutput("ex") , uiOutput("down"
### ui.R
library(shiny)
pageWithSidebar(
headerPanel('Iris k-means clustering'),
sidebarPanel(
uiOutput("ex") ,
uiOutput("down")
),
mainPanel(
uiOutput('plot')
)
)
#server.r
function(input, output, session) {
output$ex<-renderUI({
radioButtons("extension","File Format", choices = c("txt","csv","tsv","json"))
})
output$down<-renderUI({
#Download files with quotes or not depending on the quote=input$quotes which has value TRUE or FALSE.
output$downloadData <- downloadHandler(
filename = function() {
paste("file", input$extension, sep = ".")
},
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
sep <- switch(input$extension,"txt"=",", "csv" = ",", "tsv" = "\t","json"=",")
# Write to a file specified by the 'file' argument
write.table(data.frame(iris,mtcars), file, sep = sep,
row.names = FALSE)
}
)
downloadButton("downloadData", "Download")
})
}
正如@r2evans正确指出的那样,您只需在第一次调用后将append=TRUE添加到all write.table,以在一个文件中下载多个数据帧 服务器.r
由于帧不太可能是可rbind的不兼容维度,因此需要编写.tableiris,。。。然后编写.tablemtcars,append=TRUE,…。您的代码似乎无法工作。我只是得到了应用程序标题,没有任何功能。如果你删除mtcars并在浏览器中打开它,它就可以正常工作。它符合r2evans的建议。谢谢
server <- function(input, output, session) {
output$ex<-renderUI({
radioButtons("extension","File Format", choices = c("txt","csv","tsv","json"))
})
output$down<-renderUI({
output$downloadData <- downloadHandler(
#FileName
filename = function() {
paste("file", input$extension, sep = ".")
},
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
sep <- switch(input$extension,"txt"=",", "csv" = ",", "tsv" = "\t","json"=",")
write.table(iris, file, sep = sep, row.names = FALSE)
write.table(mtcars, file, sep = sep, row.names = FALSE, append = TRUE)
}
)
downloadButton("downloadData", "Download")
})
}