Hi all! I'm struggling to figure out the best way (or a way that... works) to convert a geojson column of a data frame to a sf geometry list column, then to convert that into a tibble and store it as data (that prints with tibble printing) within a package. A lot of layers, I know!
I have a small example package on github of what I've attempted and isn't working (mostly in the data-raw folder).
If I convert the column using sf::st_as_sfc()
and keep it as a data.frame as an object in the package, all is good (code is in df_sf.R)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
df_sf <- data.frame(
a = 1,
b = 2,
geometry = "{\"type\": \"Polygon\", \"coordinates\": [[[-79.4359157087306, 43.6801533947749], [-79.4359157087306, 43.6801533947749]]]}"
)
df_sf
#> a b
#> 1 1 2
#> geometry
#> 1 {"type": "Polygon", "coordinates": [[[-79.4359157087306, 43.6801533947749], [-79.4359157087306, 43.6801533947749]]]}
df_sf[["geometry"]] <- st_as_sfc(df_sf[["geometry"]], GeoJSON = TRUE, crs = 4326)
df_sf
#> a b geometry
#> 1 1 2 POLYGON ((-79.43592 43.6801...
# Save as an object in package
usethis::use_data(df_sf, overwrite = TRUE)
And I can access it just fine:
geojsontosftest::df_sf
#> a b geometry
#> 1 1 2 -79.43592, -79.43592, 43.68015, 43.68015
But, if I convert it and then convert to a tibble, it seems like things are fine. It prints as a tibble and all looks good. I also have tibble printing enabled, from using usethis::use_tibble()
, and documented that it returns a tibble. Code is in df_sf_tibble.R.
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(tibble)
df_sf <- data.frame(
a = 1,
b = 2,
geometry = "{\"type\": \"Polygon\", \"coordinates\": [[[-79.4359157087306, 43.6801533947749], [-79.4359157087306, 43.6801533947749]]]}"
)
df_sf[["geometry"]] <- st_as_sfc(df_sf[["geometry"]], GeoJSON = TRUE, crs = 4326)
# First convert to tibble, then save as object in package
df_sf_tibble <- as_tibble(df_sf)
df_sf_tibble
#> # A tibble: 1 x 3
#> a b geometry
#> <dbl> <dbl> <POLYGON [Β°]>
#> 1 1 2 ((-79.43592 43.68015, -79.43592 43.68015))
usethis::use_data(df_sf_tibble, overwrite = TRUE)
But when I try to access it from the package, I get an error:
geojsontosftest::df_sf_tibble
#> Error: Input must be a vector, not a `sfc_POLYGON/sfc` object.
Here is my session info and package versions:
Session info
devtools::session_info()
#> β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> setting value
#> version R version 3.6.1 (2019-07-05)
#> os macOS Sierra 10.12.1
#> system x86_64, darwin15.6.0
#> ui X11
#> language (EN)
#> collate en_CA.UTF-8
#> ctype en_CA.UTF-8
#> tz America/Toronto
#> date 2020-02-29
#>
#> β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> package * version date lib
#> assertthat 0.2.1 2019-03-21 [1]
#> backports 1.1.5 2019-10-02 [1]
#> callr 3.3.2 2019-09-22 [1]
#> cli 2.0.1 2020-01-08 [1]
#> crayon 1.3.4 2017-09-16 [1]
#> desc 1.2.0 2018-05-01 [1]
#> devtools 2.2.1 2019-09-24 [1]
#> digest 0.6.22 2019-10-21 [1]
#> ellipsis 0.3.0 2019-09-20 [1]
#> evaluate 0.14 2019-05-28 [1]
#> fansi 0.4.0 2018-10-05 [1]
#> fs 1.3.1 2019-05-06 [1]
#> geojsontosftest 0.0.0.9000 2020-02-29 [1]
#> glue 1.3.1 2019-03-12 [1]
#> highr 0.8 2019-03-20 [1]
#> htmltools 0.4.0 2019-10-04 [1]
#> knitr 1.28 2020-02-06 [1]
#> magrittr 1.5 2014-11-22 [1]
#> memoise 1.1.0 2017-04-21 [1]
#> pillar 1.4.3.9000 2020-02-08 [1]
#> pkgbuild 1.0.6 2019-10-09 [1]
#> pkgconfig 2.0.3 2019-09-22 [1]
#> pkgload 1.0.2 2018-10-29 [1]
#> prettyunits 1.0.2 2015-07-13 [1]
#> processx 3.4.1 2019-07-18 [1]
#> ps 1.3.0 2018-12-21 [1]
#> R6 2.4.0 2019-02-14 [1]
#> Rcpp 1.0.2 2019-07-25 [1]
#> remotes 2.1.0 2019-06-24 [1]
#> rlang 0.4.4.9001 2020-02-29 [1]
#> rmarkdown 1.16 2019-10-01 [1]
#> rprojroot 1.3-2 2018-01-03 [1]
#> sessioninfo 1.1.1 2018-11-05 [1]
#> stringi 1.4.3 2019-03-12 [1]
#> stringr 1.4.0 2019-02-10 [1]
#> testthat 2.2.1 2019-07-25 [1]
#> tibble 2.99.99.9014 2020-02-29 [1]
#> usethis 1.5.1.9000 2020-02-29 [1]
#> vctrs 0.2.99.9006 2020-02-29 [1]
#> withr 2.1.2 2018-03-15 [1]
#> xfun 0.10 2019-10-01 [1]
#> yaml 2.2.0 2018-07-25 [1]
#> source
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.1)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> local
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> Github (r-lib/pillar@8f5918c)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> Github (r-lib/rlang@5af0b7f)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> Github (tidyverse/tibble@45603eb)
#> Github (r-lib/usethis@2a3d134)
#> Github (r-lib/vctrs@1ea8454)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#> CRAN (R 3.6.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
Most of the resources I've seen for converting geojson -> sf are for a vector, not a column within a data frame/tibble. Is my approach to converting wrong? Is the tibble combination not meant to be? Thoughts appreciated!