15.2 Using parameters

You can access the parameters within the knitting environment and the R console in RStudio.12 The values are contained within a read-only list called params. In the previous example, the parameters can be accessed as follows:

params$year
params$region

Parameters can also be used to control the behavior of knitr. For example, the knitr chunk option echo controls whether to display the program code, and we can set this option globally in a document via a parameter:

---
params:
  printcode: false  # or set it to true
---

```{r, setup, include=FALSE}
# set this option in the first code chunk in the document
knitr::opts_chunk$set(echo = params$printcode)
```

  1. Parameters will not be available immediately after loading the file, but require any line of the report to be executed first.