Conditional execution of a chunk

This should be simple, so I assume I am missing something obvious. But I don't see why my chunk is executing when it shouldn't.

---
title: "Test Conditional Execution"
author: "Alan Jackson"
date: '2022-04-05'
output: html_document
params:
  condition: FALSE
---

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

eval_condition <- params$condition
```

```{r, condition, eval=eval_condition}

params$condition
eval_condition
print(paste("I am", params$condition))
```

[1] FALSE
[1] FALSE
[1] "I am FALSE"

I tested your code on my machine and, when knitting the document, the second code chunk did not run.

Are you sure you are knitting the document and not just running individual code chunks? The eval option will only work when the document is knitted (Ctrl+Shift+K). If I run each chunk individually the chunk will run.


An alternative solution for this desired behavior would be to include the define the eval_condition parameter in the setup chunk. Perhaps this might solve your problem...

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

eval_condition <- FALSE
```

1 Like

Yep, it was a dumb mistake. I was using "run all" instead of "knit". Sigh.

1 Like

This topic was automatically closed 7 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.