闪亮的报告:试图将输入值作为参数传递给编织 .Rmd 文档但失败

Shiny report : Trying to pass an input value as a parameter to an knit an .Rmd document but failing

这个简单的 Shiny 应用程序有一个包含各种可选选项 (selectInput) 的下拉列表,selectInput 中的每个选项都有一个与其名称匹配的 .Rmd 文件。根据通过下拉菜单选择的选项,我正在尝试调用其各自的 .Rmd 文件,下面是完整的代码。

eventReactive() 方法似乎获取了选定的值并作为输入传递以调用相应的 .Rmd 文件,但是代码执行但不生成报告。

或者,注释掉 eventReactive() 并调用所需的 .Rmd 文件作为变量 (report_name) 生成包含所需内容的可下载报告。我该如何解决这个问题以获得所需的功能?

app.R

library(shiny)

ui <- fluidPage(
  selectInput("select_id",
              "Select an option",
              list(
                " "=" ",
                "apples"='apples',
                "oranges"='oranges')
  ),
  radioButtons('format', 'Document format', c('HTML', 'Word'),
               inline = TRUE),
  
  downloadButton('downloadReport')
)

server <- function(input, output, session) {
  
  # download of report selectable format based on button press
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html'
      ))
    },
    
    # get selected option from dropdown and use the input value as the name to call the Rmd file
    report_name <- eventReactive(input$select_id, {
      paste(input$select_id,'Rmd', sep =".")
    }),

    # replacing report_name() with report_name [below line] generates report, but is not the desired outcome
    # report_name <- "apples.Rmd",
    
    content = function(file) { 
      cat('The selected fruit report is - - - :',report_name())
      
      src <- normalizePath(report_name())
      # 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_name(), overwrite = TRUE)
      
      out <- rmarkdown::render(report_name(),
                               switch(input$format,
                                      PDF = pdf_document(),
                                      HTML = html_document()
                               ))
      file.rename(out, file)
    } 
  ) 
}

shinyApp(ui, server)

apples.Rmd

---
title: "This is a report for Apples"
output: html_document
params:
  tbl: NA
  include: NA
---

oranges.Rmd

---
title: "This is a report for Oranges"
output: html_document
params:
  tbl: NA
  include: NA
---

需要更正的几点:

  • report_name <- ... 应该在 downloadHandler.
  • 之外
  • 如果 input$select_id" " 选项,您应该 允许下载。为此,我们req希望它成为其他东西。
  • (未成年人)您 ::-参考 render 但不是 pdf_document,等等
server <- function(input, output, session) {
  # get selected option from dropdown and use the input value as the name to call the Rmd file
  report_name <- eventReactive(input$select_id, {                       # MOVED
    req(input$select_id != " ")                                         # ADDED
    paste(input$select_id,'Rmd', sep =".")
  })

  # download of report selectable format based on button press
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html'
      ))
    },
    content = function(file) {
      req(nzchar(report_name()))                                        # ADDED
      cat('The selected fruit report is - - - :',report_name())

      src <- normalizePath(report_name())
      # 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_name(), overwrite = TRUE)

      out <- rmarkdown::render(report_name(),
                               switch(input$format,
                                      PDF = rmarkdown::pdf_document(),  # CHANGED
                                      HTML = rmarkdown::html_document() # CHANGED
                               ))
      file.rename(out, file)
    }
  )
}

Follow-on 备注:

  • 您可能希望在尝试呈现报告名称之前测试报告名称是否存在,以抢占 path-related 用户可能看不到相关错误消息的问题。 (或不,取决于您对正确处理路径的信心。)

  • 您的模板包含 params: 但您没有在 render(.) 中传递任何内容。为了简洁起见,我假设这里省略了它,但请确保将 params=... 添加到渲染调用中。

  • 您的 input$select_id 提供 "HTML""Word",但您的 switch 语句寻找 PDFHTML . The above works when html is selected, but you need to update your selectInput and switch statements to ensure all candidates are referenced correctly.