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 6 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
33 changes: 24 additions & 9 deletions R/utils-arg.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ arg_is_scalar <- function(..., allow_null = FALSE, allow_na = FALSE) {
...,
tests = function(name, value) {
if (length(value) > 1 | (!allow_null & length(value) == 0)) {
cli::cli_abort("Argument {.val {name}} must be of length 1.")
cli::cli_abort("Argument {.val {name}} must be of length 1.",
class = "epiprocess__value_not_length_1"
)
}
if (!is.null(value)) {
if (is.na(value) & !allow_na) {
cli::cli_abort(
"Argument {.val {name}} must not be a missing value ({.val {NA}})."
"Argument {.val {name}} must not be a missing value ({.val {NA}}).",
class = "epiprocess__value_is_na"
)
}
}
Expand All @@ -29,7 +32,9 @@ arg_is_numeric <- function(..., allow_null = FALSE) {
...,
tests = function(name, value) {
if (!(is.numeric(value) | (is.null(value) & allow_null))) {
cli::cli_abort("All {.val {name}} must numeric.")
cli::cli_abort("All {.val {name}} must be numeric.",
class = "epiprocess__value_is_null_or_not_numeric"
)
}
}
)
Expand All @@ -40,8 +45,10 @@ arg_is_int <- function(..., allow_null = FALSE) {
handle_arg_list(
...,
tests = function(name, value) {
if (!(all(value %% 1 == 0) | (is.null(value) & allow_null))) {
cli::cli_abort("All {.val {name}} must be whole positive number(s).")
if (!( (all(value %% 1 == 0) && all(value > 0)) | (is.null(value) & allow_null))) {
cli::cli_abort("All {.val {name}} must be whole positive number(s).",
class = "epiprocess__some_decimal_or_negative_elements"
)
}
}
)
Expand All @@ -52,16 +59,24 @@ arg_is_chr <- function(..., allow_null = FALSE, allow_na = FALSE, allow_empty =
...,
tests = function(name, value) {
if (is.null(value) & !allow_null) {
cli::cli_abort("Argument {.val {name}} may not be `NULL`.")
cli::cli_abort("Argument {.val {name}} may not be `NULL`.",
class = "epiprocess__value_is_null"
)
}
if (any(is.na(value)) & !allow_na) {
cli::cli_abort("Argument {.val {name}} must not contain any missing values ({.val {NA}}).")
cli::cli_abort("Argument {.val {name}} must not contain any missing values ({.val {NA}}).",
class = "epiprocess__some_na_elements"
)
}
if (!is.null(value) & (length(value) == 0L & !allow_empty)) {
cli::cli_abort("Argument {.val {name}} must have length > 0.")
cli::cli_abort("Argument {.val {name}} must have length > 0.",
class = "epiprocess__value_length_0"
)
}
if (!(is.character(value) | is.null(value) | all(is.na(value)))) {
cli::cli_abort("Argument {.val {name}} must be of character type.")
cli::cli_abort("Argument {.val {name}} must be of character type.",
class = "epiprocess__not_character_type"
)
}
}
)
Expand Down
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"
)
})
80 changes: 80 additions & 0 deletions tests/testthat/test-utils-arg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
test_that("arg_is_scalar basic behavior", {
expect_no_error(arg_is_scalar(d = 1, "a", 2, c = c("1"), a = list(2)))

expect_error(arg_is_scalar(c(3, 5, 5)),
class = "epiprocess__value_not_length_1"
)

expect_no_error(arg_is_scalar(NULL, allow_null = TRUE))
expect_error(arg_is_scalar(NULL),
class = "epiprocess__value_not_length_1"
)

expect_no_error(arg_is_scalar(NA, allow_na = TRUE))
expect_error(arg_is_scalar(NA),
class = "epiprocess__value_is_na"
)
})

test_that("arg_is_numeric basic behavior", {
expect_no_error(arg_is_numeric(c = 1.25, b = 2:5, 1, c(2.22, 2.12)))

for (val in list(list(1), "a", list(NULL))) {
expect_error(arg_is_numeric(val),
class = "epiprocess__value_is_null_or_not_numeric"
)
}

expect_no_error(arg_is_numeric(1, c(1.255, 2.33, 3), NULL, allow_null = TRUE))
expect_error(arg_is_numeric(1, c(1.255, 2.33, 3), NULL),
class = "epiprocess__value_is_null_or_not_numeric"
)
})

test_that("arg_is_int basic behavior", {
expect_no_error(arg_is_int(c = 1, 1, 3, b = 2:5))
expect_no_error(arg_is_int(NULL, 1, allow_null = TRUE))

for (val in list(1.25, -(1:3))) {
expect_error(arg_is_int(val),
class = "epiprocess__some_decimal_or_negative_elements"
)
}
})

test_that("arg_is_chr basic behavior", {
expect_no_error(arg_is_chr(c = c("a", "value"), d = "a", "d"))

expect_no_error(arg_is_chr(NULL, allow_null = TRUE)) #
for (val in list(NULL)) {
expect_error(arg_is_chr(val), #
class = "epiprocess__value_is_null"
)
}

expect_no_error(arg_is_chr(NA, c(NA, NA, NA), c(NA, "a"), allow_na = TRUE))
for (val in list(NA, c(NA, NA, NA), c(NA, "a"))) {
expect_error(arg_is_chr(val),
class = "epiprocess__some_na_elements"
)
}

expect_no_error(arg_is_chr(c("a", "value"), character(0), list(), allow_empty = TRUE))
for (val in list(character(0), list())) {
expect_error(arg_is_chr(val),
class = "epiprocess__value_length_0"
)
}

for (val in list(c(5, 4), list(5, 4), 5)) {
expect_error(arg_is_chr(val),
class = "epiprocess__not_character_type"
)
}
})

test_that("arg_is_chr_scalar basic behavior", {
expect_no_error(arg_is_chr_scalar("a", "b", c = "c"))
expect_no_error(arg_is_chr_scalar(c = "c"))
})