From 02a92c33ab71e3d2e0bf28ffcffb1477d4a367d4 Mon Sep 17 00:00:00 2001 From: Daniel Chudnov Date: Thu, 10 Jun 2021 14:35:01 -0400 Subject: [PATCH] added "valid, but empty input" test, fix, refs https://github.com/carriedaymont/growthcleanr/issues/49 --- R/growth.R | 36 +++++++++++++++++-------------- tests/testthat/test-cleangrowth.R | 12 +++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/R/growth.R b/R/growth.R index d3c5942d..f10227b9 100644 --- a/R/growth.R +++ b/R/growth.R @@ -564,12 +564,12 @@ cleangrowth <- function(subjid, # adult: send to cleanadult to do most of the work ---- - if (!quietly){ - cat(sprintf("[%s] Begin processing adult data...\n", Sys.time())) - } - # no need to do this if there's no data if (nrow(data.adult) > 0){ + if (!quietly){ + cat(sprintf("[%s] Begin processing adult data...\n", Sys.time())) + } + # TODO: MAKE THIS BETTER -- FUNCTION OR SOMETHING # TODO: BATCH LOGS?? # if parallel processing is desired, load additional modules @@ -659,19 +659,23 @@ cleangrowth <- function(subjid, } - # join with pediatric data - full_out <- data.table( - line = c(ret.df$line, res$line), - exclude = c(as.character(ret.df$exclude), res$result), - mean_sde = c(rep(NA, nrow(ret.df)), res$mean_sde) - ) - full_out[, exclude := factor(exclude, levels = unique(c(exclude.levels, - unique(exclude))))] - full_out <- full_out[order(line),] - # remove column added for keeping track - full_out[, line := NULL] + if (any(nrow(data.all) > 0, nrow(data.adult) > 0)) { + # join with pediatric data + full_out <- data.table( + line = c(ret.df$line, res$line), + exclude = c(as.character(ret.df$exclude), res$result), + mean_sde = c(rep(NA, nrow(ret.df)), res$mean_sde) + ) + full_out[, exclude := factor(exclude, levels = unique(c(exclude.levels, + unique(exclude))))] + full_out <- full_out[order(line),] + # remove column added for keeping track + full_out[, line := NULL] - return(full_out$exclude) + return(full_out$exclude) + } else { + return(c()) + } } diff --git a/tests/testthat/test-cleangrowth.R b/tests/testthat/test-cleangrowth.R index b3fa3588..4b89e5f4 100644 --- a/tests/testthat/test-cleangrowth.R +++ b/tests/testthat/test-cleangrowth.R @@ -94,6 +94,7 @@ test_that("growthcleanr works without either adult or pediatric data", { # using default cutpoint -- 20 only_peds <- syngrowth[syngrowth$agedays < 20*365.25,][1:50,] only_adult <- syngrowth[syngrowth$agedays >= 20*365.25,][1:50,] + nobody <- syngrowth[syngrowth$agedays > 120*365.25,] # testing cleangrowth works without adult data peds_res <- cleangrowth( @@ -119,4 +120,15 @@ test_that("growthcleanr works without either adult or pediatric data", { expect_equal(length(adult_res), nrow(only_adult)) + # testing cleangrowth works with no data + no_res <- cleangrowth( + nobody$subjid, + nobody$param, + nobody$agedays, + nobody$sex, + nobody$measurement, + quietly = T + ) + + expect_equal(length(no_res), nrow(nobody)) }) \ No newline at end of file