如何在 r shiny 中制作自定义的可下载报告?
How can you make customized downloadable reports in r shiny?
基本上,我的应用程序为用户生成了 10 个不同的表格和图形作为结果。现在,我想要做的是,为用户显示一个清单,然后要求用户 select 他们想要包含在他们下载的报告中的项目。这将生成一个自定义报告,其中仅包含用户希望拥有的内容。因此,请帮助我完成仅在选中复选框时才将项目添加到报告中的过程。我有点想我将不得不使用 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,并将其值作为输出。所以,除了现在,我已经完成了一半,我只需要将绘图作为输出而不是选择值。
你的代码看起来不错。只需将数据作为参数传递过来,然后像在 Shiny App 中一样创建绘图。您可以使用默认的 plot 函数,也可以使用 ggplot2
或 plotly
。要 include/exclude plots/tables,还要将复选框值 input$Try
作为参数传递给报告,并使用条件添加 plots/tables,如下所示。
我只 post Rmd 和我在你的闪亮应用程序中更改的行。其他一切看起来都不错。
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,因为它独立于应用程序本身。
基本上,我的应用程序为用户生成了 10 个不同的表格和图形作为结果。现在,我想要做的是,为用户显示一个清单,然后要求用户 select 他们想要包含在他们下载的报告中的项目。这将生成一个自定义报告,其中仅包含用户希望拥有的内容。因此,请帮助我完成仅在选中复选框时才将项目添加到报告中的过程。我有点想我将不得不使用 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,并将其值作为输出。所以,除了现在,我已经完成了一半,我只需要将绘图作为输出而不是选择值。
你的代码看起来不错。只需将数据作为参数传递过来,然后像在 Shiny App 中一样创建绘图。您可以使用默认的 plot 函数,也可以使用 ggplot2
或 plotly
。要 include/exclude plots/tables,还要将复选框值 input$Try
作为参数传递给报告,并使用条件添加 plots/tables,如下所示。
我只 post Rmd 和我在你的闪亮应用程序中更改的行。其他一切看起来都不错。
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,因为它独立于应用程序本身。