【问题标题】:Add multiple outputs from shiny to html markdown将多个输出从闪亮添加到 html 降价
【发布时间】:2019-08-29 23:05:43
【问题描述】:

我创建了一个闪亮的应用程序,其中基于用户选择的参数有多个输出(表格、图表和文本)。我想下载 HTML 文档中的输出。我可以使用https://shiny.rstudio.com/articles/generating-reports.html 上的小示例来做到这一点,但我似乎无法弄清楚如何在降价文件中使用多个输出。

我环顾四周,虽然有一些例子,但我似乎仍然无法弄清楚。可能只是我经验不足。我在下面修改(很差)的代码给出了输出test.texttest.text2,然后又是test.text

我希望能够添加随后将在降价中使用的多个输出值。我在反应函数中使用了我的所有输出,因为我注意到我不能在 downloadHandler 中使用 output$

这是我在downloadHandler中尝试使用的代码

 test.text <- reactive({input$gendertext}) #input text written in boxes

 test.text2 <- reactive({input$agetext})

output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)

      params <- list(n = test.text())
      params2 <- list(n = test.text2())

      rmarkdown::render(tempReport, output_file = file,
                        params = c(params,params2),
                        envir = new.env(parent = globalenv()))
    }
  )

由于我有多个输出(ggplots、表格和文本),我希望能够在 markdonw 中使用test.texttest.text2plot1...plotn 等。分开。

例如

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---
```{r}
test.text
plot1
``

```{r}
test.text2
plot2
``

如果有更简单的方法可以从闪亮的 UI 下载 Html/pdf 文件,那就太棒了!

谢谢!

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    我想通了!感谢@Ruben Kazumov 的帮助。

    为了能够将绘图添加到降价,或您可能在 output$ 中拥有的任何其他内容,您首先需要将绘图包装在反应函数中。

    例如

    plot &lt;- reactive({your plot(can be ggplot2 or whatever)}

    你可以在你的应用程序中使用

    output$plottorender &lt;- renderPlot({plot()})

    最后,您可以使用您刚刚在降价中创建的plot()

     output$report <- downloadHandler(
        filename = "report.html",
        content = function(file) {
          tempReport <- file.path(tempdir(), "report.Rmd")
          file.copy("report.Rmd", tempReport, overwrite = TRUE)`
    
          # one list() container object for all the parameters
          # all the objects have unique names (keys)
          `params <- list(text1 = input$test.text1,
                         text2 = input$test.text2,
                         plot1 = plot(),
                         plot2 = plot())) # ... whatever you have to send to the report
    
          rmarkdown::render(tempReport, output_file = file,
                            params = params,
                            envir = new.env(parent = globalenv()))
        })
    

    【讨论】:

      【解决方案2】:
      output$report <- downloadHandler(
          filename = "report.html",
          content = function(file) {
            tempReport <- file.path(tempdir(), "report.Rmd")
            file.copy("report.Rmd", tempReport, overwrite = TRUE)
      
            # one list() container object for all the parameters
            # all the objects have unique names (keys)
            params <- list(text1 = input$test.text1,
                           text2 = input$test.text2,
                           plot1 = output$plot1,
                           plot2 = output$plot2)) # ... whatever you have to send to the report
      
            rmarkdown::render(tempReport, output_file = file,
                              params = params,
                              envir = new.env(parent = globalenv()))
          }
        )
      

      然后在报告中:

      ```{r}
      params$text1 # inside the report, you should call the object by its key
      params$plot1
      ``
      
      ```{r}
      params$text2
      params$plot2
      ``
      

      【讨论】:

      • 我刚刚尝试将output$plot1 合并到downloadhandler 中,但我仍然得到Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed. [No stack trace available]
      猜你喜欢
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2018-03-12
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多