How to optionally comment out part of an RMarkdown file

Is there a way to have part of an R Markdown file optionally processed by knitr depending on the value of a parameter. To be clear, I want to be able to include or not markdown, not just a code chunk. Something like

if (params$exclude) (
## don't show this line
### or this one either
}

By "I want to be able to include or not markdown", I assume that you mean text. The way to achieve this, i.e. conditionally including text, would - as far as I am aware - be to utilise the eval option in the code chunk, and then, in that code chunk, write the markdown text you want to conditionally include (e.g. by using cat). There might be other ways to achieve this, though.

Thanks @Equation. Unfortunately, it doesn't work in general to include a code chunk or not. For example, in R Markdown

# A Header

is a header but in a code chunk it's interpreted as a constant.

I want to be able to include or exclude pretty much arbitrary sections of a markdown file. Must be a way to do it, but I haven't a clue as to what it is.

That is possible - check out this thread with the included links, it does seem similar to the problem you are facing: Create RMarkdown headers and code chunks in purrr - #4 by Equation. Let us know how it goes after you've tried.

Hi @Equation. Creating a child document as shown in the link is clearly the elegant and "right" way to do it. Nicely explained too. It doesn't work well for my situation. Basically I have an .RMD file (Beamer) that makes slides to be shown in class. I'd like to produce a second document to post for my students that omits just a few of those slides, for example slides that show the answers. The parts I want to omit include both markdown and code chunks.

While I could write the entire file and then go back and cut and paste parts into separate child documents, it would be pretty painful. Any other thoughts @Equation ? Anyone?

Inspired by @Equation's links, I have found a somewhat kludgy solution. Here's an example:

---
title: "Sample excludes"
author: "Dick Startz"
date: "4/4/2022"
output: pdf_document
params:
  cutout: TRUE
---

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

# First header

```{asis, echo = !params$cutout}
## A second header
```

```{r, include = !params$cutout,echo = FALSE}
pi
```

### A third header
```{asis, echo = !params$cutout}
#### A fourth header
```

The elements here are:
(1) Declare a parameter in the YAML header. (Parameters can also be set in a knit call.)
(2) To exclude R Markdown, use an asis block. Note that the asis blocks also end with three `, I'm just having trouble getting them to show here.
(3) To exclude a coded chunk, just put the include option as usual.

This isn't perfect, since it is annoying to have to do markdown and code chunks separately rather than just conditionally block out a whole section. Will probably work for me, but I would be glad to hear any more clever ideas.

Use (x+1) backticks if you want to show code that contains (x) backticks.

Does the following example works better for you?

---
title: "Untitled"
author: "My name"
date: '2022-04-04'
output: html_document
params:
  do_child: TRUE
---

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

# chapter1
This chapter is the first.
Depending on `params$do_child` that has now value `r params$do_child` we will show a child section
```{r chunk1,eval=params$do_child,child='mychild.Rmd'}
```

with mychild.Rmd e.g.

## section 1.1
This is a section that is not always shown

### Session Info

```{r subsec1}
sessionInfo()
```

I tried adding a fourth backtick. Didn't work. (So I must be doing something else wrong.) Hopefully, what's posted is clear. (Although if anyone would like to edit my post to make it better, feel free.)

Here is a picture, in case it helps
image

@startz I edited your post, and yes the asis engine is the correct way to conditionally include some content

You'll find other recipes in the cookbook if you want to include chunk depending on output format, or need to create some Rmd content (md + chunks) programmatically.

1 Like

@cderv, thanks for the edit.!

1 Like

I have no idea what rules of the road this may violate, but it seems that knitr::opts_chunk$set and knitr::opts_chunk$get can save arbitrary data structures that can be referenced in expressions for option values in individual chunks.

viz.

	---
	title: "dynamic chunk evaluation"
	author: "xxx"
	date: "4/10/2022"
	output: html_document
	---
	
	```{r setup, include=FALSE}
	knitr::opts_chunk$set(
	  chunkEval =list(doRun=TRUE,dontRun=FALSE),
	  echo=TRUE
	)
	```
	
	```{r eval=knitr::opts_chunk$get("chunkEval")$doRun}
	"it RAN from knitr chunk option!"
	```
	
	```{r eval=knitr::opts_chunk$get("chunkEval")$dontRun}
	"did it RUN from knitr chunk option?"
	# it did not
	```

opts_chunk is an internal structure to store chunk options. It is accessible to user so that global option can be modified, but also so that knitr can be extended (usually by package authors offering new knitr engine).

I don't see any advantage of storing doRun and dontRun into a chunk option, compared to just storing them into a usual R object or just calling eval = FALSE on the chunk you don't want to eval. Also knitr::opts_chunk$get is quite long to type and not really made to be called directly in a document, but instead usually called in helper function when access to option value is needed.
Without it, your example would look like this

---
title: "dynamic chunk evaluation"
author: "xxx"
date: "4/10/2022"
output: html_document
---
	
```{r setup, include=FALSE}
doRun <- TRUE
dontRun <- FALSE
```
	
```{r eval=doRun}
"it RAN from knitr chunk option!"
```
	
```{r eval=dontRun}
"did it RUN from knitr chunk option?"
# it did not
```

I would not advice to use opts_chunk this way really.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.