diff --git a/R/xpdb_edits.R b/R/xpdb_edits.R index ea63695d..2c6c5980 100755 --- a/R/xpdb_edits.R +++ b/R/xpdb_edits.R @@ -364,8 +364,12 @@ check_quo_vars <- function(xpdb, ..., .source, .problem) { irep <- function(x, quiet = FALSE) { if (missing(x)) stop('argument "x" is missing, with no default', call. = FALSE) if (is.factor(x)) x <- as.numeric(as.character(x)) - x <- dplyr::if_else(dplyr::lag(x, default = x[1]) > x, 1, 0) - x <- cumsum(x) + 1 + lagcheck <- dplyr::lag(x, default = x[1]) != x + dupcheck <- duplicated(x) + check <- dplyr::if_else(lagcheck & dupcheck, 1, 0) + ilen <- which(check==1)[1] - 1 + if (length(ilen)==0 || is.na(ilen)) ilen <- length(x) + x <- rep(1:(length(x)/ilen), each=ilen) msg(c('irep: ', max(x), ' simulations found.'), quiet) x } diff --git a/tests/testthat/test-edits.R b/tests/testthat/test-edits.R index 0af46f74..41814052 100755 --- a/tests/testthat/test-edits.R +++ b/tests/testthat/test-edits.R @@ -74,6 +74,9 @@ test_that('irep works properly', { expect_message(irep_out <- irep(rep(1:5, time = 3), quiet = FALSE), regexp = '3 simulations found') expect_equal(irep_out, rep(1:3, each = 5)) + expect_message(irep_out2 <- irep(rep(c(10,5,6), time = 7), quiet = FALSE), + regexp = '7 simulations found') + expect_equal(irep_out2, rep(1:7, each = 3)) }) test_that('n() works', {