If we want R code to run, but not show either the code nor any
output, we include the option include=FALSE
Chunk Names: You'll notice that after
{r
we include a name like
hide-code-and-output
. These names must be unique for each
chunk, preferably with words separated with hyphens -
.
Usually we don't want warnings or messages from our R
code in reports either, so we would also include
warning=FALSE
and include=FALSE
:
To hide just the code, but display the output (text-based output,
plots, etc.), we include the option echo=FALSE
. In
reports, we normally don't want to display the code or any warnings or
messages, so we could include all these options in each chunk:
A more efficient way to do this, however, would be to modify the chunk at the beginning of our markdown when we create a new R markdown file in R:
If we set these chunk options at the beginning, we can omit the
options echo=FALSE, warning=FALSE, message=FALSE
in our
individual code chunks. We can override them if we want, e.g.
by including echo=TRUE
.
One of the most useful sets of chunk options allows to change how
plots appear, such as fig.width
, fig.height
,
fig.align
, and so on. For example, we could use
fig.width
and fig.height
make the previous
plot squarer:
Setting fig.width
and fig.height
is for the
whole plot area so, if we have a multiple-frame plot (i.e.
using mfrow=
or mfcol=
in the
par()
function), we would need to adjust the dimensions to
match. For example:
Plots of two time series: atmospheric CO₂ concentrations (left), and sunspot numbers (right).
We add captions using
fig.caption="<figure caption text>"
. This is a better
option for reporting than including a title above plots using the
main=
option
Figure 1: Time series of atmospheric CO₂ concentrations from 1959 to 1998, measured at the Mauna Loa observatory in Hawaii.
We could just use normal markdown text to insert a caption, but using
fig.caption
can allow automatic cross-referencing of
figures and tables to their captions using the bookdown
R
package.
At this stage it's useful to note that with a long chunk header
(within {}
), such as that shown above, we should not insert
any line breaks. The chunk will return an error and not work unless
there are no line breaks in the chunk header.
bookdown
packageThe R package bookdown
is very powerful, but so far I
only use it for its ability to provide automatic Figure and Table
numbering, and cross-referencing.
You would need to run the following R code if you don't have
bookdown
installed:
You would also need to include the following in the yaml
header at the top of your R markdown document:
---
⋮
output:
bookdown::html_document2:
⋮
---
This should give a menu option to
Knit to html_document2
, which is what we should use later
to prepare our formatted document.
We can then use chunk names to cross reference figures and tables. For example, for the chunk above plotting two figures...
...we would cross-reference this by including
Figure \@ref(fig:co2-and sunspot-plots)
at the appropriate
place in our text.
Notes
-
), and
all chunk names must be uniqueFigure
or a figure
number at the start of the fig.cap=
text stringSimilarly, table captions can be cross-referenced using
Table \@ref(tab:table-chunk-name)
in the text outside of
any code chunks. We would need to set a table caption in the chunk
header using tab.cap=
, or use a package like
flextable
or knitr::kable()
and include table
caption options from those packages.
Option | Purpose |
---|---|
out.width= | Sets the width of a figure as a proportion of the output page width, |
fig.align= | Aligns the figure on the output page |
results='hold' | Waits until all the code in a chunk has run before printing any text output |
paged.print= | If TRUE (the default) adds html format to printed data frames; |
You can see a more complete table in the R Markdown Cheatsheet.
R Markdown Cheat Sheet (Posit Software, 2024)
Getting used to R, RStudio, and R Markdown – Chapter 4 in the excellent (and free) eBook by Chester Ismay
Chunk Options – Chapter 11 in R markdown cookbook by Xie et al. (2025)
Ismay, Chester (2016) Getting used to R, RStudio, and R Markdown. https://bookdown.org/chesterismay/rbasics/ (accessed 2025-06-17)
Posit Software (2024) rmarkdown::
CHEATSHEET. https://rstudio.github.io/cheatsheets/rmarkdown.pdf
(accessed 2025-06-17)
Xie, Yihui (2020) Chunk Options and Package Options. https://yihui.org/knitr/options/ (accessed 2025-06-17)
Xie Y, Dervieux C, Riederer E (2025) R Markdown Cookbook. https://bookdown.org/yihui/rmarkdown-cookbook/ (accessed 2025-06-17)
CC-BY-SA • All content by Ratey-AtUWA. My employer does not necessarily know about or endorse the content of this website.
Created with rmarkdown in RStudio. Currently using the free yeti theme from Bootswatch.