我有一个基本的闪亮应用程序,我想在其中下载包含两个不同数据帧(iris,mtcars)的文件。这可能吗?我不在乎是先显示哪个还是后一个。在下面的文件中,文件已损坏,但我这样添加它是为了更清楚我想要的内容。
### 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")
})
}
答案 0 :(得分:1)
正如@ r2evans正确指出的那样,您可以在第一次调用之后将append = TRUE
添加到所有write.table
中,以在一个文件中下载多个数据帧。
server.r
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")
})
}