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

dplyr verbs removing additional classes #1429

Closed
datalove opened this issue Sep 30, 2015 · 4 comments
Closed

dplyr verbs removing additional classes #1429

datalove opened this issue Sep 30, 2015 · 4 comments
Assignees
Labels
feature a feature request or enhancement
Milestone

Comments

@datalove
Copy link

I'm building a package to work with objects that are a special case of a tbl_df in which there is 'interval' information in the columns (i.e. one column for start of interval and one column for end of interval).

I have a constructor that adds a class to an existing data.frame or tbl_df (and some attributes too). But it seems that dplyr verbs like to remove any classes that I add.

For example:

add_class <- function(x,name) {class(x) <- c(class(x),name); x}
df <- mtcars %>% add_class('something_new')
df %>% class()
[1] "data.frame"    "something_new"

But dplyr verbs strip the new class...

df %>% arrange(mpg) %>% class()
[1] "data.frame"
> df %>% filter(mpg > 30) %>% class()
[1] "data.frame"
> df %>% tbl_df() %>% class()
[1] "tbl_df"     "tbl"        "data.frame"

I notice that dplyr 0.4.3 now preserves attributes, but it looks like classes are still getting eaten.

Is this something that's on anyone's radar? I haven't been able to find an issue for it.

@hadley hadley added feature a feature request or enhancement data frame labels Oct 21, 2015
@hadley hadley added this to the 0.5 milestone Oct 21, 2015
@hadley
Copy link
Member

hadley commented Oct 21, 2015

Did we do this deliberately? I think we should follow the same logic as preserving attributes of columns.

@eibanez
Copy link
Contributor

eibanez commented Oct 22, 2015

Related to #553 and #936, which i don't think were addressed

@hadley
Copy link
Member

hadley commented Oct 29, 2015

Your class is incorrect - it should be c("something_else", "data.frame").

And I think doing this correctly is up to you - you need to define a method for your class for each dplyr method that correctly restores all the classes and attributes. It's going to be extremely fiddly to do this in dplyr, and it doesn't seem like a huge benefit to me given that there's already a work around (which is arguably the correct thing to do anyway.

@hadley hadley closed this as completed Oct 29, 2015
@datalove
Copy link
Author

Thanks Hadley. That's how I've managed to get it to work so far, but was hoping there'd be an easier way.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants