Before I submit this as a bug report, I thought I'd toss it out to the community for verification...
Can anyone get grade_this_code()
to work in a -code-check
chunk?
The help file for grade_this_code()
states the following:
If
grade_this_code()
is called in a-code-check
chunk and returns feedback, either passing or failing feedback, then the user's code is not executed. If you want the user to see the output of their code, callgrade_this_code()
in the -check chunk.
grade_this_code()
in a -check
chuck, it works as expected when an exercise is submitted.-check
chunk into a -code-check
chunk, the tutorial raises an error: "A problem occurred with the grading code for this exercise" when an exercise is submitted.grade_this_code()
to grade_code()
in the -code-check
chunk, everything works as expected when an exercise is submitted, but grade-code()
is tagged as superseded by grade_this_code()
learnr: version 0.11.5.9000
gradethis: version 0.2.14
Here is a reproducible example that can be put in an RMarkdown file for execution.
---
title: "Reproducible"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
gradethis::gradethis_setup()
```
## Topic 1
Write the R code required to add two plus two:
```{r two-plus-two, exercise=TRUE}
```
```{r two-plus-two-solution}
2+2
```
<!-- The following works fine: note `-check` chunk with `grade_this_code()`
```{r two-plus-two-check}
gradethis::grade_this_code()
```
-->
<!-- The following works fine: note `-code-check` chunk with `grade_code()`
```{r two-plus-two-code-check}
gradethis::grade_code()
```
-->
<!-- the following fails with an error: `-code-check` chunk with `grade_this_code() -->
```{r two-plus-two-code-check}
gradethis::grade_this_code()
```
TL;DR Please simply use the grade_code
further:) as a superseded function has a known better alternative, but the function itself is NOT going away.
First of all, the issue is most probably connected with gradethis
package. I found out that the last issue in gradethis
github repository is from Sep 2023 and there is no response yet. The introduction stated that It is in the development stage and is not yet on CRAN. The last commit to the repo was half a year ago. Summing up, there is a significant probability that the solution is almost abandoned.
Confirmed: When I add the a chunk with "-code-check" suffix (check without evaluation) then gradethis::grade_this_code()
will raise an error.
You have an easy and reproducible example to present the error, so for sure, it is enough to be added as an issue on github. Please remember to add session info. However, be aware that there is a significant chance you will not get any response.
In the docs of grade_this_code()
, we can find out, "If grade_this_code() is called in a -code-check chunk and returns feedback, either passing or failing feedback, then the user's code is not executed." so for sure, such functionality should be supported.
Please remember a superseded function has a known better alternative, but the function itself is NOT going away. So feel free to continue the use of the grade_code()
function.
Error message:
Loading required package: shiny
Loading required package: gradethis
Attaching package: ‘gradethis’
The following objects are masked from ‘package:learnr’:
random_encouragement, random_praise
Listening on http://127.0.0.1:4532
#> grade_this({
#> .message <- code_feedback()
#> if (is.null(.message)) {
#> if ("pass" %in% get(".__action")) {
#> pass(get(".__correct"))
#> }
#> }
#> else {
#> if ("fail" %in% get(".__action")) {
#> fail(get(".__incorrect"), hint = FALSE)
#> }
#> }
#> invisible(NULL)
#> })(check_env)
Error in (function (e) { on_error(e, this_env)})(structure(list(message = "Assertion on 'user_env' failed: Must be an environment, not 'NULL'.", call = code_feedback()), class = c("simpleError", "error", "condition"))): Assertion on 'user_env' failed: Must be an environment, not 'NULL'.
#> grade_this({
#> .message <- code_feedback()
#> if (is.null(.message)) {
#> if ("pass" %in% get(".__action")) {
#> pass(get(".__correct"))
#> }
#> }
#> else {
#> if ("fail" %in% get(".__action")) {
#> fail(get(".__incorrect"), hint = FALSE)
#> }
#> }
#> invisible(NULL)
#> })(check_env)
Error in (function (e) { on_error(e, this_env)})(structure(list(message = "Assertion on 'user_env' failed: Must be an environment, not 'NULL'.", call = code_feedback()), class = c("simpleError", "error", "condition"))): Assertion on 'user_env' failed: Must be an environment, not 'NULL'.