Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally reveal (or hide) exercise solution (#470)
Co-authored-by: Barret Schloerke <barret@rstudio.com> Co-authored-by: gadenbuie <gadenbuie@users.noreply.github.com>
- Loading branch information
1 parent
dd4338c
commit b36e840
Showing
12 changed files
with
294 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
```{r filter, exercise=TRUE} | ||
# filter the flights table to include only United and American flights | ||
flights | ||
``` | ||
|
||
```{r filter-hint-1} | ||
filter(flights, ...) | ||
``` | ||
|
||
```{r filter-hint-2} | ||
filter(flights, UniqueCarrier=="AA") | ||
``` | ||
|
||
```{r filter-solution, exercise.reveal_solution = FALSE} | ||
filter(flights, UniqueCarrier=="AA" | UniqueCarrier=="UA") | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
context("Optionally reveal solution") | ||
|
||
render_tutorial_with_reveal_solution <- function(opt_string) { | ||
ex <- readLines(test_path("tutorials", "optional-show-solution.Rmd")) | ||
ex <- sub("#<<reveal_solution>>", opt_string, ex, fixed = TRUE) | ||
|
||
tut_rmd <- tempfile(fileext = ".Rmd") | ||
on.exit(unlink(tut_rmd)) | ||
|
||
writeLines(ex, tut_rmd) | ||
tut_html <- rmarkdown::render(tut_rmd, quiet = TRUE) | ||
|
||
# overwrite exit handler to remove all tutorial files | ||
on.exit({ | ||
rmarkdown::shiny_prerendered_clean(tut_rmd) | ||
unlink(tut_html) | ||
unlink(tut_rmd) | ||
}, add = FALSE) | ||
|
||
paste(readLines(tut_html), collapse = "\n") | ||
} | ||
|
||
default_solution <- "<code># DEFAULT SOLUTION 4631b0</code>" | ||
hidden_solution <- "<code># HIDDEN SOLUTION 48da3c</code>" | ||
shown_solution <- "<code># SHOWN SOLUTION 781cbb</code>" | ||
ex_opt_solution <- "<code># EXERCISE OPT SOLUTION 15c861</code>" | ||
var_hide_solution <- "<code># HIDDEN VAR SOLUTION 0b219b</code>" | ||
var_show_solution <- "<code># SHOWN VAR SOLUTION aba888</code>" | ||
|
||
test_that("Solutions are revealed or hidden with tutorial_options()", { | ||
skip_if_not(rmarkdown::pandoc_available()) | ||
|
||
ex_show <- render_tutorial_with_reveal_solution("tutorial_options(exercise.reveal_solution = TRUE)") | ||
expect_match(ex_show, default_solution, fixed = TRUE) | ||
expect_failure(expect_match(ex_show, hidden_solution, fixed = TRUE)) | ||
expect_match(ex_show, shown_solution, fixed = TRUE) | ||
expect_match(ex_show, ex_opt_solution, fixed = TRUE) | ||
expect_failure(expect_match(ex_show, var_hide_solution, fixed = TRUE)) | ||
expect_match(ex_show, var_show_solution, fixed = TRUE) | ||
|
||
ex_hide <- render_tutorial_with_reveal_solution("tutorial_options(exercise.reveal_solution = FALSE)") | ||
expect_failure(expect_match(ex_hide, default_solution, fixed = TRUE)) | ||
expect_failure(expect_match(ex_hide, hidden_solution, fixed = TRUE)) | ||
expect_match(ex_hide, shown_solution, fixed = TRUE) | ||
expect_match(ex_hide, ex_opt_solution, fixed = TRUE) | ||
expect_failure(expect_match(ex_hide, var_hide_solution, fixed = TRUE)) | ||
expect_match(ex_hide, var_show_solution, fixed = TRUE) | ||
}) | ||
|
||
test_that("Solutions are revealed or hidden with global option", { | ||
skip_if_not(rmarkdown::pandoc_available()) | ||
|
||
ex_show <- render_tutorial_with_reveal_solution("options(tutorial.exercise.reveal_solution = TRUE)") | ||
expect_match(ex_show, default_solution, fixed = TRUE) | ||
expect_failure(expect_match(ex_show, hidden_solution, fixed = TRUE)) | ||
expect_match(ex_show, shown_solution, fixed = TRUE) | ||
expect_match(ex_show, ex_opt_solution, fixed = TRUE) | ||
expect_failure(expect_match(ex_show, var_hide_solution, fixed = TRUE)) | ||
expect_match(ex_show, var_show_solution, fixed = TRUE) | ||
|
||
ex_hide <- render_tutorial_with_reveal_solution("options(tutorial.exercise.reveal_solution = FALSE)") | ||
expect_failure(expect_match(ex_hide, default_solution, fixed = TRUE)) | ||
expect_failure(expect_match(ex_hide, hidden_solution, fixed = TRUE)) | ||
expect_match(ex_hide, shown_solution, fixed = TRUE) | ||
expect_match(ex_hide, ex_opt_solution, fixed = TRUE) | ||
expect_failure(expect_match(ex_hide, var_hide_solution, fixed = TRUE)) | ||
expect_match(ex_hide, var_show_solution, fixed = TRUE) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.