在闪亮的应用程序中使用 R Markdown .docx 模板

Use R Markdown .docx template in shiny app

我构建了一个闪亮的应用程序,可以过滤数据并自动呈现 .docx 报告。因为我想自定义我的报告,所以我想在 YAML header 中使用 reference_docx 函数。我有闪亮的应用程序、降价文档和 template.docx 都在一个文件夹中。但是,闪亮的应用程序不会像降价通常那样自动“使用” template.docx 。我假设我必须在 tempdir() 或类似的东西中加载模板,但我不太清楚如何。我需要做什么才能将参考 docx 加载到闪亮的应用程序中?

感谢您的帮助!

将您的 rmarkdown 和 word 模板保存在应用程序文件夹中。

在您的下载处理程序中:

        output$your_output <- downloadHandler(
            filename = function() {
                'output_title.docx'
            },
            content = function(file) {
                tempReport <- file.path(temp_folder, 
                       "rmarkdown_template.Rmd")
                tempTemplate <- file.path(temp_folder, "template.docx")
                file.copy("rmarkdown_template.Rmd", tempReport, overwrite = TRUE)
                file.copy("template.docx", tempTemplate, overwrite = TRUE)

                # Params
                
                )
                rmarkdown::render(tempReport, output_file = file, output_format = 'word_document',
                                  params = params,
                                  envir = new.env(parent = globalenv())
                )
            }
        )

然后在你的 yaml 中:

output: 
  word_document:
    reference_docx: template.docx