Skip to content
New course: Interactive Dashboards with Shiny. Get 50% off with coupon SHINYLAUNCH.
R for the Rest of Us Logo

Making Your Reports Shine: Word Edition

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

Use a reference document to change the look and feel of your report when knitted to Word

Learn More

Daniel Hadley talks about using reference documents to brand RMarkdown reports in this video from rstudio::conf 2018.

Richard Layton also has an article on reference documents on the RStudio website.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Abby Isaacson

Abby Isaacson

June 10, 2021

FYI, I got an error when trying the word default, saying: Error: Functions that produce HTML output found in document targeting docx output. Please change the output type of this document to HTML. Alternatively, you can allow HTML output in non-HTML formats by adding this option to the YAML front-matter of your rmarkdown file:

always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

So I had to add the recommended line before the output (after did not work): title: "Who's Getting Vaccinated for Measles in U.S. Schools?" author: "Abby Isaacson" date: "3/22/2021" always_allow_html: true output: word_document: default html_document: toc: TRUE number_sections: FALSE

This yielded the default Word doc, however my second question (which has been true for your paged report templates as well): The leaflet map I created in the last section has disappeared. When I knit to PDF, the map is still there just not interactive. With paged report or Word, it's just gone. Any tips?

David Keyes

David Keyes

June 11, 2021

This isn't something I've ever done but it does seem like it just can't render the leaflet map at all when you knit to Word. You could set up your RMarkdown doc so that it only executes a code chunk if it's knitting to HTML, say, but honestly most of the time I just choose an output format that makes most sense for a report and then render it to that.

JULIO VERA DE LEON

JULIO VERA DE LEON

May 10, 2022

Hi! Is it possible to apply themes to a table and make it appear with that theme in Word?

David Keyes

David Keyes

May 10, 2022

As of now, you can only use flextable when you're knitting to Word. flextable has a few themes. See https://ardata-fr.github.io/flextable-book/format.html#available-themes

However, soon the gt package will be able to work with Word. When this happens, you'll be able to use the gtextras package, which has a bunch of nice themes. See https://jthomasmock.github.io/gtExtras/index.html

JULIO VERA DE LEON

JULIO VERA DE LEON

May 10, 2022

Thanks David!

Chris Matthis

Chris Matthis

May 26, 2022

I think the solution video and the main video are the same 53 second clip. Should the main one be different and introduce the template building? Thanks!

David Keyes

David Keyes

May 27, 2022

Sorry about that! Fixed now.

Sara Cifuentes

Sara Cifuentes

June 9, 2022

Hello, I have created a template document. However, when I knit it, I do not receive any error, and the word document maintains the default appearance without changes.

output: word_document: reference_docx: template.docx html_document: toc: TRUE toc_depth: 2 toc_float: TRUE

Charlie Hadley

Charlie Hadley

June 9, 2022

Hi Sara!

I think this is another example where indentation is important. It's also worthwhile checking if the template.docx file is in the exact same folder as the .Rmd file itself?

<script src="https://gist.github.com/charliejhadley/6ec34e4bba40c7456dbe9b6085ac7bb0.js"></script>

Let me know if this doesn't solve your issue.

Cheers, Charlie

Kirstin O'Dell

Kirstin O'Dell

December 13, 2022

I'm having the same issue. Here is my code. The .Rmd file and Template folder are in the same folder.

title: "Enrollment Report" date: "12/12/2022" params: race_ethnicity_category: "Hispanic/Latino" output: word_document: reference_docx: Template/ko-template-doc.docx html_document: toc: TRUE toc_depth: 1 toc_float: TRUE fig_height: 10 fig_width: 8 fig_caption: TRUE

Can you copy your YAML to a gist and post the link? I suspect it may be an indentation issue, but it's hard to tell because the R for the Rest of Us website messes up formatting, sadly.

Kirstin O'Dell

Kirstin O'Dell

December 13, 2022

https://gist.github.com/odellk/b434a69587ddebf3df0cf87d9ca80701

Hmm, it actually looks ok to me. Is your Rmd file in a sub-folder by chance?

Andrew Paquin

Andrew Paquin

May 30, 2023

Hi David, I ran into a bit of trouble. Parts of my chosen template document have come over to my report, but not all. I've described what I see in the video linked below. Any ideas? https://www.loom.com/share/017e9dbe8597432d92e7554f1f9207f4

David Keyes

David Keyes

May 30, 2023

I'm pretty sure it's because the template you're using doesn't use the built-in styles. To have header styles applied, for example, you need to use the Word style Heading 1, Heading 2, etc. From the video, it looks like your template is using custom styles that don't match up with the defaults. Does that make sense?

Andrew Paquin

Andrew Paquin

May 31, 2023

Yup. Makes sense. I am more apt to use html or PDF anyway. I can create something in Word if I need it.