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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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)
}

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)

expect_equal(class(x), class(y))
})
Loading