因此,基本上,我的应用程序有10个不同的表和图表正在为用户生成结果。现在,我要做的是,为用户显示一个清单,然后要求用户选择他们想要包括在他们下载的报告中的项目。这将产生一个定制的报告,其中将只包括用户希望拥有的东西。因此,仅在选中复选框时,请帮助我完成将项添加到报表中的过程。我有这样的想法,我必须用r标记来生成这份报告,但我很想进一步澄清它。
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,并将它的值作为输出。所以,我已经到了一半,除了现在,我只需要得到作为输出的地块,而不是选择评价。
发布于 2021-01-19 12:52:09
你的代码看起来不错。只需将数据作为一个参数传递,并创建情节,就像在您闪亮的应用程序中一样。您可以使用默认的绘图函数,但也可以使用ggplot2或plotly。若要包含/排除绘图/表,还应将复选框值input$Try作为参数传递给报表,并使用条件添加绘图/表,如下所示。
我只发布了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}图书馆(针织品)
tbl <- params$tbl
包含<- params$include
```{r echo=FALSE, results='asis'}如果(%中包含‘HI’%){
kable(tbl[1:5,], caption= 'A table')}
```{r echo=FALSE, result='asis'}如果(%中包含‘嘿’%){
plot(tbl$mpg, tbl$cyl)}
我真正喜欢的是,您不必每次更改报告时都重新启动闪亮的应用程序,因为它独立于应用程序本身。
https://stackoverflow.com/questions/65786335
复制相似问题