3 minutes
RMarkdown: Common Word Tasks Rmarkdown
September 8, 2020
Now that you have the rmd command to start building documents you should already know how to do a lot stuff in markdown and how it will translate into MS word.
The flavor of markdown that R markdown uses is Pandoc markdown which has a nice reference here.
With that you can get away with most of the content of a document that you would want.
---
output: word_document
---
# My awesome document
Templates
MS Word
For creating templates for word documents it couldn’t be any easier. You just create a document and then update the styles to what you want.
Then you can reference it in the header like this.
---
output:
word_document:
reference_docx: ./Template.docx
---
More reference documents on this here. The main one to keep an eye on is how you need to find the table style.
HTML
For html you can create a template html file by doing the following:
output:
html_output:
template: my-template.html
Then you can create a template html file like the following. You simply have to place a $body$
tag where you want the content to be placed.
<head>
</head>
<body>
$body$
</body>
You also have the ability to place content from the header into the template too like the following
foo: bar
output:
html_output:
template: my-template.html
<head>
</head>
<body>
<p> My foo is a $foo$ </p>
$body$
</body>
For the full documentation on html output see the documentation. There is a heap of things you can change.
PDF generation is done via LaTeX which means you have a million options (also the reason by the docker image is over 2GB in size).
The basics however are you can specify different parts of the resulting LaTeX document with the includes setting (each of them is optional).
---
output:
pdf_document:
includes:
in_header: "preamble.tex"
before_body: "before.tex"
after_body: "after.tex"
---
As you can see you can add code to the preamble and also before and after the body of the document. I won’t go into the syntax for latex here because its a massive topic and would need a tutorial of its own.
I will however put some example templates in the git repository.
It is also worth looking at the r markdown documentation on what you can do here too.
Multiple output
Another thing that is cool about R Markdown is you can also output to multiple formats at the same time. To do this you just simply have to create multiple entries under output like the following:
output:
pdf_document: default
html_document: default
RMarkdown Series
- 01 - Replacing MS Office
- 02 - Setting up R Markdown
- 03 - How to do common word tasks in R Markdown
- 04 - Generating presentations in R Markdown and Reval.JS
- 05 - Working with data and R Markdown
- 06 - Generating flow charts
- 07 - Creating books in Bookdown
- 08 - Misc other tools
- 09 - Co-operating with other people