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

Using st_as_sf on a data.table produces broken object #2151

Open
lpiep opened this issue Apr 25, 2023 · 1 comment
Open

Using st_as_sf on a data.table produces broken object #2151

lpiep opened this issue Apr 25, 2023 · 1 comment

Comments

@lpiep
Copy link

lpiep commented Apr 25, 2023

Describe the bug

st_as_sf used on a data.table creates an object with some broken functionality. While I wouldn't necessarily expect to be able to use data.table specific functions on the result, base functions appear to remain overridden by their data.table equivalent and are not functional until the user manually removes the data.table class. I'll provide an example with split.

To Reproduce

library(sf)
library(data.table)

# Make a data.table (some text borrowed from KeesDoolNMI in issue 1749)
df <- data.table(geom = rep("POLYGON ((6.215807 52.60666, 6.215807 52.60651, 6.215394 52.60651, 6.215394 52.60666, 6.215807 52.60666))", 10), group = rep(1:2, 5))

# Now set as sf data.frame
df <- st_as_sf(df, wkt = "geom")

# df has both data.table and sf classes
class(df)

#> [1] "sf"         "data.table" "data.frame"

# This fails
df_split <- split(df, df$group)

#> Error in `[.data.frame`(x, i) : undefined columns selected

# But after casting back to data.frame, it works
data.table::setDF(df)
df_split <- split(df, df$group)

If reporting a change from previous versions

✔️ Please read https://cran.r-project.org/web/packages/sf/news/news.html first.

Paste the output of your `sessionInfo()` and `sf::sf_extSoftVersion()`:
> sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.14.8 sf_1.0-9         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10        rstudioapi_0.14    magrittr_2.0.3     units_0.8-1        tidyselect_1.2.0   R6_2.5.1          
 [7] rlang_1.0.6        fansi_1.0.4        dplyr_1.1.0        tools_4.2.2        grid_4.2.2         KernSmooth_2.23-20
[13] utf8_1.2.3         cli_3.6.0          e1071_1.7-13       DBI_1.1.3          class_7.3-20       tibble_3.1.8      
[19] lifecycle_1.0.3    vctrs_0.5.2        glue_1.6.2         proxy_0.4-27       compiler_4.2.2     pillar_1.8.1      
[25] generics_0.1.3     classInt_0.4-9     pkgconfig_2.0.3   


> sf::sf_extSoftVersion()
          GEOS           GDAL         proj.4 GDAL_with_GEOS     USE_PROJ_H           PROJ 
       "3.8.0"        "3.0.4"        "6.3.1"         "true"         "true"        "6.3.1" 
@edzer
Copy link
Member

edzer commented Apr 26, 2023

Not unlikely that this is related to #2131

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