Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier testing of base graphics #6

Open
richierocks opened this issue May 3, 2018 · 0 comments
Open

Easier testing of base graphics #6

richierocks opened this issue May 3, 2018 · 0 comments

Comments

@richierocks
Copy link
Contributor

richierocks commented May 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants