You can write code to draw base plots in several ways. For example, the following are all equivalent.
plot(cars$speed, cars$dist)
plot(dist ~ speed, cars)
with(cars, plot(speed, dist))
library(magrittr)
cars %$% plot(speed, dist)
Writing SCTs to check for all syntaxes is tricky. You have to do something like this:
test_or({
ex() %>% check_function('plot') %>% {
check_arg(., 'x') %>% check_equal()
check_arg(., 'y') %>% check_equal()
}
}, {
ex() %>% override_solution('plot(dist ~ speed, cars))') %>% check_function('plot') %>% {
check_arg(., 'formula') %>% check_equal()
check_arg(., 'data') %>% check_equal()
}
}, {
ex() %>% override_solution('with(cars, plot(speed, dist))') %>% check_function('with') %>% {
check_arg(., 'data') %>% check_equal()
check_arg(., 'expr') %>% check_function('plot') %>% {
check_arg(., 'x') %>% check_equal(eval = FALSE)
check_arg(., 'y') %>% check_equal(eval = FALSE)
}
}
}, {
ex() %>% override_solution('cars %$% plot(speed, dist)') %>% {
check_code(., 'cars %$%', fixed = TRUE)
check_function(., 'plot') %>% {
check_arg(., 'x') %>% check_equal(eval = FALSE)
check_arg(., 'y') %>% check_equal(eval = FALSE)
}
}
})
Ideally, there would be a test_base_plot()
function that handled all the specific cases.
One complication is that formula syntax is not supported for all base plot types. For example, hist()
doesn't allow this. So you'd need a completely separate test_base_hist()
.
It might be easier from a technical point of view if the plot was first converted to a grid plot, in order to have an object to test. You can do this using grid.echo()
and grid.grab()
from the gridGraphics
package.
See https://journal.r-project.org/archive/2015-1/murrell.pdf
FYI For base plotting, the Data Visualization in R course has longer, messy SCTs for the different cases in Ch1 and Ch2 (I didn’t get round to doing them for the later chapters yet) https://github.com/datacamp/courses-data-vis-in-r