13.7 Create Word template file
To make sure tables always export with a suitable font size, you may edit your Word file but only to create a new template. You will then use this template to Knit the R Markdown document again.
In the Word document the first example outputted, click on a table.
The style should be compact
:
Right-click > Modify… > font size = 9
Alter heading and text styles in the same way as desired.
Save this as colonTemplate.docx
(avoid underscores in the name of this file).
Move the file to your project folder and reference it in your .Rmd
YAML header, as shown below.
Make sure you get the spacing correct, unlike R code, the YAML header is sensitive to formatting and the number of spaces at the beginning of the argument lines.
Finally, to get the figure printed in a size where the labels don’t overlap each other, you will have to specify a width for it.
The Chunk cog introduced in the previous chapter is a convenient way to change the figure size (it is in the top-right corner of each grey code chunk in an R Markdown document).
It usually takes some experimentation to find the best size for each plot/output document; in this case we are going with fig.width = 10
.
Knitting Example 2 here gives us Figure 13.2B). For something that is generated automatically, it looks awesome.
---
title: "Example knitr/R Markdown document"
author: "Your name"
date: "22/5/2020"
output:
word_document:
reference_docx: colonTemplate.docx
---
```{r setup, include=FALSE}
# Load data into global environment.
library(finalfit)
library(dplyr)
library(knitr)
load(here::here("data", "out.rda"))
```
## Table 1 - Demographics
```{r table1, echo = FALSE}
kable(table1, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))
```
## Table 2 - Association between tumour factors and 5 year mortality
```{r table2, echo = FALSE}
kable(table2, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))
```
## Figure 1 - Association between tumour factors and 5 year mortality
```{r figure1, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10}
explanatory = c( "differ.factor", "age", "sex.factor",
"extent.factor", "obstruct.factor",
"nodes")
dependent = "mort_5yr"
colon_s %>%
or_plot(dependent, explanatory,
breaks = c(0.5, 1, 5, 10, 20, 30))
```