rstudio / learnr

Interactive Tutorials with R Markdown
https://pkgs.rstudio.com/learnr
Apache License 2.0
705 stars 236 forks source link

Setup chunks for pre-evaluated exercises: source hidden but output is shown #587

Open gadenbuie opened 2 years ago

gadenbuie commented 2 years ago

When an exercise is pre-evaluated with exercise.eval = TRUE, the setup chunks are all evaluated as well and the output is shown. But echo=FALSE is enforced on the setup chunk, so it's not possible to override this behavior.

Reprex ```` --- title: "Tutorial" output: learnr::tutorial runtime: shiny_prerendered --- ```{r setup, include=FALSE} library(learnr) ``` ## Topic 1 ```{r two-plus-two-setup, echo=TRUE} x <- 2 x ``` ```{r two-plus-two, exercise=TRUE, exercise.eval = TRUE} x + 2 ``` ````

image

I see two options for when exercise.eval=TRUE:

I like the second option best as it's most consistent with our other modes.

OTOH, the first option can be used as a way to have setup chunks appear in both the exercise and the pre-rendered tutorial, rather than using ref.label or other chunk-repeating techniques. I'm less excited about this option since it requires pre-evaluating the exercise code, which may contain errors or be incomplete, and it's a rather large side-effect to result from setting the exercise.eval chunk option.

gadenbuie commented 1 year ago

When exercise.eval = FALSE:

  1. Act as if eval = FALSE and echo = FALSE were added to the chunk
  2. If eval = TRUE is set on the chunk, evaluate the setup chunk in prerender also
  3. If echo = TRUE is set on the chunk, pre-render uses echo = TRUE, include = <chunk_setting> %||% TRUE so that the chunk is echoed and output is shown in the static content.

When exercise.eval = TRUE, do the same as the above, but ensure that the chunk is evaluated as if it had eval = TRUE.