3
votes

Unable to use a modified Word template from Rstudio using Knit Word from Rstudio. Rstudio and required packages were installed and updated this week. Running OS X 10.10.5 and using Word 2011. Simplified testing to Yihui Xie's 113-externalization.rmd and 113-foo.R and have followed his Vimeo video https://vimeo.com/110804387 on this subject. In all cases, knitting the RMD file uses the default formats and not the modified template stored as template.docx. I have tried putting copies in all locations in the project directory without success.

#113-externalization.Rmd
---
title: "Untitled"
output:
     word_document: 
     reference_doc: "template.docx"
---

# Code Externalization

```{r cache=FALSE}
knitr::read_chunk('113-foo.R')
```

The following two chunks are from the external R script `113-foo.R`:

```{r test-a}
```
```{r test-b}
```
#code for 113-foo.R
# ---- test-a ----
1 + 1
x = rnorm(10)

# ---- test-b ----
mean(x)
sd(x)

Search found Changing word template for knitr in Rmarkdown, but that failed to locate the modified template at any location in the project directory.

2
In the video he uses reference_docx:. Does that make the difference?Martin Schmelzer
Yes and No. I changed to reference_docx: and received a failure message. Entered the text asuser3838963
Well for me it does work using reference_docx: word-template.docx. Make sure your indentation in the YAML header is correct (check the video again).Martin Schmelzer
I just tried it: doesnt matter if you use reference_doc or reference_docx. I guess it is the indentation then...Martin Schmelzer

2 Answers

2
votes

Pretty sure the indentation of the YAML header is the problem. Change it to:

---
title: "Untitled"
output:
  word_document: 
    reference_docx: "template.docx"
---

I also just found out for myself that it does not make a difference if you use reference_doc or reference_docx.

0
votes

Tried changing the reference thanks to Martin Schmelzer's comment and accidentally hit upon the solution.
Working YAML HEADER

---
title: "Untitled"
output:
     word_document:
      reference_doc: "template.docx"
---

FAILING YAML HEADERS.

---
title: "Untitled"
output:
     word_document:
      reference_doc: "template.docx"
---

Putting everything in a single line also fails. I haven't found the documentation yet but it appears that a CR and or an indent is needed between the various entries in the output line.