Skip to content

Test autoplot and arg-checker functions #409

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

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions R/autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,37 @@ autoplot.epi_df <- function(
# --- check for numeric variables
allowed <- purrr::map_lgl(object[non_key_cols], is.numeric)
allowed <- allowed[allowed]
if (length(allowed) == 0) {
cli::cli_abort("No numeric variables were available to plot automatically.")
if (length(allowed) == 0 && rlang::dots_n(...) == 0L) {
cli::cli_abort("No numeric variables were available to plot automatically.",
class = "epiprocess__no_numeric_vars_available")
}
vars <- tidyselect::eval_select(rlang::expr(c(...)), object)
if (rlang::is_empty(vars)) { # find them automatically if unspecified
vars <- tidyselect::eval_select(names(allowed)[1], object)
cli::cli_warn(
"Plot variable was unspecified. Automatically selecting {.var {names(allowed)[1]}}."
"Plot variable was unspecified. Automatically selecting {.var {names(allowed)[1]}}.",
class = "epiprocess__unspecified_plot_var"
)
} else { # if variables were specified, ensure that they are numeric
ok <- names(vars) %in% names(allowed)
if (!any(ok)) {
cli::cli_abort(
"None of the requested variables {.var {names(vars)}} are numeric."
"None of the requested variables {.var {names(vars)}} are numeric.",
class = "epiprocess__all_requested_vars_not_numeric"
)
} else if (!all(ok)) {
cli::cli_warn(c(
"Only the requested variables {.var {names(vars)[ok]}} are numeric.",
i = "`autoplot()` cannot display {.var {names(vars)[!ok]}}."
))
),
class = "epiprocess__some_requested_vars_not_numeric")
vars <- vars[ok]
}
}

# --- create a viable df to plot
pos <- tidyselect::eval_select(
rlang::expr(c("time_value", geo_and_other_keys, names(vars))), object
rlang::expr(c("time_value", tidyselect::all_of(geo_and_other_keys), names(vars))), object
)
if (length(vars) > 1) {
object <- tidyr::pivot_longer(
Expand Down
73 changes: 0 additions & 73 deletions R/utils-arg.R

This file was deleted.

87 changes: 87 additions & 0 deletions tests/testthat/test-autoplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
library(dplyr)

d <- as.Date("2020-01-01")

raw_df_chr <- dplyr::bind_rows(
dplyr::tibble(geo_value = "ak", time_value = d + 1:5, value = "a"),
dplyr::tibble(geo_value = "al", time_value = d + 1:5, value = "d")
)
ungrouped_chr <- as_epi_df(raw_df_chr, as_of = d + 6)
grouped_chr <- ungrouped_chr %>%
group_by(geo_value)

raw_df_num <- dplyr::bind_rows(
dplyr::tibble(geo_value = "ak", time_value = d + 1:5, value = 11:15),
dplyr::tibble(geo_value = "al", time_value = d + 1:5, value = 1:5)
)
ungrouped_num <- as_epi_df(raw_df_num, as_of = d + 6)
grouped_num <- ungrouped_num %>%
group_by(geo_value)

test_that("autoplot fails if no non-key columns are numeric", {
expect_error(autoplot(ungrouped_chr),
class = "epiprocess__no_numeric_vars_available"
)

# Multiple non-numeric columns
testdf <- mutate(ungrouped_chr, value2 = "d")
expect_error(autoplot(testdf),
class = "epiprocess__no_numeric_vars_available"
)

expect_error(autoplot(grouped_chr),
class = "epiprocess__no_numeric_vars_available"
)

# A numeric column is available, but is a key not a value.
testdf <- mutate(raw_df_chr, key1 = c(1:5, 5:9)) %>%
as_tsibble(index = time_value, key = c(geo_value, key1)) %>%
as_epi_df(as_of = d + 6)
expect_error(autoplot(testdf),
class = "epiprocess__no_numeric_vars_available"
)
})

test_that("autoplot warns when a variable is not specified, and lists the auto-selected column", {
expect_warning(autoplot(ungrouped_num),
regexp = ".*selecting `value`[.]",
class = "epiprocess__unspecified_plot_var"
)

expect_warning(autoplot(grouped_num),
regexp = ".*selecting `value`[.]",
class = "epiprocess__unspecified_plot_var"
)
})

test_that("autoplot errors when all specified columns are not numeric, and lists column names", {
expect_error(autoplot(ungrouped_chr, value),
regexp = ".*value.*",
class = "epiprocess__all_requested_vars_not_numeric"
)

testdf <- mutate(ungrouped_chr, value2 = "d")
expect_error(autoplot(testdf, value, value2),
regexp = ".*variables `value` and `value2` are.*",
class = "epiprocess__all_requested_vars_not_numeric"
)

expect_error(autoplot(grouped_chr, value),
regexp = ".*variables `value` are.*",
class = "epiprocess__all_requested_vars_not_numeric"
)
})

test_that("autoplot warns when some specified columns are not numeric, and lists column names", {
testdf <- mutate(ungrouped_num, value2 = "d")
expect_warning(autoplot(testdf, value, value2),
regexp = ".*`value` are numeric.*cannot display `value2`.*",
class = "epiprocess__some_requested_vars_not_numeric"
)

testdf <- mutate(grouped_num, value2 = "d")
expect_warning(autoplot(testdf, value, value2),
regexp = ".*`value` are numeric.*cannot display `value2`.*",
class = "epiprocess__some_requested_vars_not_numeric"
)
})