我正在寻找一种通过生成.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)
}
)
感谢您的帮助
答案 0 :(得分:2)
您无法像这样编写页面上显示的文字。您可以下载存储为数据或用户输入的文本。您的代码中也存在一些问题:
工作示例
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)