【发布时间】: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