Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 23, 2024
1 parent 5de51d9 commit c2ccd12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 8 additions & 5 deletions R/data_replicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ data_replicate <- function(data,
)
}

# check that "expand" contains no Inf
if (any(is.infinite(data[[expand]]))) {
insight::format_error(
"The column provided in `expand` contains infinite values. Please provide a column that does not contain infinite values." # nolint
)
}

# check that "expand" is integer
if (!.is_integer(data[[expand]])) {
insight::format_error(
Expand Down Expand Up @@ -101,11 +108,7 @@ data_replicate <- function(data,

.is_integer <- function(x) {
tryCatch(
if (is.infinite(x)) {
FALSE
} else {
all(x %% 1 == 0)
},
all(x %% 1 == 0),
warning = function(w) is.integer(x),
error = function(e) FALSE
)
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-data_replicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ test_that("data_replicate: simple use case", {
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"))

d <- data.frame(
a = c("a", "b", "c"),
b = 1:3,
rep = c(3, 2, 4),
stringsAsFactors = FALSE
)
out <- data_replicate(d, "rep")
expect_identical(out$a, c("a", "a", "a", "b", "b", "c", "c", "c", "c"))
})


test_that("data_replicate: errors", {
data(mtcars)
d <- head(mtcars)
Expand All @@ -38,4 +48,7 @@ test_that("data_replicate: errors", {
expect_error(data_replicate(d, expand = "qsec"), regex = "The column provided")
d$carb[3] <- NA
expect_error(data_replicate(d, "carb"), regex = "missing values")
d <- head(mtcars)
d$carb[3] <- Inf
expect_error(data_replicate(d, "carb"), regex = "infinite values")
})

0 comments on commit c2ccd12

Please sign in to comment.