Skip to content

Commit 02a92c3

Browse files
committed
added "valid, but empty input" test, fix, refs carriedaymont#49
1 parent e8e0724 commit 02a92c3

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

R/growth.R

+20-16
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,12 @@ cleangrowth <- function(subjid,
564564

565565
# adult: send to cleanadult to do most of the work ----
566566

567-
if (!quietly){
568-
cat(sprintf("[%s] Begin processing adult data...\n", Sys.time()))
569-
}
570-
571567
# no need to do this if there's no data
572568
if (nrow(data.adult) > 0){
569+
if (!quietly){
570+
cat(sprintf("[%s] Begin processing adult data...\n", Sys.time()))
571+
}
572+
573573
# TODO: MAKE THIS BETTER -- FUNCTION OR SOMETHING
574574
# TODO: BATCH LOGS??
575575
# if parallel processing is desired, load additional modules
@@ -659,19 +659,23 @@ cleangrowth <- function(subjid,
659659
}
660660

661661

662-
# join with pediatric data
663-
full_out <- data.table(
664-
line = c(ret.df$line, res$line),
665-
exclude = c(as.character(ret.df$exclude), res$result),
666-
mean_sde = c(rep(NA, nrow(ret.df)), res$mean_sde)
667-
)
668-
full_out[, exclude := factor(exclude, levels = unique(c(exclude.levels,
669-
unique(exclude))))]
670-
full_out <- full_out[order(line),]
671-
# remove column added for keeping track
672-
full_out[, line := NULL]
662+
if (any(nrow(data.all) > 0, nrow(data.adult) > 0)) {
663+
# join with pediatric data
664+
full_out <- data.table(
665+
line = c(ret.df$line, res$line),
666+
exclude = c(as.character(ret.df$exclude), res$result),
667+
mean_sde = c(rep(NA, nrow(ret.df)), res$mean_sde)
668+
)
669+
full_out[, exclude := factor(exclude, levels = unique(c(exclude.levels,
670+
unique(exclude))))]
671+
full_out <- full_out[order(line),]
672+
# remove column added for keeping track
673+
full_out[, line := NULL]
673674

674-
return(full_out$exclude)
675+
return(full_out$exclude)
676+
} else {
677+
return(c())
678+
}
675679

676680
}
677681

tests/testthat/test-cleangrowth.R

+12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test_that("growthcleanr works without either adult or pediatric data", {
9494
# using default cutpoint -- 20
9595
only_peds <- syngrowth[syngrowth$agedays < 20*365.25,][1:50,]
9696
only_adult <- syngrowth[syngrowth$agedays >= 20*365.25,][1:50,]
97+
nobody <- syngrowth[syngrowth$agedays > 120*365.25,]
9798

9899
# testing cleangrowth works without adult data
99100
peds_res <- cleangrowth(
@@ -119,4 +120,15 @@ test_that("growthcleanr works without either adult or pediatric data", {
119120

120121
expect_equal(length(adult_res), nrow(only_adult))
121122

123+
# testing cleangrowth works with no data
124+
no_res <- cleangrowth(
125+
nobody$subjid,
126+
nobody$param,
127+
nobody$agedays,
128+
nobody$sex,
129+
nobody$measurement,
130+
quietly = T
131+
)
132+
133+
expect_equal(length(no_res), nrow(nobody))
122134
})

0 commit comments

Comments
 (0)