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

Simplify dplyr_reconstruct() #5160

Merged
merged 3 commits into from
Apr 29, 2020

Conversation

DavisVaughan
Copy link
Member

@DavisVaughan DavisVaughan commented Apr 29, 2020

Closes #5158

This drastically simplifies dplyr_reconstruct.data.frame() by taking an approach similar to dplyr_col_modify.data.frame(), which just restores the attributes directly.

I've also pulled over the tests from #5158, but I think the attributes one might have been backwards. I think we care about keeping the attributes from template, not data.

The job of the data frame method of dplyr_reconstruct() is now very simple and well defined. It keeps all attributes of template except:

  • names in case columns were modified
  • row.names in case rows were modified

There was some code about the .drop attribute in the old dplyr_reconstruct.data.frame(), but I don't think it belonged there because we have a grouped_df method as well. It also wouldn't have worked right because the way to get to the drop attribute is through the group data of the data frame. It's not on the data frame directly.

@hadley
Copy link
Member

hadley commented Apr 29, 2020

I think this also fixes #5132?

@DavisVaughan
Copy link
Member Author

DavisVaughan commented Apr 29, 2020

That issue links to #4617, which has this example:

library(dplyr, warn.conflicts = FALSE)

df1 <- tibble(
  a = 1:2, 
  b = 2:1
)

df2 <- tibble(
  a = structure(1:2, foo = "bar"),
  c = 2:1
)

left_join(df1, df2, by = "a")$a
#> [1] 1 2
left_join(df2, df1, by = "a")$a
#> [1] 1 2
#> attr(,"foo")
#> [1] "bar"

That behavior isn't touched by this PR.

But honestly I sort of feel like this behavior is right. You get the attributes on the columns of the first input?

@DavisVaughan
Copy link
Member Author

DavisVaughan commented Apr 29, 2020

With dplyr_col_modify(), dplyr_row_slice(), and dplyr_reconstruct() already in place, I don't currently see any cases where we aren't preserving free floating attributes. I think we are doing the right thing now? Which implies that issue #5132 can be closed anyways.

Comment on lines -162 to +164
attr_new <- attributes(data)
attrs <- attributes(template)

to_copy <- setdiff(names(attr_old), c("class", "row.names", "names", ".drop"))
attr_new[to_copy] <- attr_old[to_copy]
attrs$names <- names(data)
attrs$row.names <- .row_names_info(data, type = 0L)
Copy link
Member

Choose a reason for hiding this comment

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

I think this would benefit from a lightweight C function that walks the pairlist, replaces in place and appends if necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that might be a useful helper for vctrs or similar. It doesn't need to block this PR.

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Thanks @DavisVaughan!

@DavisVaughan DavisVaughan merged commit 2b488e0 into tidyverse:master Apr 29, 2020
@DavisVaughan DavisVaughan deleted the generic-simplify branch April 29, 2020 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants