-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Use vctrs::vec_restore to preserve data frame subclass and attributes #3429
Comments
It would be great if In my lumberjack package, I define a new pipe operator that can track changes in data in a custom way. The pseudocode is like this: `%>>%` <- function(x,f){
old <- x
new <- f(x)
if ( x has a logger attribute ) attr(x, "logger")(old, new)
return(new)
} I have other use cases as well. What I'm saying is that attributes are not necessarily row-wise or column wise. I understand that the whole concept of the At the moment, you are creating an 'inconsistent' object, in interaction with at least one package outside of the tidyverse (mine). I don't think that's what you'd want either. |
Also relevant: #1064. Not sure why we keep attributes for some cases. test_that("filter, slice and arrange preserves attributes (#1064)", {
df <- structure(
data.frame(x = 1:10, g1 = rep(1:2, each = 5), g2 = rep(1:5, 2)),
meta = "this is important"
)
res <- filter(df, x < 5) %>% attr("meta")
expect_equal(res, "this is important")
res <- filter(df, x < 5, x > 4) %>% attr("meta")
expect_equal(res, "this is important")
res <- df %>% slice(1:50) %>% attr("meta")
expect_equal(res, "this is important")
res <- df %>% arrange(x) %>% attr("meta")
expect_equal(res, "this is important")
res <- df %>% summarise(n()) %>% attr("meta")
expect_equal(res, "this is important")
res <- df %>% group_by(g1) %>% summarise(n()) %>% attr("meta")
expect_equal(res, "this is important")
res <- df %>% group_by(g1, g2) %>% summarise(n()) %>% attr("meta")
expect_equal(res, "this is important")
}) |
I just figured out, while walking to the eRum conference venue, that in my use case, I can just re-add the attribute I need in the %L>% operator :-). Anyway, it would still be useful to be able to somehow keep attributes when passing data through |
See discussion of problem, and solution, in https://adv-r.hadley.nz/s3.html#allowing-subclassing, and examples at https://vctrs.r-lib.org/articles/s3-vector.html. |
Now tracking in r-lib/vctrs#703 |
to get rid of redundant
tbl_df
methods which complicate navigating the code. When sloop is ready, we'll be ready to switch.The text was updated successfully, but these errors were encountered: