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

Use vctrs::vec_restore to preserve data frame subclass and attributes #3429

Closed
krlmlr opened this issue Mar 15, 2018 · 5 comments
Closed

Use vctrs::vec_restore to preserve data frame subclass and attributes #3429

krlmlr opened this issue Mar 15, 2018 · 5 comments
Labels
breaking change ☠️ API change likely to affect existing code feature a feature request or enhancement verbs 🏃‍♀️
Milestone

Comments

@krlmlr
Copy link
Member

krlmlr commented Mar 15, 2018

to get rid of redundant tbl_df methods which complicate navigating the code. When sloop is ready, we'll be ready to switch.

@markvanderloo
Copy link

It would be great if dplyr (and other tidyverse) verbs can keep user-defined attributes. Obviously, (as @hadley noted in #3558) you can't keep row.names in a filter. Let me explain one use case.

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 tidyverse is to be a type-safe zone within R. Keeping user-added attributes does not make the output of tidyverse verbs inconsistent with the tidyverse, except in case of name collisions but those can be handled e.g. with a warning or message. Being consistent with the tidyverse is the responsibilty of package authors on the fringe of it (like me).

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.

@romainfrancois
Copy link
Member

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")
})

@markvanderloo
Copy link

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 dplyr verbs, but I understand something is in te works. @romainfrancois: cheers for the (live) discussions 👍

@hadley hadley changed the title Implement and use reconstruct() internally Use vctrs::vec_restore to preserve data frame subclass and attributes Oct 8, 2018
@hadley
Copy link
Member

hadley commented Oct 8, 2018

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.

@hadley
Copy link
Member

hadley commented Dec 10, 2019

Now tracking in r-lib/vctrs#703

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change ☠️ API change likely to affect existing code feature a feature request or enhancement verbs 🏃‍♀️
Projects
None yet
Development

No branches or pull requests

4 participants