diff --git a/R/prep_trajectory.R b/R/prep_trajectory.R index bdf30e56..c26a2ed4 100644 --- a/R/prep_trajectory.R +++ b/R/prep_trajectory.R @@ -33,7 +33,8 @@ prep_trajectory <- function(data, span_5yr = FALSE, value_col = "percentage_of_initial_production_by_scope") { - check_prep_trajectory(data, value_col = value_col) + env <- list(data = substitute(data)) + check_prep_trajectory(data, value_col = value_col, env = env) data <- data %>% prep_common() %>% @@ -47,9 +48,12 @@ prep_trajectory <- function(data, data } -check_prep_trajectory <- function(data, value_col) { +check_prep_trajectory <- function(data, value_col, env) { stopifnot(is.data.frame(data)) crucial <- c(common_crucial_market_share_columns(), value_col) hint_if_missing_names(abort_if_missing_names(data, crucial), "market_share") + enforce_single_value <- c("sector", "technology", "region", "scenario_source") + abort_if_multiple(data, enforce_single_value, env = env) + invisible(data) } diff --git a/tests/testthat/test-plot_trajectory.R b/tests/testthat/test-plot_trajectory.R index 901291f3..d44b2841 100644 --- a/tests/testthat/test-plot_trajectory.R +++ b/tests/testthat/test-plot_trajectory.R @@ -41,15 +41,6 @@ test_that("outputs default axis labels", { expect_equal(p$labels$y, "value") }) -test_that("the errors message includes the name of the user's data", { - # Keep even if already tested in qplot_. Non-standard evaluation is fragile - bad_region <- head(market_share, 2L) %>% - mutate(region = c("a", "b")) %>% - prep_trajectory() - - expect_error(plot_trajectory(bad_region), "bad_region") -}) - test_that("By default doesn't center the Y axis", { data <- example_market_share() %>% prep_trajectory(convert_label = identity, span_5yr = FALSE) diff --git a/tests/testthat/test-prep_trajectory.R b/tests/testthat/test-prep_trajectory.R index 2267ff5c..b1892b51 100644 --- a/tests/testthat/test-prep_trajectory.R +++ b/tests/testthat/test-prep_trajectory.R @@ -44,3 +44,11 @@ test_that("handles span_5yr correctly", { out <- prep_trajectory(example_market_share(), span_5yr = TRUE) expect_true(all(out$year <= min(out$year) + 5)) }) + +test_that("the errors message includes the name of the user's data", { + # Keep even if already tested in qplot_. Non-standard evaluation is fragile + bad_region <- head(market_share, 2L) %>% + mutate(region = c("a", "b")) + + expect_error(prep_trajectory(bad_region), "bad_region") +})