我正在寻找一种通过生成 .txt 文件来下载应用程序上显示的文本的方法。这是我的尝试,不幸的是没有成功:
library(shiny)
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),"Line1",
br(),"Line2",
br(),"Line3"),
downloadButton("Download Metadata", label = "Download")
)
)
server <- function(input, output, session){
output$downlaodData <- downloadHandler(
filename = function(){
paste("data-", Sys.Date(), ".txt", sep = "")
},
content = function(file) {
write.txt(data, file)
}
)
感谢您的帮助
最佳答案
您不能编写这样显示在您的页面上的文本。您可以下载存储为数据或用户输入的文本。您的代码中也存在一些问题:
工作示例
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)
关于r - 在 Shiny 中下载文本并生成 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45240660/