Objects not found in next code chunk

I'm having major issues creating a learnr tutorial, but this only seems to be an issue in the very middle of my tutorial...

I'm using the palmerpenguins dataset for the tutorial and things are all fine and dandy.

My setup chunk looks like:

knitr::opts_chunk$set(echo = TRUE)

This code chunk works perfectly:

## loading ALL tidyverse packages at once because we'll be using many of them, normaly required
## filtering for Chinstrap AND Gentoo penguins from the species column of penguins
penguin_chin_gent <-filter(penguins, species == "Chinstrap" & species == "Gentoo")

## filtering for Chinstrap OR Gentoo penguins from the species column of penguins
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
# print the first couple rows

And then when I get to this code chunk:

## what are the column names of our dataset again? Let's check
## OK, so body weight is found under column "body_mass_g". Let's select body_mass_g and species so that we
## can calculate the mean body weight of each species.

## what does this function do?
count(penguin_chin_gent_bw, species)

The users encounter the error: object 'penguin_chin_gent' not found.

This might be a beginner R markdown issue but I can't seem to figure out the problem here. Other data persists between code chunks, just not this one object.

Any help GREATLY appreciated!!

learnR by default creates a new R session for each chunk.

In one respect it means if the student makes a complete mess of chunk1 they don't break the whole thing.

But they relatively recently added the ability to chain chunks together, effectively you can rerun chunk1 (let's hope there is no massive code there) as a setup using exercise.setup

So in {r chunk name, exercise.setup="PreviousChunk"}

Fantastic! This is so helpful!

However, you have also brought my attention to the very reason this is not a default option... So, I've added code chunks with {r, echo=F } that include the correct required objects throughout the tutorial to make it run smoothly and to prevent any breaking by the student!

The echo=F chunks could be set to be hints - if you are happy to let the student have something they can just copy.

Likewise exercise.setup calls the version the student was offered. So if your task was to calculate a+b as a variable called c

{r firstChunk}
a = 5
b = 10

If your next task was to divide c by 5 and create a variable d, you can't call the chunk and use c, because c doesn't exist.

But you could have a chunk with echo=F that is

{r hiddenChunk, echo=F, exercise.setup="firstChunk"}
c <- a + b
{r secondExercise, exercise.setup="hiddenChunk"}

Awesome! This has saved my weekend from being an utter failure.

Thank you so much!

This is working 100% on my local machine, but then when I deploy on shinyapps.io I'm hit with the same issue.

The weird part is that it was working last night on shinyapps.io (even confirmed by a student!). I just reverted back to the working version and...not working anymore.

Have you had any issues with this? Here is the code now... sorry for the formatting.

First chunk:
{r filter, exercise=TRUE} penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")

Second chunk:
{r subsetfallback, echo=F, exercise.setup="select"} penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")

Third chunk (Does not work on shinyapps.io... I get the object not found error.):
{r select, exercise=T} penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)

Third chunk has no setup attribute?

Oh infact. Second calls the third. Do they need to run in order?

Ah. I think you are calling them the wrong way round.

Remove the exercise.setup in chunk2 and add

exercise.setup="sunset fallback"

To chunk3

HAH, yes I did. Wow. Let this be a lesson to myself not to decide to randomly create a tutorial using a new-to-me R package days before the class :smile:

Thank you again for all your very quick responses!!

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.