Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 20, 2024
1 parent 1ccfd42 commit ef5e1e6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions R/data_expand.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#' @param data A data frame.
#' @param expand The name of the column that contains the counts of replications
#' for each row.
#' @param remove_na Logical, if `TRUE`, missing values (`NA`) in the column
#' provided in `expand` are removed from the data frame. If `FALSE` and `expand`
#' contains missing values, the function will throw an error.
#' @param ... Currently not used.
#' @inheritParams find_columns
#'
Expand Down Expand Up @@ -65,6 +68,19 @@ data_expand <- function(data,
# also remove "expand" from "select" string
select <- setdiff(select, expand)

# if user doesn't want to remove "NA", but replicates contain "NA",
# give informative error here
if (!remove_na && anyNA(replicates)) {
insight::format_error(
"The column provided in `expand` contains missing values, but `remove_na` is set to `FALSE`.",
"Please set `remove_na` to `TRUE` or remove the missing values from the data frame."
)
}

# remove rows where "expand" is NA
data <- data[!is.na(replicates), ]
replicates <- replicates[!is.na(replicates)]

# fin
as.data.frame(do.call(cbind, lapply(data[select], function(variable) {
unlist(Map(rep, variable, replicates), use.names = FALSE)
Expand Down
4 changes: 4 additions & 0 deletions man/data_expand.Rd

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

35 changes: 35 additions & 0 deletions tests/testthat/test-data_expand.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
test_that("data_expand: simple use case", {
data(mtcars)
d <- as.data.frame(head(mtcars))
out <- data_expand(d, "carb")
expect_identical(dim(out), c(13L, 10L))
expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 108, 258, 360, 360, 225))
expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

d$mpg[5] <- NA
out <- data_expand(d, "carb")
expect_identical(dim(out), c(13L, 10L))
expect_identical(out$mpg, c(21, 21, 21, 21, 21, 21, 21, 21, 22.8, 21.4, NA, NA, 18.1))
expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

d$carb[3] <- NA
out <- data_expand(d, "carb", remove_na = TRUE)
expect_identical(dim(out), c(12L, 10L))
expect_identical(out$mpg, c(21, 21, 21, 21, 21, 21, 21, 21, 21.4, NA, NA, 18.1))
expect_named(out, c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear"))

out <- data_expand(d, "carb", select = c("disp", "hp"), remove_na = TRUE)
expect_identical(dim(out), c(12L, 2L))
expect_identical(out$disp, c(160, 160, 160, 160, 160, 160, 160, 160, 258, 360, 360, 225))
expect_named(out, c("disp", "hp"))
})

test_that("data_expand: errors", {
data(mtcars)
d <- as.data.frame(head(mtcars))
expect_error(data_expand(d), regex = "No column")
expect_error(data_expand(d, expand = c("mpg", "gear")), regex = "a single string")
expect_error(data_expand(d, expand = "geas"), regex = "The column provided")
d$carb[3] <- NA
expect_error(data_expand(d, "carb"), regex = "missing values")
})

0 comments on commit ef5e1e6

Please sign in to comment.