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

Column classes don't survive dplyr::bind_rows (or joins?) #13

Open
jimjam-slam opened this issue Jul 9, 2019 · 1 comment
Open

Column classes don't survive dplyr::bind_rows (or joins?) #13

jimjam-slam opened this issue Jul 9, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@jimjam-slam
Copy link
Owner

jimjam-slam commented Jul 9, 2019

Some workflows I'm testing that involve using dplyr::bind_rows() on data frames that have collateral columns cause them to lose their formatting:

library(tidyverse)
library(collateral)

test =
  # tidy up and trim down for the example
  mtcars %>%
  rownames_to_column(var = "car") %>%
  as_tibble() %>%
  select(car, cyl, disp, wt) %>%
  # spike some rows in cyl == 4 to make them fail
  mutate(wt = dplyr::case_when(
    wt < 2 ~ -wt,
    TRUE ~ wt)) %>%
  # nest and do some operations quietly()
  nest(-cyl) %>%
  mutate(qlog = map_quietly(data, ~ log(.$wt)))

# now slice them up...
test1 = test %>% slice(1)
test2 = test %>% slice(2)
test3 = test %>% slice(3)

# ... and combine them. problem!
test_rejoined = bind_rows(test1, test2, test3)
#> Warning in bind_rows_(x, .id): Vectorizing 'quietly_mapped' elements may
#> not preserve their attributes

#> Warning in bind_rows_(x, .id): Vectorizing 'quietly_mapped' elements may
#> not preserve their attributes

#> Warning in bind_rows_(x, .id): Vectorizing 'quietly_mapped' elements may
#> not preserve their attributes
test_rejoined
#> # A tibble: 3 x 3
#>     cyl data              qlog      
#>   <dbl> <list>            <list>    
#> 1     6 <tibble [7 x 3]>  <list [4]>
#> 2     4 <tibble [11 x 3]> <list [4]>
#> 3     8 <tibble [14 x 3]> <list [4]>
class(test_rejoined$qlog)
#> [1] "list"

Created on 2019-07-09 by the reprex package (v0.3.0)

This took me by surprise a little, as my understanding is that classes are supposed to be exempt from attributes getting wiped when a vector is modified. So I'm not 100% sure what's happening, but it may be that I need to handle collateral lists getting concatenated or subsetted a little more explicitly.

R/format.r has some extra S3 methods that I mostly added because I was trying to follow along with the pillar documentation, but I wonder if—for example—these two might deserve some more attention:

#' @rdname collateral_extras
#' @export
c.safely_mapped <- function(x, ...) {
  as_safely_mapped(NextMethod())
}
#' @rdname collateral_extras
#' @export
`[.safely_mapped` <- function(x, i) {
  as_safely_mapped(NextMethod())
}

Originally posted by @Rensa in #9 (comment)

@jimjam-slam
Copy link
Owner Author

This may also be related to tidyverse/dplyr#2457. I'm not sure if simply implementing c and [ properly would fix the problem, though.

@jimjam-slam jimjam-slam added the bug Something isn't working label Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant