Skip to content

Commit

Permalink
chore(example tutorials): Better error messages when {gradethis} isn'…
Browse files Browse the repository at this point in the history
…t installed (#802)
  • Loading branch information
gadenbuie authored Sep 27, 2023
1 parent 2859e0c commit 0e1c0d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
8 changes: 6 additions & 2 deletions inst/examples/external/evaluator-tests.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ runtime: shiny_prerendered

```{r setup, include=FALSE}
library(learnr)
if (!rlang::is_installed("gradethis")) {
stop("Please install `gradethis` from GitHub (rstudio/gradethis) for this example.")
}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(exercise.checker = function(...){
library(gradethis)
Expand Down Expand Up @@ -158,7 +162,7 @@ Enter the number 4, then click "Submit Answer" (**not Run Code**)
3
#4
```

```{r excheck-check}
gradethis::grade_result(
gradethis::fail_if(~ identical(.result, 1), "Custom message for value 1."),
Expand All @@ -182,7 +186,7 @@ The following should work just as the previous example did. Click "Submit Answer
3
#4
```

```{r excheck2-check}
grade_result(
fail_if(~ identical(.result, 1), "Custom message for value 1."),
Expand Down
16 changes: 9 additions & 7 deletions inst/tutorials/sql-exercise/sql-exercise.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ description: >

```{r setup, include=FALSE}
library(learnr)
# requires gradethis (>= 0.2.6.9000)
if (!rlang::is_installed("gradethis", version = "0.2.6.9000")) {
stop("Please install `gradethis` from GitHub (rstudio/gradethis) for this example.")
}
library(gradethis)
# Create an ephemeral in-memory RSQLite database
Expand Down Expand Up @@ -95,19 +97,19 @@ grade_this({
.solution <- DBI::dbGetQuery(db_con, .solution_code)

cols_expected <- c("name", "mpg", "cyl")

for (column in cols_expected) {
if (!column %in% names(.result)) {
fail("Did you forget to include `{column}`?")
}
}

extra_cols <- setdiff(names(.result), cols_expected)
if (length(extra_cols)) {
extra_cols <- knitr::combine_words(extra_cols, and = " or ", before = "`")
fail("You don't need the columns {extra_cols}.")
}

pass_if_equal(x = .result[cols_expected])
fail()
})
Expand All @@ -134,19 +136,19 @@ grade_this({
.solution <- DBI::dbGetQuery(db_con, .solution_code)
cols_expected <- c("name", "mpg", "cyl")
for (column in cols_expected) {
if (!column %in% names(.result)) {
fail("Did you forget to include `{column}`?")
}
}
extra_cols <- setdiff(names(.result), cols_expected)
if (length(extra_cols)) {
extra_cols <- knitr::combine_words(extra_cols, and = " or ", before = "`")
fail("You don't need the columns {extra_cols}.")
}
pass_if_equal(x = .result[cols_expected])
fail()
})
Expand Down

0 comments on commit 0e1c0d6

Please sign in to comment.