Code chunks
Enough about markdown, let's get to the fun part and include some code! Look at the last code chunk in the template R Markdown document that you just created, as an example:
```{r pressure, echo = FALSE}
plot(pressure)
```
The R code is surrounded by: ```{r}
and ```
. The r
indicates that the code chunk contains R code (it is possible to add code chunks using other languages, e.g. Python). After that comes an optional chunk name, pressure
in this case (this can be used to reference the code chunk as well as alleviate debugging). Last comes chunk options, separated by commas (in this case there is only one option: echo = FALSE
).
Note
The code chunk name pressure has nothing to do with the code plot(pressure)
. In the latter case, pressure is a default R dataframe that is used in examples. The chunk name happened to be set to the string pressure as well, but could just as well have been called something else, e.g. "Plot pressure data".
Below are listed some useful chunk options related to evaluating and displaying code chunks in the final file:
Chunk option | Effect |
---|---|
echo = FALSE | Prevents code, but not the results, from appearing in the finished file. This is a useful way to embed figures. |
include = FALSE | Prevents both code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. |
eval = FALSE | The code in the code chunk will not be run (but the code can be displayed in the finished file). Since the code is not evaluated, no results can be shown. |
results = "hide" | Evaluate (and display) the code, but don't show the results. |
message = FALSE | Prevents messages that are generated by code from appearing in the finished file. |
warning = FALSE | Prevents warnings that are generated by code from appearing in the finished file. |
- Go back to your template R Markdown document in RStudio and locate the
cars
code chunk. - Add the option
echo = FALSE
:
```{r cars, echo = FALSE}
summary(cars)
```
- How do you think this will affect the rendered file? Press Knit and check if you were right.
- Remove the
echo = FALSE
option and addeval = FALSE
instead:
```{r cars, eval = FALSE}
summary(cars)
```
- How do you think this will affect the rendered file? Press Knit and check if you were right.
- Remove the
eval = FALSE
option and addinclude = FALSE
instead:
```{r cars, include = FALSE}
summary(cars)
```
There are also some chunk options related to plots:
Chunk option | Effect |
---|---|
fig.height = 9, fig.width = 6 | Set plot dimensions to 9x6 inches (the default is 7x7.) |
out.height = "10cm", out.width = "8cm" | Scale plot to 10x8 cm in the final output file. |
fig.cap = "This is a plot." | Add a figure caption. |
- Go back to your template R Markdown document in RStudio and locate the
pressure
code chunk. - Add the
fig.width
andfig.height
options as below:
```{r pressure, echo = FALSE, fig.width = 6, fig.height = 4}
plot(pressure)
```
- Press Knit and look at the output. Can you see any differences?
- Now add a whole new code chunk to the end of the document. Give it the name
pressure 2
(code chunks have to have unique names, or no name). Add thefig.width
andout.width
options like this:
```{r pressure 2, echo = FALSE, fig.width = 9, out.width = "560px"}
plot(pressure)
```
- Press Knit and look at the output. Notice the difference between the two plots? In the second chunk we have first plotted a figure that is a fair bit larger (9 inches wide) than that in the first chunk. Next we have down-sized it in the final output, using the
out.width
option (where we need to use a size metric recognized by the output format, in this case "560px" which works for HTML).
Have you noticed the first chunk?
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
In this way we can set global chunk options, i.e. defaults for all chunks. In this example, echo
will always be set to TRUE
, unless otherwise specified in individual chunks.
Tip
For more chunk options, have a look at page 2-3 of this reference.
It is also possible to create different types of interactive plots using R Markdown. You can see some examples of this here. If you want to try it out you can add the following code chunk to your document:
```{r}
library(networkD3)
data(MisLinks, MisNodes)
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 0.4)
```
Quick recap
In this section you learned how to include code chunks and how to use chunk options to control how the output (code, results and figures) is displayed.