diff --git a/R/utils.R b/R/utils.R index f225ac1..4d80292 100644 --- a/R/utils.R +++ b/R/utils.R @@ -141,6 +141,10 @@ evaluations <- function(num, eval_cat = 4, eval_num = 3) { add_column <- function(x, values, name) { # Add the column and rename it + if (name %in% colnames(x)) { + msg <- paste("Column", name, "is already present. Did you meant this?") + warning(msg, call. = FALSE) + } out <- cbind(x, values) colnames(out)[ncol(out)] <- name rownames(out) <- NULL diff --git a/tests/testthat/test-follow_up.R b/tests/testthat/test-follow_up.R index e0c3371..3ce6085 100644 --- a/tests/testthat/test-follow_up.R +++ b/tests/testthat/test-follow_up.R @@ -1,106 +1,106 @@ -test_that("follow_up works", { - data(survey, package = "MASS") - survey1 <- survey[1:118, ] - survey2 <- survey[119:nrow(survey), ] - expect_warning( - expect_warning( - expect_warning( - expect_warning( - fu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10) - ) - ) - ) - ) - expect_s3_class(fu, "data.frame") -}) - -test_that("follow_up2 works", { - data(survey, package = "MASS") - expect_error(follow_up2(survey, iterations = 10)) - - old_n <- 118 - # old vs new - survey$batch <- c(rep("old", old_n), rep(NA, nrow(survey) - old_n)) - expect_warning( - expect_warning( - expect_warning( - expect_warning( - fu1 <- follow_up2(survey, iterations = 10), - "with the data."), - "with the new samples"), - "some problems with the new data."), - "some problems with the old data.") - expect_type(fu1, "character") - # old vs new with confounding effects - survey$batch <- ifelse(survey$Clap %in% "Right", "old", NA) - expect_error( - expect_warning( - expect_warning( - expect_warning( - expect_warning( - follow_up2(survey, iterations = 10) - ) - ) - ) - ) - ) - - # old with batches vs new - bn <- batch_names(create_subset(old_n, 20)) - survey$batch <- c(bn, rep(NA, nrow(survey) - old_n)) - expect_warning( - expect_warning( - expect_warning( - expect_warning( - fu3 <- follow_up2(survey, iterations = 10) - ) - ) - ) - ) - expect_type(fu3, "character") - # old with batches and confounding effect vs new - survey$batch <- ifelse(survey$Clap %in% "Right", "old", NA) - bn <- batch_names(create_subset(sum(survey$Clap %in% "Right", na.rm = TRUE), 20)) - survey$batch[survey$batch %in% "old"] <- bn - expect_warning( - expect_warning( - expect_warning( - expect_warning( - fu4 <- follow_up2(survey, iterations = 10) - ) - ) - ) - ) - expect_type(fu4, "character") -}) - - -test_that("valid_followup works", { - data(survey, package = "MASS") - survey1 <- survey[1:118, ] - survey2 <- survey[119:nrow(survey), ] - - expect_warning( - expect_warning( - expect_warning( - expect_warning( - out <- valid_followup(survey1, survey2) - ) - ) - ) - ) - expect_false(out) - survey$batch <- NA - survey$batch[1:118] <- "old" - - expect_warning( - expect_warning( - expect_warning( - expect_warning( - out <- valid_followup(all_data = survey) - ) - ) - ) - ) - expect_false(out) -}) +test_that("follow_up works", { + data(survey, package = "MASS") + survey1 <- survey[1:118, ] + survey2 <- survey[119:nrow(survey), ] + expect_warning( + expect_warning( + expect_warning( + expect_warning( + fu <- follow_up(survey1, survey2, size_subset = 50, iterations = 10, old_new = "batch2") + ) + ) + ) + ) + expect_s3_class(fu, "data.frame") +}) + +test_that("follow_up2 works", { + data(survey, package = "MASS") + expect_error(follow_up2(survey, iterations = 10)) + + old_n <- 118 + # old vs new + survey$batch <- c(rep("old", old_n), rep(NA, nrow(survey) - old_n)) + expect_warning( + expect_warning( + expect_warning( + expect_warning( + fu1 <- follow_up2(survey, iterations = 10), + "with the data."), + "with the new samples"), + "some problems with the new data."), + "some problems with the old data.") + expect_type(fu1, "character") + # old vs new with confounding effects + survey$batch <- ifelse(survey$Clap %in% "Right", "old", NA) + expect_error( + expect_warning( + expect_warning( + expect_warning( + expect_warning( + follow_up2(survey, iterations = 10) + ) + ) + ) + ) + ) + + # old with batches vs new + bn <- batch_names(create_subset(old_n, 20)) + survey$batch <- c(bn, rep(NA, nrow(survey) - old_n)) + expect_warning( + expect_warning( + expect_warning( + expect_warning( + fu3 <- follow_up2(survey, iterations = 10) + ) + ) + ) + ) + expect_type(fu3, "character") + # old with batches and confounding effect vs new + survey$batch <- ifelse(survey$Clap %in% "Right", "old", NA) + bn <- batch_names(create_subset(sum(survey$Clap %in% "Right", na.rm = TRUE), 20)) + survey$batch[survey$batch %in% "old"] <- bn + expect_warning( + expect_warning( + expect_warning( + expect_warning( + fu4 <- follow_up2(survey, iterations = 10) + ) + ) + ) + ) + expect_type(fu4, "character") +}) + + +test_that("valid_followup works", { + data(survey, package = "MASS") + survey1 <- survey[1:118, ] + survey2 <- survey[119:nrow(survey), ] + + expect_warning( + expect_warning( + expect_warning( + expect_warning( + out <- valid_followup(survey1, survey2) + ) + ) + ) + ) + expect_false(out) + survey$batch <- NA + survey$batch[1:118] <- "old" + + expect_warning( + expect_warning( + expect_warning( + expect_warning( + out <- valid_followup(all_data = survey) + ) + ) + ) + ) + expect_false(out) +}) diff --git a/tests/testthat/test-inspect.R b/tests/testthat/test-inspect.R index 50855a2..f8c32a3 100644 --- a/tests/testthat/test-inspect.R +++ b/tests/testthat/test-inspect.R @@ -40,3 +40,16 @@ test_that("inspect with translate_index", { i2 <- inspect(index2, i1, index_name = "spatial") expect_true(all(table(i2$batch, i2$spatial)<= 1)) }) + + test_that("Warning on duplidate names", { + data(survey, package = "MASS") + columns <- c("Sex", "Age", "Smoke") + expect_warning(index <- design(pheno = survey[, columns], size_subset = 70, + iterations = 10)) + batches <- inspect(index, survey[, columns]) + + expect_warning(index2 <- design(pheno = batches, size_subset = 70, + iterations = 10)) + expect_warning(inspect(index2, batches)) + expect_no_warning(inspect(index2, batches, index_name = "batch2")) +}) \ No newline at end of file