Search code examples
rr-markdownquartoreproducible-researchtargets-r-package

Validating successful execution of R scripts from top to bottom in a clean session without errors


I teach multiple R Courses where students have to submit an analysis they did on a specific dataset. As a first, (small) step towards reproducibility, I want to make sure the script they are submitting ran at least once on their local computer with the following constraints. It ran:

  1. in a new session
  2. from top to bottom
  3. without any errors.

Until now, I've used RMarkdown / Quarto to "enforce" this. Rendering a R/Qmd file creates a new session for the code, which is executed from top to bottom, and by default errors halt the rendering process. The resulting output file (typically PDF or HTML) gives me confidence that the above requirements are met. (Of course, there are relatively easy ways to trick me, but these require malice, which I do not assume).

What I don't like about this approach is that the students are forced to work with R/Qmds rather than R-scripts. This generates additional overhead (e.g. when debugging) and sources of errors (issues related to RMarkdown / Quarto, rather than to the code).

Another approach would be to use targets, which is a great package but also generates much overhead that would completely overwhelm the students (who are R and programming novices).

Can anyone recommend a different, simple approach to my problem?


Solution

  • One potential solution is the students use reprex to render their submissions.

    Pros:

    • runs their code in a clean session
    • prints warnings/errors along with the code
    • you can copy/paste their code, along with their output, and run it on your own computer 'as is' to confirm their submission is valid
    • relatively straightforward to use:
      • installing the package installs an RStudio plugin
      • you can copy your code / the whole script to the clipboard then run reprex::reprex(wd = ".", venue = "html") to get a rendered html file and a markdown file in your 'current working directory' for submission
      • you can reprex an R script, e.g. reprex(input = "my_reprex.R")
    • if/when students come to stackoverflow for help, they'll know how to reprex their minimal reproducible example, making their code easier for us to run and ensuring they include the libraries they're using, eliminating typos, etc
    • in my opinion, the docs are very good; they are easy to understand and implement if you have a basic understanding of R
    • you can add the sessionInfo() to the reprex (session_info = TRUE) to help you support your students with troubleshooting/issues/bugs/etc

    Cons:

    • html/markdown submission files can be edited (may invite attempts to cheat, although running their submission on your computer and getting a different checksum would catch this)
    • if students are using Rstudio cloud, or something similar, the steps are slightly different (explained in the docs, definitely not an insurmountable problem)
    • the reprex docs are very good, but 'first timers' / beginners may struggle with some of the concepts (you may need to explain some of the concepts in more detail)

    Overall, I think it's a good option if the students read the docs and you provide clear instructions/expectations for the final submission file/s.