【问题标题】:How can you make customized downloadable reports in r shiny?如何在 r shiny 中制作自定义的可下载报告?
【发布时间】:2021-04-23 10:45:16
【问题描述】:

因此,基本上,我的应用程序为用户生成了 10 个不同的表格和图表作为结果。现在,我想要做的是,为用户显示一个清单,然后要求用户选择他们想要包含在他们下载的报告中的项目。这将制作一个自定义报告,其中仅包含用户希望拥有的东西。因此,请仅在选中复选框的情况下帮助我完成将项目添加到报告中的过程。我有一种想法,即我必须使用 r-markdown 生成报告,但希望进一步澄清它。

app.R

library(shiny)
library(rmarkdown)
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('test.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, 'test.Rmd', overwrite = TRUE)
      
      out <- rmarkdown::render('test.Rmd',
                               params = list(text = input$text,outp=input$Try),
                               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),
             checkboxGroupInput("Try","Let's hope this works",choiceNames = list("include hi","include hey","include hello","include how are you"),choiceValues = list("HI","HEY","HELLO","HOW ARE YOU")),
             downloadButton('downloadReport'))
  
)

shinyApp(ui = ui, server = server)

test.Rmd

---
title: "Parameterized Report for Shiny"
output: html_document
params:
  text: 'NULL'
  outp: 'NULL'
---

# Some title

`r params[["text"]]`
`r params[["outp"]]`

在此,我添加了 ID 为“Try”的 checkboxGroupInput,并将其值作为输出。所以,除了现在,我已经完成了一半,我只需要将绘图作为输出而不是选择值。

【问题讨论】:

  • 你试过什么?请分享一些代码。
  • 我现在添加了一个示例代码sn-p。我希望这会有所帮助。

标签: r shiny r-markdown report shinydashboard


【解决方案1】:

您的代码看起来不错。只需将数据作为参数传递并像在 Shiny App 中一样创建绘图。您可以使用默认绘图功能,也可以使用ggplot2plotly。要包含/排除图表/表格,还将复选框值input$Try 作为参数传递给报告并使用条件添加图表/表格,如下所示。

我只发布 Rmd 和我在您的 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('test.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, 'test.Rmd', overwrite = TRUE)
            
            include <- input$Try
            out <- rmarkdown::render('test.Rmd',
                                     params = list(tbl = mtcars, include = include),
                                     switch(input$format,
                                            PDF = pdf_document(), 
                                            HTML = html_document(), 
                                            Word = word_document()
                                     ))
            file.rename(out, file)
        }
    )
}

更新了 r 降价:

---
title: "Parameterized Report for Shiny"
output: html_document
params:
  tbl: NA
  include: NA
---

```{r echo=FALSE}
library(knitr)
tbl <- params$tbl
include <- params$include
```

```{r echo=FALSE, results='asis'}
if ('HI' %in% include) {
    kable(tbl[1:5,], caption= 'A table')
}
```

```{r echo=FALSE, result='asis'}
if ('HEY' %in% include) {
    plot(tbl$mpg, tbl$cyl)
}
```

我真正喜欢的是,您不必每次更改报告时都重新启动 Shiny App,因为它独立于应用程序本身。

【讨论】:

  • 感谢 Tom 的回答,但我想知道如何使这个条件成为条件。例如,如果用户希望报告中只有一个表格,那么他们只需选中该复选框,报告将包含所需的输出。它基本上是一个有条件的报告编织,我希望只有当复选框值为真时,报告的该部分才应包含在内。
  • 感谢您提供详细信息。我相应地更新了代码。
  • 只是这个问题的扩展。你认为有可能在闪亮的标签上以统一的方式向用户展示所有可供下载的东西,然后他/她可以选择是否应该下载
  • 很多事情都是可能的:-)。看看 shinydashboard 包。我喜欢使用 box 函数来组织事物。
  • 是的,我正在使用闪亮的仪表板包。我的问题是我们是否可以一起显示和下载选定的项目。例如,我们在屏幕上显示两个表格,一旦用户选中“包含表格 1”选项,第一个表格就会包含在报告中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 2017-03-11
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
相关资源
最近更新 更多