Search code examples
rstatisticslearnr

It is possible to store in memory code from one exercise to another in learnr?


I have created a tutorial in learnr and I have noticed that for each code chunk that I create, I need to rerun the code from previous chunks if I wish to use the same data.

For instance, if I have something like this :

oneway <- aov(data=iris, Petal.Length~Species)

which I make in one exercise chunk, and I run:

summary(oneway)

in the next chunk, it will not recognize "oneway" anymore.


Solution

  • This is a "feature" of learnr such that any exercise can be done at any time without relying on previous steps.

    The way around this is by setting up shared prepare blocks:

    ```{r prepare-a}
    oneway <- aov(data=iris, Petal.Length~Species)
    ```
    ```{r a, exercise=TRUE}
    oneway
    ```
    
    ```{r b, exercise=TRUE, exercise.setup = "prepare-a"}
    summary(oneway)
    ```
    

    See Exercise Setup for more info.