2

The exercise tutorial to the learnr package refers to the checkr packages:

The checkr package currently provides code checking functions that are compatible with learnr.

(Be aware that this reference does not point to the CRAN package checkr. Unfortunately, there is a duplicate package name.)

But in the learnr tutorial there is no advice how to use it together with checkr. On the other hand, the vignette in checkr is still a draft version, discusses advanced issues and is lacking an easy example in relation to learnr.

What I am looking for is a concrete procedure of a model example. For instance: How to check student input with the two-plus-twoexample, provided by the learnrR Markdown template "Interactive Tutorials".

So my question is: How to check with learnr the R code required to add two plus two using the checkr package?


Maybe this questions should use new tags (learnr and checkr) but I do not have the privileges to do so.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
petzi
  • 1,430
  • 1
  • 17
  • 32

1 Answers1

1

After many experiments the following procedure worked for me:

  1. I have started a new project and loaded the R Markdown template "Interactive Tutorial" and named it "01-Exercises". RStudio generated a folder this name and put my "01-Exercises.Rmd" in this folder.
  2. I have added the line tutorial_options(exercise.checker = checkr::check_for_learnr) to the setup chunk of my "01-Exercises.Rmd" file.
  3. I have added a new R chunk with the label two-and-two-check. (= The same name as the chunk name of the student task but added -check.)
  4. In this R chunk I have added just one line check_two_and_two(USER_CODE) which will become my test function.
  5. I have written a test function check_two_and_two in an extra script file "check_test.R" and saved under a folder "www".
  6. I have sourced this script to the function into the memory. I ran rmarkdown::run("01-Exercises/01-Exercises.Rmd") from the console.

And here is my test function:

check_two_and_two <- function(USER_CODE) {
    code <- for_checkr(USER_CODE)
    t1 <- line_where(code, insist(all(F == "+"), "Your operator is {{F}}. This is not the assigned task."))
    if (failed(t1)) return(t1)
    t2 <- line_where(code, insist(all(V == 4), "Your solution is {{V}}. This is not the result (= 4) asked for."))
    if (failed(t2)) return(t2)
    line_binding(code, 2 + 2, failif(FALSE, "The pattern did not match."), message = "The result is correct, but I was looking for 2 + 2.")
}

Even if this worked for me: Maybe there is a better (more effective) solution?

petzi
  • 1,430
  • 1
  • 17
  • 32