Home > Mobile >  RMarkdown Using comments to make sections optional
RMarkdown Using comments to make sections optional

Time:12-22

Is it possible to have entire sections programmatically commented out in RMarkdown. For example, say I have a section of my report :

### 2.1.   Heading

Blah blah blah my description, with `r variables` and other stuff.


#### Table 2.1.1. Sub heading

```{r table_2_1_1}

kable_2_1_1

```

Now based on a boolean in the code, I want to completely comment out that whole section. I've tried putting r variables into the report like "< !--" & "-->" but that does not work.

CodePudding user response:

You can pass a boolean value to eval in your rmd chunk to conditionally knit different blocks of rmarkdown code:

---
title: "Untitled"
author: "Matt"
date: "12/21/2021"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)

should_i_evaluate <- F
```

```{r header, eval = should_i_evaluate}
asis_output("# My Header")
```

```{asis, echo = should_i_evaluate}
 Blah blah blah my description, with `r names(mtcars[1:3])` and other stuff.
```

```{r cars, eval = should_i_evaluate}
summary(mtcars)
```

When should_i_evaluate is TRUE:

When should_i_evaluate is FALSE:

  • Related