如何从R Shiny应用程序下载四开本pdf报告



我正在尝试根据用户提供的输入下载pdf/docx/html报告。为此,我创建了一个Shiny应用程序和一个带有参数的qmd文件。当我单独渲染时,QMD文件工作正常。但是,如果我渲染相同的文档,并尝试从Shiny下载它是不工作。这是我的downloadHandler

output$report <- downloadHandler(
filename = function() {
paste("report", input$report_file_type, sep = ".")
},
content = function(file) {
output <- quarto::quarto_render(
input = 'report_template.qmd',
output_format = input$report_file_type,
execute_params = list(name = input$author_name,
report_title = input$report_title)
)
file.copy(output, file)
}

)编辑:这是qmd报告模板

---
title: "`r params$report_title`"
author: "`r params$name`"
date: "`r format(Sys.time(), '%d %B, %Y')`"
format: html
embed-resources: true
params:
report_title: "report title"
name: "author name"
p_list: list("A", "B")
---
Printing each element of the list...
```{r}
#| echo: true
for(p in eval(parse(text = params$p_list))){
print(p)
}
```
Printing the list...
```{r}
#| echo: true
print(eval(parse(text = params$p_list)))
```

我没有得到任何错误,但同时没有任何东西被写入下载文件夹。似乎渲染的文档没有被分配到输出。也许这就是为什么应对线不起作用的原因。问题可能是什么?如何解决?提前谢谢。

quarto::quarto_render不返回生成的文件或任何东西,因此代码中的outputNULL,因此您没有得到任何输出。

现在要使此代码工作,我们需要将quarto::quarto_render生成的文件复制到传递给downloadHandlercontent参数的函数的file参数中。

shinyApp(

# UI -----------------------------------------------------------
ui = fluidPage(
textInput("author_name", "Author Name", "John Doe"),
textInput("report_title", "Report Title", "Quarto From Shiny"),
radioButtons(
inputId = "report_file_type",
label = "Report Type",
choices = c("HTML" = "html", "PDF" = "pdf")
),
downloadButton("report", "Generate report")
),

# SERVER --------------------------------------------------------
server = function(input, output) {

output$report <- downloadHandler(
filename = function() {
paste("report", input$report_file_type, sep = ".")
},
content = function(file) {
quarto::quarto_render(
input = "report_template.qmd",
output_format = input$report_file_type,
execute_params = list(
name = input$author_name,
report_title = input$report_title
)
)
# copy the quarto generated file to `file` argument.
generated_file_name <- paste("report_template", input$report_file_type, 
sep = ".")
file.copy(generated_file_name, file)
}
)
}
)

最新更新