From 2679cf2c1fa4b37cbf1b99d11ee86846530b539d Mon Sep 17 00:00:00 2001 From: Dawid Kaledkowski Date: Thu, 22 Jan 2026 11:30:08 +0100 Subject: [PATCH 1/3] c.teal_slices - Ignore slices which have the same values --- NEWS.md | 4 ++++ R/teal_slices.R | 5 +++-- tests/testthat/test-teal_slices.R | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index f1787cd20..635d33d1e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # teal.slice 0.7.1.9003 +### Enhancements + +* Relaxed assertions on `teal_slices` in `c.teal_slices()` to ignore duplicated elements. + # teal.slice 0.7.1 ### Miscellaneous diff --git a/R/teal_slices.R b/R/teal_slices.R index 9b1bebfc8..89a6806f6 100644 --- a/R/teal_slices.R +++ b/R/teal_slices.R @@ -199,11 +199,12 @@ c.teal_slices <- function(...) { all_attributes <- lapply(x, attributes) all_attributes <- coalesce_r(all_attributes) all_attributes <- all_attributes[names(all_attributes) != "class"] - + slices <- unlist(x, recursive = FALSE) + hashes <- vapply(slices, function(x) rlang::hash(isolate(as.list(x))), character(1)) do.call( teal_slices, c( - unique(unlist(x, recursive = FALSE)), + slices[!duplicated(hashes)], all_attributes ) ) diff --git a/tests/testthat/test-teal_slices.R b/tests/testthat/test-teal_slices.R index 63ba6f9b9..4b4cb6fa0 100644 --- a/tests/testthat/test-teal_slices.R +++ b/tests/testthat/test-teal_slices.R @@ -266,6 +266,23 @@ testthat::test_that("c.teal_slices concatenates `teal_slices` objects", { testthat::expect_length(c(fss1, fss2), length(fss1) + length(fss2)) }) +testthat::test_that("c.teal_slices ignores duplicated slices", { + ts1 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") + ts2 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") + + testthat::expect_identical( + c(teal_slices(ts1), teal_slices(ts2)), + teal_slices(ts1) + ) +}) + +testthat::test_that("c.teal_slices throws when non-identical slices with the same id", { + ts1 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") + ts2 <- teal_slice(dataname = "ds1", varname = "var2", id = "slice1") + + testthat::expect_error(c(teal_slices(ts1), teal_slices(ts2)), "slice1") +}) + testthat::test_that("c.teal_slices coalesces attributes", { fs1 <- teal_slice("data1", "var1") fs2 <- teal_slice("data1", "var2") From 0098f382cb2db0c3b90889f2ef674b31cd98ef98 Mon Sep 17 00:00:00 2001 From: Dawid Kaledkowski Date: Fri, 23 Jan 2026 14:09:15 +0100 Subject: [PATCH 2/3] @llrs-roche review --- R/teal_slices.R | 7 +++--- tests/testthat/test-teal_slices.R | 36 +++++++++++++++---------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/R/teal_slices.R b/R/teal_slices.R index 89a6806f6..f527c4a76 100644 --- a/R/teal_slices.R +++ b/R/teal_slices.R @@ -94,10 +94,12 @@ teal_slices <- function(..., allow_add = TRUE) { slices <- list(...) checkmate::assert_list(slices, types = "teal_slice", any.missing = FALSE) + hashes <- vapply(slices, function(x) rlang::hash(isolate(as.list(x))), character(1)) + slices <- slices[!duplicated(hashes)] slices_id <- isolate(vapply(slices, `[[`, character(1L), "id")) if (any(duplicated(slices_id))) { stop( - "Some teal_slice objects have the same id:\n", + "Conflicting `teal_slice` objects id. Items have the same id but their settings differ:\n", toString(unique(slices_id[duplicated(slices_id)])) ) } @@ -200,11 +202,10 @@ c.teal_slices <- function(...) { all_attributes <- coalesce_r(all_attributes) all_attributes <- all_attributes[names(all_attributes) != "class"] slices <- unlist(x, recursive = FALSE) - hashes <- vapply(slices, function(x) rlang::hash(isolate(as.list(x))), character(1)) do.call( teal_slices, c( - slices[!duplicated(hashes)], + slices, all_attributes ) ) diff --git a/tests/testthat/test-teal_slices.R b/tests/testthat/test-teal_slices.R index 4b4cb6fa0..ac2e451c4 100644 --- a/tests/testthat/test-teal_slices.R +++ b/tests/testthat/test-teal_slices.R @@ -21,8 +21,6 @@ testthat::test_that("teal_slices checks arguments", { testthat::expect_error(teal_slices(fs1, fs2, count_type = c("all", "none"))) testthat::expect_error(teal_slices(fs1, fs2, allow_add = NULL), "Assertion on 'allow_add' failed") - - testthat::expect_error(teal_slices(fs1, fs1, fs2), "Some teal_slice objects have the same id") }) testthat::test_that("teal_slices returns `teal_slices`", { @@ -45,6 +43,24 @@ testthat::test_that("teal_slices returns `teal_slices`", { testthat::expect_length(teal_slices(fs1, fs2), 2L) }) +testthat::test_that("teal_slices accepts duplicated elements if they have identical values", { + ts1 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") + ts2 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") + + testthat::expect_no_error(teal_slices(ts1, ts2)) +}) + +testthat::test_that("teal_slices throw an error if elements with duplicated id have different settings", { + ts1 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") + ts2 <- teal_slice(dataname = "ds1", varname = "var2", id = "slice1") + + testthat::expect_error( + teal_slices(ts1, ts2), + "Conflicting `teal_slice` objects id. Items have the same id but their settings differ:\nslice1" + ) +}) + + testthat::test_that("teal_slices raises error when include_varnames and exclude_varnames specified same dataset", { testthat::expect_error( teal_slices( @@ -266,22 +282,6 @@ testthat::test_that("c.teal_slices concatenates `teal_slices` objects", { testthat::expect_length(c(fss1, fss2), length(fss1) + length(fss2)) }) -testthat::test_that("c.teal_slices ignores duplicated slices", { - ts1 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") - ts2 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") - - testthat::expect_identical( - c(teal_slices(ts1), teal_slices(ts2)), - teal_slices(ts1) - ) -}) - -testthat::test_that("c.teal_slices throws when non-identical slices with the same id", { - ts1 <- teal_slice(dataname = "ds1", varname = "var1", id = "slice1") - ts2 <- teal_slice(dataname = "ds1", varname = "var2", id = "slice1") - - testthat::expect_error(c(teal_slices(ts1), teal_slices(ts2)), "slice1") -}) testthat::test_that("c.teal_slices coalesces attributes", { fs1 <- teal_slice("data1", "var1") From d9a468f404e4780bec04c0e3bc6b5ec55d5ef09a Mon Sep 17 00:00:00 2001 From: Dawid Kaledkowski Date: Fri, 23 Jan 2026 14:10:36 +0100 Subject: [PATCH 3/3] change NEWS --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 635d33d1e..a2a68e80f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ ### Enhancements -* Relaxed assertions on `teal_slices` in `c.teal_slices()` to ignore duplicated elements. +* Relaxed assertions on `teal_slices` to ignore duplicated `teal_slice` elements. # teal.slice 0.7.1