Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain class inheritance #62

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/restore_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +62 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As shown in the proposed new test, this will come short in complex cases where the safeframe is a subclass of a data.frame (e.g., a tibble).

I think the only solution is to restore the class in the function where it's dropped (i.e., the subsetting functions) and to restore a copy of the original class

}

x
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lifecycle
linelist
messspeeds
rlang
struct
tibble
tidyselect
tidyverse
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-restore_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
chartgerink marked this conversation as resolved.
Show resolved Hide resolved

expect_equal(class(x), class(y))
chartgerink marked this conversation as resolved.
Show resolved Hide resolved
})
Loading