Home > Mobile >  Use R Markdown .docx template in shiny app
Use R Markdown .docx template in shiny app

Time:10-22

I build a shiny app that can filter data and automatically render .docx reports. As I want to customize my reports I would like to use the reference_docx function in the YAML header. I have the shiny app, the markdown document and the template.docx all in one folder. However, the shiny app does not automatically "use" the template.docx as markdown usually does. I assume that I have to load the template in a tempdir() or something like this, but I couldn't quite figure out how. What do I have to do to load the reference docx into the shiny app?

Thank you for your help!

CodePudding user response:

Keep your rmarkdown and word template in app folder.

In your downloadhandler:

        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())
                )
            }
        )

Then in your yaml:

output: 
  word_document:
    reference_docx: template.docx
  • Related