3 Code
An important reason for creating this bookdown template is to present analyses performed in R. Not only do we want to show these results, we also want to show the R code that was used to produce those results. This can be done by using so-called code chunks.
3.1 Code chunks
A code chunk is created by using using two sets of three back ticks (```). In the case of R code, you add a set of parentheses containing the letter r
, followed by a label that describes the code chunk. The actual R code goes between the sets of back ticks. For example, in the code chunk below we use R code to present a summary of the cars
data frame.
```{r cars}
# Print a summary of the 'cars' data frame
summary(cars)
```
This produces the following result:
# Print a summary of the 'cars' data frame summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
By default, the code that produces this result is hidden. Readers who are interested in the code can click on the ‘Toggle code’ button in the navigation bar to show all the code chunks.
Note that the above example table is output in a ‘raw text’ format; this could be styled to be more attractive with packages such as kableExtra
.