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

When using separate_wider_delim() function remove sf class and returns tibble #2359

Open
benjaminhlina opened this issue Mar 14, 2024 · 1 comment

Comments

@benjaminhlina
Copy link

When using separate_wider_delim() with a sf object, the function misbehaves and returns a tibble. If using separate() the class of the sf object is retained. The same issue was documented in issue tidyverse/tidyr#1495 but no reprex was provided. I had originally made the issue report on the {tidyr} repo, see tidyverse/tidyr#1498, but was recently informed that is likely due to an issues with {sf} not supporting these newer versions of separate_ functions. This is using v1.3.0 of {tidyr} and {sf} v1.0-15 and see reprex.

# load packages 
suppressPackageStartupMessages({
  library(crawl) # loaded for example sf object 
  library(sf) # load sf 
  library(tidyr) # load for separate functions  
})

# load harbour seal data 
data("harborSeal_sf")

# check out the sf object 
class(harborSeal_sf)
#> [1] "sf"         "data.frame"
# make up fake column to separate 

harborSeal_sf$fake <- "test_1"
# now use separate_wide_delim 

error <- harborSeal_sf |>
  separate_wider_delim(cols = fake, names = c("fake", "num"), delim = "_")
class(error)
#> [1] "tbl_df"     "tbl"        "data.frame"
# returns tibble not an sf object so it changes the object's class 

# if we use separate it keeps the objects class 
no_error <- harborSeal_sf |>
  separate(col = fake, into = c("fake", "num"), sep = "_")

class(no_error)
#> [1] "sf"         "data.frame"

Created on 2024-03-14 with reprex v2.1.0

@benjaminhlina benjaminhlina changed the title When using separate_wider_delim() function remove sf class and return tibble When using separate_wider_delim() function remove sf class and returns tibble Mar 14, 2024
@olivroy
Copy link
Contributor

olivroy commented Apr 4, 2024

Since the function uses tidyr::unpack() internally, we'd need to wait for tidyr to make pack()/ unpack() S3 methods and sf could provide its own method then. Not much sf can do without underlying S3 method.

Would have to wait for tidyverse/tidyr#1372 to be merged

Edit: there would still be work required in sf afterwards to define pack(<sf>) and unpack(<sf>)

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

2 participants