Skip to content

Commit

Permalink
Warn of duplicate names
Browse files Browse the repository at this point in the history
Close #48
  • Loading branch information
llrs committed Mar 27, 2024
1 parent 817cf57 commit 736e1cd
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 106 deletions.
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
212 changes: 106 additions & 106 deletions tests/testthat/test-follow_up.R
Original file line number Diff line number Diff line change
@@ -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)
})
13 changes: 13 additions & 0 deletions tests/testthat/test-inspect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})

0 comments on commit 736e1cd

Please sign in to comment.