r - 在 Shiny 中下载文本并生成 txt 文件

标签 r shiny shiny-server shinydashboard

我正在寻找一种通过生成 .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)
      }
    )

感谢您的帮助

最佳答案

您不能编写这样显示在您的页面上的文本。您可以下载存储为数据或用户输入的文本。您的代码中也存在一些问题:

  • 数据未定义,因此您没有指定应下载的内容
  • write.txt 不是函数,使用 write.table 代替
  • downloadbutton 和 downloadHandler 应该具有相同的 id。


  • 工作示例
    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/

    相关文章:

    r - R 中的指数回归

    r - 在同一图中交错和堆叠 geom_bar?

    r - 如何将十进制日期格式(例如2011.580)转换为普通日期格式?

    r - Shiny 的动态仪表标题

    r - 上传,转换xlsx文件和下载结果 Shiny

    r - 更改 R 中向量内的唯一名称之一

    R Shiny in Memory Application 或 noSQL

    javascript - 从 server.R 中的 .js 接收数据

    r - 通过字符串 : R Shiny 调用 UI 输入

    r - Shiny:observeEvent和eventReactive有什么区别?