Generating downloadable reports (rmarkdown) : reference_docx (yaml header) is not read by knitr

rmarkdown

#1

Hi everyone,

I carry out a Shiny app to generate downloadable reports. User can export the report as a html, pdf or MS Word document as well.
I use another MS Word template that the one provided by default. Hence, I use knitr option "reference_docx" (see Happy collaboration with Rmd to docx http://rmarkdown.rstudio.com/articles_docx.html).

On Rstudio, when I knit the Rmd file, everything works fine: styles are passed from the styles reference docx file to the final docx report but, when I tried to implement this on the shiny app the final report is generated without any style.
It seems that the reference docx is not read.
Yet, reference docx and Rmd file are both in the same directory...

Thank you for your help,

Laurent

For your information, below you can find an overview of the YAML header

---
author: "John Doe"
date: '`r format(Sys.time(), "%B-%Y")`'
output:
  word_document:
    toc: true
    toc_depth: 4
    keep_md: true
    reference_docx: mystyle.docx
  pdf_document:
    toc: true
    toc_depth: 4
    fig_caption: true
    keep_tex: true
  html_document:
    toc: true
    toc_depth: 4
    number_sections: true
params:
  csv.header: NA
  csv.name: NA
  csv.quote: NA
  csv.sep: NA
  csv.skip: NA
  csv.asfactor: NA
  csv.encoding: NA
  csv.colnames: NA
  csv.dec: NA
  csv.datapath: NA
  csv.columnsid: NA
  datasite: NA
  fraction: NA
  subset: NA
  longversion: NA
  withaustria: NA
  width.fig: 6
  height.fig: 4.5
  dpi.fig: 300
title: '`r paste("Grimm demonstration of equivalence", params$datasite, sep = " : ")`'
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
- \usepackage{pdfpages}
- \usepackage[utf8]{inputenc}
- \usepackage[T1]{fontenc}
- \usepackage{longtable}
- \usepackage{float}
---

#2

Hi, Laurent,
I have the same issue...
I just solved it by changing the render function in my server like this :
render('report_name.Rmd',encoding="utf-8",
word_document(reference_docx = "template_name.docx"))

Hoping it will work !
Bye


#3

In case anyone else is still struggling with this issue:

I tried Ben's solution and it still didn't work for me until I included the full file path (e.g. reference_docx = "~/ref_folder/mystyle.docx")


#4

I am having the exact same problem and have tried using word_document() and absolute paths as suggested above, but this has made no difference.

It is odd because it works as expected on rstudio server but not shiny server; even though they are running on the same machine.

I am using absolute paths in with temporary folders, so /tmp/RtmpnWjfwF/oGLv9Kz/template.docx. Could this be causing a problem? Both .Rmd and template.docx are in the same temporary folder.


#5

Hi everyone,

Sorry for the late reply (especially for bpauget :slight_smile: ) and thanks for your help.
A few months ago, I was able to figure out this bizarre behavior. But unfortunately, I could not remember which chunk of code was modified to avoid this issue.
Now, I get it :
In my case, changing the argument output_format from word_document() to "word_document" (char) in the rmarkdown::render() function did the trick.
See printscreen :

  1. rmarkdown::render(..., output_format = "word_document",...) as char : the customized Word template specified in YAML header ("mystyle.docx") is correctly read when compiling the report.

(reply to be continued because since I'm a new user, I can only attach a single image in this reply(weird...)


#6

... continue...

  1. However, when I put the function word_document() in the render function rmarkdown::render(..., output_format = word_document(),...) the template is not read anymore.

Here is the YAML header :

date: '`r format(Sys.time(), "%B-%Y")`'
author: '`r paste("ISSeP", params$author, sep = " - ")`'
output:
  html_document:
    toc: true
    toc_depth: 4
    number_sections: true
  word_document:
    toc: true
    toc_depth: 4
    keep_md: true
    reference_docx: mystyle.docx
  pdf_document:
    toc: true
    toc_depth: 4
    fig_caption: true
    keep_tex: true
    includes:
      in_header: header.tex
fontsize: 11pt
params:
  csv.name: NA
  csv.header: NA
  csv.quote: NA
  csv.sep: NA
  csv.skip: NA
  csv.asfactor: NA
  csv.encoding: NA
  csv.colnames: NA
  csv.dec: NA
  csv.datapath: NA
  csv.columnsid: NA
  datasite: NA
  fraction: NA
  subset: NA
  longversion: NA
  withaustria: NA
  year.equation: NA
  user.calibration: NA
  author: NA
  width.fig: 6
  height.fig: 4.5
  dpi.fig: 300
  
title: '`r paste("Grimm demonstration of equivalence", params$datasite, sep = " : ")`'

Hoping this helps !
Please, let me know :slight_smile:

Regards,

Laurent


#7

A post was split to a new topic: Issues generating downloadable reports from shiny-server - word_document


#8