【问题标题】:Generate report with Shiny app and Rmarkdown使用 Shiny 应用程序和 Rmarkdown 生成报告
【发布时间】:2016-05-20 13:02:35
【问题描述】:

我想创建一个闪亮的应用程序,让您可以下载报告。现在我试图让事情变得超级简单,闪亮的应用程序上唯一的输入是用户可以使用textarea输入的一些文本:

library(shiny)
server <- function(input, output) {
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },
    content = function(file) {
      src <- normalizePath('report.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)

      library(rmarkdown)
      out <- render('report.Rmd', switch(
        input$format,
        PDF = pdf_document(), HTML = html_document(), Word = word_document()
      ))
      file.rename(out, file)
    }
  )
}

ui <- fluidPage(
  tags$textarea(id="text", rows=10, cols=80, "Default value"),

  flowLayout(radioButtons('format', 'Document format', c('HTML', 'Word'),
                          inline = TRUE),
             downloadButton('downloadReport'))

)

shinyApp(ui = ui, server = server)

我的report.Rmd 文件只有两行:

# Title

`r renderPrint({ input$text })`

唉,报告没有打印input$text

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:
    library(shiny)
    server <- function(input, output) {
      output$downloadReport <- downloadHandler(
        filename = function() {
          paste('my-report', sep = '.', switch(
            input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
          ))
        },
        content = function(file) {
          src <- normalizePath('report.Rmd')
    
          # temporarily switch to the temp dir, in case you do not have write
          # permission to the current working directory
          owd <- setwd(tempdir())
          on.exit(setwd(owd))
          file.copy(src, 'report.Rmd', overwrite = TRUE)
    
          out <- rmarkdown::render('report.Rmd',
                                   params = list(text = input$text),
                                   switch(input$format,
                                          PDF = pdf_document(), 
                                          HTML = html_document(), 
                                          Word = word_document()
                                   ))
          file.rename(out, file)
        }
      )
    }
    
    ui <- fluidPage(
      tags$textarea(id="text", rows=20, cols=155, 
                    placeholder="Some placeholder text"),
    
      flowLayout(radioButtons('format', 'Document format', c('HTML', 'Word'),
                              inline = TRUE),
                 downloadButton('downloadReport'))
    
    )
    
    shinyApp(ui = ui, server = server)
    

    report.Rmd

    ---
    title: "Parameterized Report for Shiny"
    output: html_document
    params:
      text: 'NULL'
    ---
    
    # Some title
    
    `r params[["text"]]`
    

    【讨论】:

    • ...换句话说,对于那些稍后阅读本文的人来说——您需要在 Rmd 的 YAML 标头中添加一个“参数”部分,您将渲染到该部分。见@Benjamin 的answer notes at the end
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多