Search code examples
rquarto

Evaluate code block in quarto dependant on evaluation of an R variable


I have the following quarto document, and I want to execute the second code block depending on the value of OK. As below, it does not work. I tried double and triple backticks, using only OK, but nothing worked. How can I do this?

---
title: "Test"
format: html
---

## Set a variable

```{r}
OK <- FALSE
```

## Running Code depending on value of `OK`

```{r}
#| eval: `r OK`
print("OK is TRUE - otherwise you won't see anything here.")
```

Solution

  • You can use R code as chunk option values by prefacing them with !expr.

    ---
    title: "Test"
    format: html
    ---
    
    ## Set a variable
    
    ```{r}
    OK <- FALSE
    ```
    
    ## Running Code depending on value of `OK`
    
    ```{r}
    #| eval: !expr OK
    
    print("OK is TRUE - otherwise you won't see anything here.")
    ```
    

    use of r code as chunk option