From f17b295a70fd5abccece4d9db8f342cd1e41b7a8 Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Mon, 6 Jan 2025 11:33:36 +0100 Subject: [PATCH 1/3] Update class inheritance upon `restore_labels` --- R/restore_labels.R | 4 +++- tests/testthat/test-restore_labels.R | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/R/restore_labels.R b/R/restore_labels.R index 09022dc..c85b237 100644 --- a/R/restore_labels.R +++ b/R/restore_labels.R @@ -59,7 +59,9 @@ restore_labels <- function(x, newLabels, # Ensure class consistency if (!inherits(x, "safeframe")) { - class(x) <- c("safeframe", class(x)) + current_classes <- class(x) + df_index <- which(current_classes == "data.frame") + class(x) <- append(current_classes, "safeframe", after = df_index - 1) } x diff --git a/tests/testthat/test-restore_labels.R b/tests/testthat/test-restore_labels.R index b81daba..d5e2711 100644 --- a/tests/testthat/test-restore_labels.R +++ b/tests/testthat/test-restore_labels.R @@ -26,3 +26,21 @@ test_that("tests for restore_labels", { x$speed <- "test3" expect_equal(class(x), c("safeframe", "data.frame")) }) + +test_that("retain class inheritance #56", { + + x <- make_safeframe( + cars, + speed = "Miles per hour" + ) + class(x) <- c("linelist", class(x)) + class(x) + #> [1] "linelist" "safeframe" "data.frame" + + y <- suppressWarnings(x[, 1]) + #> Warning: The following labelled variables are lost: + #> dist - NULL + class(y) + + expect_equal(class(x), class(y)) +}) From 6c50d74c381f6e7ebf1264c88b60a067a674558c Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Mon, 6 Jan 2025 12:45:44 +0100 Subject: [PATCH 2/3] Update WORDLIST --- inst/WORDLIST | 1 + 1 file changed, 1 insertion(+) diff --git a/inst/WORDLIST b/inst/WORDLIST index cfbfcd0..fd1e877 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -11,6 +11,7 @@ lifecycle linelist messspeeds rlang +struct tibble tidyselect tidyverse From 0fe06e07f1d4188059670b38c5cf76d04b09e74f Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Wed, 12 Feb 2025 11:23:13 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> --- tests/testthat/test-restore_labels.R | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-restore_labels.R b/tests/testthat/test-restore_labels.R index d5e2711..7b2026c 100644 --- a/tests/testthat/test-restore_labels.R +++ b/tests/testthat/test-restore_labels.R @@ -34,13 +34,18 @@ test_that("retain class inheritance #56", { speed = "Miles per hour" ) class(x) <- c("linelist", class(x)) - class(x) - #> [1] "linelist" "safeframe" "data.frame" - y <- suppressWarnings(x[, 1]) - #> Warning: The following labelled variables are lost: - #> dist - NULL - class(y) expect_equal(class(x), class(y)) + + # For more complex class inheritance structure, such as a linelist tibble + x_tbl <- make_safeframe( + tibble::as_tibble(cars), + speed = "Miles per hour" + ) + class(x_tbl) <- c("linelist", class(x_tbl)) + + y_tbl <- suppressWarnings(x_tbl[, 1]) + + expect_equal(class(x_tbl), class(y_tbl)) })