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

发布于 2025-01-26 09:13:48 字数 2342 浏览 5 评论 0原文

这个简单的闪亮应用程序具有各种可选选项(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
---

This simple Shiny app has a dropdown of various selectable options (selectInput), each of the options in selectInput have a .Rmd file matching its name. Based on the option selected through the dropdown I'm trying to call its respective .Rmd file, complete code below.

The eventReactive() method seems to get the selected value and is passed as an input to call the respective .Rmd file, however the code executes but does not generate the report.

Alternatively, commenting out eventReactive() and calling out the required .Rmd file as a variable (report_name) generates a downloadable report with the required content. How do I go about fixing this to get the desired functionality?

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
---

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自演自醉 2025-02-02 09:13:48

要纠正的几件事:

  • report_name&lt; - ...应在下载handler之外。
  • 如果输入$ select_id“” 选择,则可能应该不是 允许下载。为此,我们req uire是其他的。
  • 渲染但不是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)
    }
  )
}

  • 次要)您:: -Reference 在尝试渲染报告名称之前的存在,以抢占与路径相关的问题,如果用户可能看不到相关的错误消息。 (是否不是,取决于您正确处理路径的自信心。)

  • 您的模板包括参数:,但您没有在Render(。)。我假设此处省略了它,但请确保您将params = ...添加到渲染调用中。


  • 您的输入$ select_id优惠<代码>“ html” 和“ word”,但是您的switch> switch语句正在寻找PDFhtml。当选择HTML时,上述有效,但是您需要更新selectInputswitch> switch语句以确保正确引用所有候选人。

A few things to correct:

  • report_name <- ... should be outside of the downloadHandler.
  • You should likely not allow a download if input$select_id is the " " choice. For this, we require it to be something else.
  • (Minor) You ::-reference render but not pdf_document, etc.
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 notes:

  • You might want to test for presence of the report name before trying to render it, to preempt path-related problems where the user may not see the relevant error message. (Or not, depending on how confident you are that paths are being handled correctly.)

  • Your templates include params: but you are not passing anything in render(.). I'm assuming it was omitted here for brevity, but make sure you add params=... into the render call.

  • Your input$select_id offers "HTML" and "Word", yet your switch statements look for PDF and HTML. The above works when html is selected, but you need to update your selectInput and switch statements to ensure all candidates are referenced correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文