我有一个简单的闪亮应用程序,它下载一个 .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)
希望这可以解决您的问题。我刚刚包含了一个文本输入,默认值设置为您的文件名,如上所述,然后将下载函数中的文件名设置为该文本输入。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句