From 348dbc30d36601fba59ecc8f9e4c0ca7cc345083 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 8 May 2026 19:04:25 +0200 Subject: [PATCH] Sync shapes when dropping elements from table --- R/methods.R | 2 ++ R/utils.R | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/R/methods.R b/R/methods.R index 3b0832b0..6522f75b 100644 --- a/R/methods.R +++ b/R/methods.R @@ -272,6 +272,8 @@ f <- \(.) setReplaceMethod(., y <- attr(x, paste0(., "s")) y[[i]] <- value attr(x, paste0(., "s")) <- y + if (. == "table") + x <- .sync_shapes_on_drop(x) return(x) }) for (. in one) eval(f(.), parent.env(environment())) diff --git a/R/utils.R b/R/utils.R index b3de2fbc..cf84ffe9 100644 --- a/R/utils.R +++ b/R/utils.R @@ -72,3 +72,17 @@ tables(x) <- ts return(x) } + +.sync_shapes_on_drop <- \(x) { + if (!length(shapes(x))) return(x) + all_nms <- instances(table(x)) + for (i in seq_along(shapes(x))) { + s <- shapes(x)[[i]] + # check which cells still exist + # FIXME: I kind of doubt this always has this name. Do we have an accessor for this??? + keep <- s[["__index_level_0__"]] %in% all_nms + s <- s[keep, , drop = FALSE] + shapes(x)[[i]] <- s + } + return(x) +} \ No newline at end of file