我有一个简单的闪亮应用程序,可以下载.txt文件。我的问题是,我希望能够从应用程序中设置文件名并将其下载为filename.txt例如,而不是现在的“ download_button”。
library(shiny)
text=c("Line1", "Line2","Line3")
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),text[1],
br(),text[2],
br(),text[3]),
downloadButton("download_button", label = "Download")
)
)
server <- function(input, output, session){
output$download_button <- downloadHandler(
filename = function(){
paste("data-", Sys.Date(), ".txt", sep = "")
},
content = function(file) {
writeLines(paste(text, collapse = ", "), file)
# write.table(paste(text,collapse=", "), file,col.names=FALSE)
}
)
}
shinyApp(ui,server)
答案 0 :(得分:1)
希望这可以解决您的问题。我只是包括了一个文本输入,默认值如上所述设置为您的文件名,然后在下载功能中将该文件名设置为该文本输入。
text=c("Line1", "Line2","Line3")
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),text[1],
br(),text[2],
br(),text[3]),
textInput("filename", "Input a name for the file", value = paste0("data-", Sys.Date(),".txt")),
downloadButton("download_button", label = "Download")
)
)
server <- function(input, output, session){
output$download_button <- downloadHandler(
filename = function(){
input$filename
},
content = function(file) {
writeLines(paste(text, collapse = ", "), file)
}
)
}
shinyApp(ui,server)