This is great because I can check the object (and rlang is always installed in the image because ggplot2 imports it).
There are 2 small problems:
It's not intuitive, and
It requires a custom message every time because "Running rlang::eval_tidy(p$mapping$x, p$data) didn't give the correct result." will scare all but the bravest students.
I think these problems can be nicely solved by having a check_aes() that wraps this functionality.
Something like
check_aes <- function(state, plot, aesthetic) {
cmd <- sprintf("rlang::eval_tidy(%s$mapping[[%s]], %s$data)", plot, aesthetic, plot)
aes_error_msg <- sprintf("Evaluating the %s aesthetic for plot %s threw an error.", aesthetic, plot)
aes_incorrect_msg <- sprintf("The %s aesthetic for plot %s has the wrong value.", aesthetic, plot)
ex() %>%
check_expr(cmd) %>%
check_result(error_msg = aes_error_msg) %>%
check_equal(incorrect_msg = aes_incorrect_msg)
}
One bigger problem is that it requires that the student assign the plot to a variable. I thought I could use last_plot() to retrieve the last plot that the students drew, but evaluation of that plot fails.
@richierocks I currently do not have time for this, but feel free to add this utility function in testwhat_ext.. If you make a PR, I'll gladly review it.
I'm just starting to go through the ggplot2 courses updating SCTs to not use
test_ggplot()
.For a simple plot like this:
I'm writing SCTs like this:
So far, the most unsatisfying part is checking the aesthetics, since you always have to do
eval = FALSE
.ggplot2
v3 is built on top ofrlang
, so I can now doThis is great because I can check the object (and
rlang
is always installed in the image becauseggplot2
imports it).There are 2 small problems:
I think these problems can be nicely solved by having a
check_aes()
that wraps this functionality.Something like
One bigger problem is that it requires that the student assign the plot to a variable. I thought I could use
last_plot()
to retrieve the last plot that the students drew, but evaluation of that plot fails.last_plot()
effectively doesasNamespace("ggplot2")$.store$get()
, and I've no idea howtestwhat
tries to evaluate that.