I am writing a learnr tutorial and find that some exercise chunks do not work and give me unclear errors. The code I am using works fine interactively, so something must be going wrong inside the exercise chunk itself. It may be that the exercise setup does not work well, that the data I use isn't read in correctly, or perhaps the chunk is using a function that I specify elsewhere inside the tutorial but isn't accessed inside the exercise chunk.
This is not about a specific error, but my question is about how we can debug a specific exercise chunk in a learnr tutorial. Ideally, I am looking for something like browser() which is very useful in interactive settings. In other words, my ideal would be that I click "Run Document" and that some browser-like tool then opens when that specific exercise chunk is reached, so I can see what exactly is going on inside.
I have tried adding browser() to the chunk, but that doesn't open the browser for me.
Is there something similar that I can use? Is there something that I can put inside the exercise chunk to create some browser()-like power to trace what is going on inside the exercise chunk itself?
The crux of the problem is that the document rendering happens in a background session in RStudio that isn't attached to the R console and where browser() doesn't work. You can reach a similar problem by trying to add browser() in a regular R chunk — if you click the "Knit" button you also won't be able to debug a chunk.
The work-around (for both regular R Markdown and learnr tutorials) is to call the rendering function in your R console. For learnr that means calling rmarkdown::run(), giving it the path to your tutorial source file, and for regular .Rmds that's rmarkdown::render().
In learnr's case, rmarkdown::run() will start up the interactive tutorial. You can put browser() into any exercise chunk in the Rmd source and when you click on the "Submit Answer" button the evaluation will pause in the debugger when it reaches your browser() call.
If that doesn't help reveal your problem, please feel free to post some more details about your tutorial. It would be helpful to reduce the problem to a single exercise that reproduces your issue.
Thank you, that helps a bit.
The main challenge is that this requires manually going through each chunk and I have a lot of tutorials and they all necessarily depend on external packages that are in active development. So, it is a lot of work doing this manually and frequently.
What would be great is to be able to add some unit tests inside each learnr chunk that can be run as part of a test run (Like what you would do using testthat or tinytest on a package) and that would not be shown to users when they run the tutorial.
That way, we could run the test automatically every month or so for all tutorials and quickly see if a change in an external package (or in learnr or gradethis, etc.) affects a specific chunk in a specific tutorial.
Until then, this is a useful test that we do anyway, once in a while.