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

Extend validate_has_data to accept vector input #962

Merged
merged 10 commits into from
Nov 20, 2023
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Filter state snapshots can now be uploaded from file. See `?snapshot`.
* Added argument to `teal_slices` and made modifications to `init` to enable tagging `teal_slices` with an app id to safely upload snapshots from disk.
* Added `landing_popup_module` function which creates a module that will display a popup when the app starts. The popup will block access to the app until it is dismissed.
* `validate_has_data` now accepts a `vector` input along with `data.frame`.

# teal 0.14.0

Expand Down
12 changes: 10 additions & 2 deletions R/validations.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Validate that dataset has a minimum number of observations
#'
#' @description `r lifecycle::badge("stable")`
#' @param x a data.frame
#' @param x a data.frame or a vector
#' @param min_nrow minimum number of rows in \code{x}
#' @param complete \code{logical} default \code{FALSE} when set to \code{TRUE} then complete cases are checked.
#' @param allow_inf \code{logical} default \code{TRUE} when set to \code{FALSE} then error thrown if any values are
Expand Down Expand Up @@ -46,7 +46,15 @@ validate_has_data <- function(x,
stopifnot(
"Please provide a character vector in msg argument of validate_has_data." = is.character(msg) || is.null(msg)
)
vedhav marked this conversation as resolved.
Show resolved Hide resolved
validate(need(!is.null(x) && is.data.frame(x), "No data left."))

validate(need(is.vector(x) || is.data.frame(x), "Input must be a vector or a data frame."))

if (is.vector(x) && !is.null(x)) {
x <- data.frame(x)
}

validate(need(!is.null(x), "No data left."))

if (!is.null(min_nrow)) {
if (complete) {
complete_index <- stats::complete.cases(x)
Expand Down
2 changes: 1 addition & 1 deletion man/validate_has_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/test-validate_has_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,73 @@ data <- data.frame(x = 1:10, y = c(1:9, NA), z = c(Inf, 2:10))
testthat::test_that("validate_has_data throws no error when data has at least as many rows as min_nrow", {
testthat::expect_silent(validate_has_data(data, 10))
testthat::expect_silent(validate_has_data(data, 5))
testthat::expect_silent(validate_has_data(data$x, 10))
testthat::expect_silent(validate_has_data(data$x, 5))
})

testthat::test_that("validate_has_data throws error when min_nrow > #rows of data", {
testthat::expect_error(validate_has_data(data, 11))
testthat::expect_error(validate_has_data(data$x, 11))
})

testthat::test_that("validate_has_data accepts logical complete argument", {
testthat::expect_silent(validate_has_data(data[, c("x", "z")], 10, complete = TRUE))
testthat::expect_silent(validate_has_data(data[, c("x", "z")], 10, complete = FALSE))
testthat::expect_silent(validate_has_data(data$x, 10, complete = TRUE))
testthat::expect_silent(validate_has_data(data$x, 10, complete = FALSE))
})

testthat::test_that("validate_has_data throws error when data has NA and complete is set to TRUE", {
testthat::expect_error(validate_has_data(data[, c("x", "y")], 10, complete = TRUE))
testthat::expect_error(validate_has_data(data$y, 10, complete = TRUE))
})

testthat::test_that("validate_has_data accepts logical allow_inf argument", {
testthat::expect_silent(validate_has_data(data[, c("x", "y")], 10, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data[, c("x", "y")], 10, complete = TRUE, allow_inf = FALSE))
testthat::expect_silent(validate_has_data(data$y, 10, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data$y, 10, complete = TRUE, allow_inf = FALSE))
})

testthat::test_that("validate_has_data accepts throws error when data has Inf values and allow_inf is set to FALSE", {
testthat::expect_error(validate_has_data(data[, c("x", "z")], 10, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data$z, 10, allow_inf = FALSE))
})

testthat::test_that("validate_has_data accepts throws error when data has Inf values and allow_inf is set to FALSE", {
testthat::expect_error(validate_has_data(data[, c("x", "z")], 10, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data[, c("x", "z")], 10, complete = TRUE, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data$z, 10, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data$z, 10, complete = TRUE, allow_inf = FALSE))
})

testthat::test_that("validate_has_data allow_inf argument ignores non-numeric columns", {
data <- data.frame(x = 3:5, w = c("A", "B", "C"), z = c(Inf, 4, 5))
testthat::expect_silent(validate_has_data(data[, c("x", "w")], 3, allow_inf = FALSE))
testthat::expect_error(validate_has_data(data, 3, allow_inf = FALSE))
testthat::expect_silent(validate_has_data(data$w, 3, allow_inf = FALSE))
})

testthat::test_that("validate_has_data returns message when msg argument is set", {
testthat::expect_error(
validate_has_data(data, 11, msg = "Check data."),
"Minimum number of records not met: >= 11 records required.\nCheck data."
)
testthat::expect_error(
validate_has_data(data$x, 11, msg = "Check data."),
"Minimum number of records not met: >= 11 records required.\nCheck data."
)
})

testthat::test_that("validate_has_data returns message msg argument is set and complete is set to TRUE", {
testthat::expect_error(
validate_has_data(data[, c("x", "y")], 11, complete = TRUE, msg = "Check data."),
"Number of complete cases is less than: 11\nCheck data."
)
testthat::expect_error(
validate_has_data(data$y, 11, complete = TRUE, msg = "Check data."),
"Number of complete cases is less than: 11\nCheck data."
)
})

testthat::test_that("validate_has_data returns throws error with non-character msg input", {
Expand Down