rstudio / learnr

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

Error if `-solution` chunk has an unneeded `exercise.setup` option #779

Open rossellhayes opened 1 year ago

rossellhayes commented 1 year ago

Using an exercise.setup option in a -solution results in an error if the chunk is not referenced in the exercise.setup option of another chunk.

---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)

Exercise 1

x <- 1

Exercise 2

y <- x + 1

Exercise 3

z <- x + y

``` r
rmarkdown::run("setup-chaining.Rmd")
#> processing file: setup-chaining.Rmd
#> Quitting from lines 35-36 (setup-chaining.Rmd)
#> Error: Chunk 'exercise-3-solution' is not being used by any exercise or exercise setup chunk.
#> Please remove chunk 'exercise-3-solution' or reference 'exercise-3-solution' with `exercise.setup = 'exercise-3-solution'`

Created on 2023-04-14 with reprex v2.0.2

This can cause problems for authors who are testing an unfinished exercise that uses setup chaining. They will have to remember not to add an exercise.setup option to exercise-3-solution until after they've written exercise-4.

Would it cause any issues if this threw a warning instead of an error?

nischalshrestha commented 1 year ago

This error actually occurs anytime there is a dangling final setup chunk not being used anywhere else in the tutorial:

---
title: "Tutorial with unused setup chunk"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)
x <- 2
y <- x + 1

will yield:

Error: Chunk 'second_setup' is not being used by any exercise or exercise setup chunk. Please remove chunk 'second_setup' or reference 'second_setup' with exercise.setup = 'second_setup'



This error was initially put in place just as a safeguard that users might accidentally forget to use a setup chunk, but authors might want a warning instead to nudge them instead of totally halting rendering of document to check other things.

But, we'll need to look at [these lines](https://github.com/rstudio/learnr/blob/1ffad2a752b21a782da54bd243221e325f489e0c/R/knitr-hooks.R#L127-L133) and that general function for chained setup chunks to verify if there are other reasons to error. 🤔