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

list_rbind() loses spatial class #1076

Closed
francisbarton opened this issue May 2, 2023 · 4 comments
Closed

list_rbind() loses spatial class #1076

francisbarton opened this issue May 2, 2023 · 4 comments

Comments

@francisbarton
Copy link

When binding a list of spatial data frames, the sf class is lost from the result.
(Perhaps this is where the ptype argument to list_rbind() is useful, but I don't know what I would need to use there.)

This feels like a loss of functionality, or at least an unexpected result, compared to using dplyr::bind_rows(), or a workflow using map_dfr that I might have used previously.

I'm sure you'll say this is behaving as desired, not a bug, but as a user of purrr this felt like an unexpected behaviour.

nc <- sf::st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)

test_list <- list(ashe = dplyr::slice(nc, 1), surry = dplyr::slice(nc, 3))

purrr::list_rbind(test_list) |>
  class()
#> [1] "data.frame"

dplyr::bind_rows(test_list) |>
  class()
#> [1] "sf"         "data.frame"

Created on 2023-05-02 with reprex v2.0.2

@francisbarton francisbarton changed the title list_rbind() loses spatial attributes list_rbind() loses spatial class May 2, 2023
@francisbarton
Copy link
Author

francisbarton commented May 2, 2023

Seems more odd, since a few years ago when dplyr::bind_rows() started handling binding sf data frames - which was great - this was based on bind_rows() starting to use vctrs::vec_rbind() - which I believe is also the basis for list_rbind()
tidyverse/dplyr#2457 (comment)

@batpigandme
Copy link
Contributor

From taking a peek at the code, it looks like dplyr::bind_rows() actually overrides vctrs coercion rules and derives the class from the first data frame in:
https://github.com/tidyverse/dplyr/blob/1832ffbbdf3a85145b1545b84ee7b55a99fbae98/R/bind-rows.R#L77-L84

  # Override vctrs coercion rules and instead derive class from first input
  if (is.data.frame(first)) {
    out <- dplyr_reconstruct(out, first)
  } else {
    out <- as_tibble(out)
  }
  out
}

@hadley
Copy link
Member

hadley commented Jul 26, 2023

Tracking in related vctrs issue.

@hadley hadley closed this as completed Jul 26, 2023
@francisbarton
Copy link
Author

Maybe this one r-lib/vctrs#1741

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

No branches or pull requests

3 participants