【问题标题】:Use R Markdown .docx template in shiny app在闪亮的应用程序中使用 R Markdown .docx 模板
【发布时间】:2021-12-08 12:19:10
【问题描述】:

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

感谢您的帮助!

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    将您的 rmarkdown 和 word 模板保存在 app 文件夹中。

    在您的下载处理程序中:

            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
    

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2018-10-25
      相关资源
      最近更新 更多