Running R code chunk only once in Quatro

Hey,
I am trying to compose a Quatro presentation, but I came upon a problem. I have the very first r code chunk that I need it to be run only once. It constructs a simulated dataset. I keep rerendring as I go and the dataset keeps changing as well. Is there a way that I could run this code chunk only once?
Thnaks

If the only problem is that the dataset keeps changing when created , setting the random seed could help:

set.seed(2023)
(x<- runif(3))
#> [1] 0.4666139 0.3351910 0.1628176
# some hours later in a new quatro run ...
set.seed(2023)
(x<- runif(3))
#> [1] 0.4666139 0.3351910 0.1628176
Created on 2023-05-06 with reprex v2.0.2
1 Like

One of these may help. If the generated object isn’t something that needs to be kept current (such as fake data), just saveRDS(object, “filename”) outside the qmd and put object = readRDS(“filename”) in the setup chunk and it will be in namespace for subsequent chunks.

Thank you so much for your input.
The dataset is the major problem, but it is not the only one. I am wondering if there is a way I could change the code chunk option dynamically. That is, certain chunk/s are run based on certain conditions on the runtime.

I think part of your code might be not visible?
(If you want to show code with three backticks enclose the code with four backticks).

Now we have no way to see clearly what you are doing.

Did you see Yihui's remarks on objects and hooks ?

Thank you so much for your input.
Actually, I did, but I truly do not know why it was not showing the whole code.

Please check now. I have update the code. Thank you so much.

Hello @F_SL ,

I have done some tests (see code below) and I think that the eval settings determine if the chunk and hook code is executed.
If eval == F the chunk is not executed and the same goes for the hooks.
Only when eval == T the chunk and the hooks are executed. If eval is set to Fin a hook this will have no impact on the current chunk any more and only will evaluated for the next chunk.
I think that this is the reason that only chunks test2 and test3 are executed.
So in my opinion (and I can be wrong) you can not use hooks to determine if the current chunk should be executed or not. You should use the direct method from test5 and test6.

---
title: "Untitled"
author: "My name"
date: "`r Sys.Date()`"
output: html_document
---

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

knitr::opts_hooks$set(
  dode = function(options) {
    if (options$dode == TRUE) {
      opts_chunk$set(eval = TRUE)
    } else {
      opts_chunk$set(eval = FALSE)
    }
    options
  }
)
```

```{r test1,dode=TRUE}
str(opts_chunk$get(c('dode','eval')))
str(opts_current$get(c('dode','eval')))
cat("test1\n")
1+2*3
```

```{r test2,dode=TRUE}
cat("test2\n")
str(opts_chunk$get(c('dode','eval')))
str(opts_current$get(c('dode','eval')))
1+2*3
```

```{r test3,dode=FALSE}
cat("test3\n")
str(opts_chunk$get(c('dode','eval')))
str(opts_current$get(c('dode','eval')))
1+2*3
```

```{r test4,dode=TRUE}
cat("test4\n")
str(opts_chunk$get(c('dode','eval')))
1+2*3
```

```{r evaloff, eval=TRUE,echo=FALSE}
dode2 <- FALSE
```

```{r test5,eval=dode2}
cat("test5\n")
str(opts_chunk$get(c('dode','eval')))
str(opts_current$get(c('dode','eval')))
1+2*3
```

```{r evalon, eval=TRUE,echo=FALSE}
dode2 <- TRUE
```

```{r test6,eval=dode2}
cat("test6\n")
str(opts_chunk$get(c('dode','eval')))
str(opts_current$get(c('dode','eval')))
1+2*3
```
1 Like

About this, did you consider caching ?

1 Like

Thank you so much!!!
That worked, indeed.
:+1:t3::+1:t3::+1:t3:

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.