8.6 Some tips and tricks
Problem :
When rendering my R markdown document to pdf my code runs off the edge of the page.
Solution:
Add a global_options argument at the start of your .Rmd file in a code chunk:
```{r, global_options, include=FALSE}
knitr::opts_chunk$set(message=FALSE, tidy.opts=list(width.cutoff=60), tidy=TRUE)
```
This code chunk won’t be displayed in the final document due to the include = FALSE
argument and you should place the code chunk immediately after the YAML header to affect everything below that.
tidy.opts = list(width.cutoff = 60), tidy=TRUE
defines the margin cutoff point and wraps text to the next line. Play around with this value to get it right (60-80 should be OK for most documents).
Problem:
When I load a package in my R markdown document my rendered output contains all of the startup messages and/or warnings.
Solution:
You can load all of your packages at the start of your R markdown document in a code chunk along with setting your global options.
```{r, global_options, include=FALSE}
knitr::opts_chunk$set(message=FALSE, warning=FALSE, tidy.opts=list(width.cutoff=60))
suppressPackageStartupMessages(library(ggplot2))
```
The message=FALSE
and warning=FALSE
arguments suppress messages and warnings. The suppressPackageStartupMessages(library(ggplot2))
will load the ggplot2
package but suppress startup messages.
Problem:
When rendering my R markdown document to pdf my tables and/or figures are split over two pages.
Solution:
Add a page break using the LateX \pagebreak
notation before your offending table or figure.
Problem:
The code in my rendered document looks ugly!
Solution:
Add the argument tidy=TRUE
to your global arguments. Sometimes, however, this can cause problems especially with correct code indentation.