diff --git a/R/cov_search.R b/R/cov_search.R index c3ec481..9c24d21 100644 --- a/R/cov_search.R +++ b/R/cov_search.R @@ -13,6 +13,12 @@ #' MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed = 123) { + # Check that covariates supplied by user exist in the data + errors <- data_validation(tab, list_pop_param, cov_continuous, cov_factors) + if (length(errors) > 0) { + stop(paste0(errors, sep = "\n"), call. = FALSE) + } + stopifnot(is.numeric(seed)) set.seed(seed) @@ -247,7 +253,25 @@ MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed = ) } - +data_validation <- function(tab, list_pop_param, cov_continuous, cov_factors) { + errors <- c() + + for (i in 1:3) { + vectors <- list(list_pop_param, cov_continuous, cov_factors) + vector_names <- c("list_pop_param", "cov_continuous", "cov_factors") + + missing_values <- setdiff(vectors[[i]], colnames(tab)) + + if (length(missing_values) > 0) { + error_message <- + paste( "The following values from", vector_names[[i]], "are missing in the dataset:", toString(missing_values)) + + errors <- c(errors, error_message) + } + } + return(errors) + +} @@ -282,6 +306,13 @@ MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed = #' } #' @export generate_residualsplots <- function(tab, list_pop_param, cov_continuous, cov_factors, result_ML, result_5folds, i, seed = 123 ) { + + # Check that covariates supplied by user exist in the data + errors <- data_validation(tab, list_pop_param, cov_continuous, cov_factors) + if (length(errors) > 0) { + stop(paste0(errors, sep = "\n"), call. = FALSE) + } + stopifnot(is.numeric(seed)) set.seed(seed) diff --git a/R/generate_residualsplots2.R b/R/generate_residualsplots2.R index db6ae08..e6fdd90 100644 --- a/R/generate_residualsplots2.R +++ b/R/generate_residualsplots2.R @@ -36,6 +36,12 @@ generate_residualsplots2 <- function(data, result, i, seed = NULL) { result_ML <- result$result_ML result_5folds <- result$result_5folds + # Check that covariates supplied by user exist in the data + errors <- data_validation(data, list_pop_param, cov_continuous, cov_factors) + if (length(errors) > 0) { + stop(paste0(errors, sep = "\n"), call. = FALSE) + } + # Selection of columns required data <- data %>% dplyr::select(ID, dplyr::all_of(list_pop_param), dplyr::all_of(cov_continuous), dplyr::all_of(cov_factors)) diff --git a/tests/testthat/test-generate_residualsplots.R b/tests/testthat/test-generate_residualsplots.R index e98b6a8..624226a 100644 --- a/tests/testthat/test-generate_residualsplots.R +++ b/tests/testthat/test-generate_residualsplots.R @@ -64,3 +64,14 @@ testthat::test_that("test-generate_residualsplots returns the same plots each ti vdiffr::expect_doppelganger("SEX Residuals", plot_V1$SEX) }) +testthat::test_that("Error messages will be generated when values supplied to the function are absent in the data.frame", { + + testthat::expect_error(generate_residualsplots(tab = data, + list_pop_param = c("clearance"), + cov_continuous = c("weight"), + cov_factors = c("dIaB"), + result_ML = result$result_ML, #selected covariates for parameter of interest after the voting + result_5folds = result$result_5folds, #selected covariates for parameter of interest for each folds + i=c('CL'))) +}) + diff --git a/tests/testthat/test-generateresidualsplots2.R b/tests/testthat/test-generateresidualsplots2.R index a335d17..477947f 100644 --- a/tests/testthat/test-generateresidualsplots2.R +++ b/tests/testthat/test-generateresidualsplots2.R @@ -42,3 +42,9 @@ testthat::test_that("generate_residualsplots does not return plots for Cl", { testthat::test_that("generate_residualsplots does not return plots for V1", { testthat::expect_null(plot_V1) }) + +testthat::test_that("Error messages will be generated when values supplied to the function are absent in the data.frame", { + + #Since residualsplots2 function pulls these values from the results object, it should always run correctly. + testthat::expect_no_error(generate_residualsplots2(data, result, i = c('V1'))) +}) diff --git a/tests/testthat/test-mlcovsearch.R b/tests/testthat/test-mlcovsearch.R index 5cd75e4..069230e 100644 --- a/tests/testthat/test-mlcovsearch.R +++ b/tests/testthat/test-mlcovsearch.R @@ -25,6 +25,14 @@ result <- suppressWarnings(MLCovSearch(data, #NONMEM output "HGB","HCT","PLT"), cov_factors = c("SEX","RACE","DIAB","ALQ","WACT","SMQ"))) +testthat::test_that("Error messages will be generated when values supplied to the MLCovSearch are absent in the data.frame", { + + testthat::expect_error(MLCovSearch(data, #NONMEM output + list_pop_param = c("clearance"), + cov_continuous = c("weight"), + cov_factors = c("dIaB"))) +}) + testthat::test_that("MLCovSearch result_ML is a data.frame with only one covariate selection", { testthat::expect_true(is.data.frame(result$result_ML))