UWA logoMaterial to support teaching in Environmental Science at The University of Western Australia

Units ENVT3361, ENVT4461, and ENVT5503

Hiding both code and output

If we want R code to run, but not show either the code nor any output, we include the option include=FALSE

```{r hide-code-and-output, include=FALSE}
# the R markdown chunk option include = FALSE is used when it's not
# helpful to see the code and output, for example loading packages
library(car)
library(lmtest)
library(robustbase)
```

 

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:

```{r hide-code-output-warnings-messages, include=FALSE, warning=FALSE, message=FALSE}

```

 

Hiding the code

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:

```{r co2-plot-hide-code, echo=FALSE, warning=FALSE, message=FALSE}
par(mar=c(3,3,1,1), mgp=c(1.7,0.3,0), tcl=0.2)
# co2 is a built-in R dataset
plot(co2, col="blue3", type="l", xlab="Date",
     ylab=expression(paste("Atmospheric ",CO[2]," concentration at Mauna Loa")))
```

 

 

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:
```{r set-knitr-chunk-options}
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
```

 

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.

Re-sizing plots

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:

```{r co2-plot-squarer, echo=FALSE, warning=FALSE, message=FALSE, fig.width=5, fig.height=5}

```

 

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:

```{r co2-and sunspot-plots, echo=FALSE, warning=FALSE, message=FALSE, fig.width=10, fig.height=5, fig.cap="Plots of two time series: atmospheric CO₂ concentrations (left), and sunspot numbers (right)."}
par(mfrow=c(1,2), mar=c(3,3,1,1), mgp=c(1.7,0.3,0), tcl=0.2, font.lab=2)
# co2 and sunspots are built-in R datasets
plot(co2, col="blue3", type="l", xlab="Date",
     ylab=expression(bold(paste("Atmospheric ",CO[2]," concentration at Mauna Loa"))), 
     main=expression(paste("Atmospheric ",CO[2]," concentration at Mauna Loa")))
plot(sunspots, col="firebrick", type="l", xlab="Date",
     ylab="Relative number of sunspots", main="Monthly Sunspot Numbers, 1749–1983")
```

 

Plots of two time series: atmospheric CO₂ concentrations (left), and sunspot numbers (right).

Plots of two time series: atmospheric CO₂ concentrations (left), and sunspot numbers (right).

 

Adding figure captions to plots

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

```{r co2-plot-caption, echo=FALSE, warning=FALSE, message=FALSE, fig.width=5, fig.height=5, fig.align='center', fig.cap="Figure 1: Time series of atmospheric CO₂ concentrations from 1959 to 1998, measured at the Mauna Loa observatory in Hawaii."}

```
Figure 1: Time series of atmospheric CO₂ concentrations from 1959 to 1998, measured at the Mauna Loa observatory in Hawaii.

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.

 

Using the bookdown package

The 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:

if(!require(bookdown)) install.packages("bookdown")

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...

```{r co2-and sunspot-plots, echo=FALSE, warning=FALSE, message=FALSE, fig.width=10, fig.height=5, fig.cap="Plots of two time series: atmospheric CO₂ concentrations (left), and sunspot numbers (right)."}

```


...we would cross-reference this by including Figure \@ref(fig:co2-and sunspot-plots) at the appropriate place in our text.

Notes

  1. chunk names can not contain spaces, periods, underscores, or slashes; words should just be separated by hyphens (-), and all chunk names must be unique
  2. we don't need to include the word Figure or a figure number at the start of the fig.cap= text string

Similarly, 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.

 

Other chunk options

Table 1: Some useful options for controlling the output from code chunks in R markdown.

Option

Purpose

out.width=

Sets the width of a figure as a proportion of the output page width,
e.g. out.width='50%'

fig.align=

Aligns the figure on the output page
e.g. fig.align='center'

results='hold'

Waits until all the code in a chunk has run before printing any text output
(other choices are 'markup', 'hide', 'asis')

paged.print=

If TRUE (the default) adds html format to printed data frames;
if FALSE output is just text

You can see a more complete table in the R Markdown Cheatsheet.

 

Other R Markdown Resources

References

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.