15.3 Execute content conditionally via the asis
engine
As its name indicates, the asis
engine writes out the chunk content as is. The advantage of using this engine is that you can include some content conditionally—the display of the chunk content is decided by the chunk option echo
. When echo = FALSE
, the chunk will be hidden. Below is a simple example:
```{r}
getRandomNumber <- function() {
sample(1:6, 1)
}
```
```{asis, echo = getRandomNumber() == 4}
According to https://xkcd.com/221/, we just generated
a **true** random number!
```
The text in the asis
chunk will be displayed only if the condition getRandomNumber() == 4
is (randomly) true.