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

Issues with new Tibble #1381

Closed
gregmacfarlane opened this issue Apr 23, 2020 · 5 comments
Closed

Issues with new Tibble #1381

gregmacfarlane opened this issue Apr 23, 2020 · 5 comments

Comments

@gregmacfarlane
Copy link

@gregmacfarlane gregmacfarlane commented Apr 23, 2020

The recent updates to tibble seem to have totally destroyed many of the tidyverse functionality of sf. Here's the examples from the help page.

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
nc = st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.6/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
nc %>% filter(AREA > .1) %>% plot()
#> Error: Can't slice a scalar
# plot 10 smallest counties in grey:
st_geometry(nc) %>% plot()
nc %>% select(AREA) %>% arrange(AREA) %>% slice(1:10) %>% plot(add = TRUE, col = 'grey')
#> Error: Can't slice a scalar
title("the ten counties with smallest area")

nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
nc %>% group_by(area_cl) %>% class()
#> Error: All columns in a tibble must be vectors.
#> x Column `geometry` is a `sfc_MULTIPOLYGON/sfc` object.
nc2 <- nc %>% mutate(area10 = AREA/10)
nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class()
#> Error: Input must be a vector, not a `sfc_MULTIPOLYGON/sfc` object.
nc %>% transmute(AREA = AREA/10) %>% class()
#> [1] "sf"         "data.frame"
nc %>% select(SID74, SID79) %>% names()
#> [1] "SID74"    "SID79"    "geometry"
nc %>% select(SID74, SID79, geometry) %>% names()
#> [1] "SID74"    "SID79"    "geometry"
nc %>% select(SID74, SID79) %>% class()
#> [1] "sf"         "data.frame"
nc %>% select(SID74, SID79, geometry) %>% class()
#> [1] "sf"         "data.frame"
nc2 <- nc %>% rename(area = AREA)
nc %>% slice(1:2)
#> Error: Can't slice a scalar
nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
nc.g <- nc %>% group_by(area_cl)
#> Error: All columns in a tibble must be vectors.
#> x Column `geometry` is a `sfc_MULTIPOLYGON/sfc` object.
nc.g %>% summarise(mean(AREA))
#> Error in eval(lhs, parent, parent): object 'nc.g' not found
nc.g %>% summarise(mean(AREA)) %>% plot(col = grey(3:6 / 7))
#> Error in eval(lhs, parent, parent): object 'nc.g' not found
nc %>% as.data.frame %>% summarise(mean(AREA))
#>   mean(AREA)
#> 1    0.12626
nc[c(1:100, 1:10), ] %>% distinct() %>% nrow()
#> [1] 100
library(tidyr)
nc %>% select(SID74, SID79) %>% gather("VAR", "SID", -geometry) %>% summary()
#>      VAR                 SID                  geometry  
#>  Length:200         Min.   : 0.000   MULTIPOLYGON :200  
#>  Class :character   1st Qu.: 2.000   epsg:4267    :  0  
#>  Mode  :character   Median : 5.000   +proj=long...:  0  
#>                     Mean   : 7.515                      
#>                     3rd Qu.: 9.000                      
#>                     Max.   :57.000
library(tidyr)
nc$row = 1:100 # needed for spread to work
nc %>% select(SID74, SID79, geometry, row) %>%
  gather("VAR", "SID", -geometry, -row) %>%
  spread(VAR, SID) %>% head()
#> Simple feature collection with 6 features and 3 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
#>   row SID74 SID79                       geometry
#> 1   1     1     0 MULTIPOLYGON (((-81.47276 3...
#> 2   2     0     3 MULTIPOLYGON (((-81.23989 3...
#> 3   3     5     6 MULTIPOLYGON (((-80.45634 3...
#> 4   4     1     2 MULTIPOLYGON (((-76.00897 3...
#> 5   5     9     3 MULTIPOLYGON (((-77.21767 3...
#> 6   6     7     5 MULTIPOLYGON (((-76.74506 3...
storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326)
#> Error: Input must be a vector, not a `sfc_POINT/sfc` object.
x <- storms.sf %>% group_by(name, year) %>% nest
#> Error in eval(lhs, parent, parent): object 'storms.sf' not found
trs = lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]) %>% st_sfc(crs = 4326)
#> Error in lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]): object 'x' not found
trs.sf = st_sf(x[,1:2], trs)
#> Error in st_sf(x[, 1:2], trs): object 'x' not found
plot(trs.sf["year"], axes = TRUE)
#> Error in plot(trs.sf["year"], axes = TRUE): object 'trs.sf' not found

sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.6
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] tidyr_1.0.2       dplyr_0.8.99.9002 sf_0.7-7         
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.4         knitr_1.24.6       magrittr_1.5      
#>  [4] units_0.6-6        tidyselect_1.0.0   R6_2.4.1          
#>  [7] rlang_0.4.5.9000   fansi_0.4.1        stringr_1.4.0     
#> [10] highr_0.8          tools_3.6.1        grid_3.6.1        
#> [13] xfun_0.9           KernSmooth_2.23-15 cli_2.0.2         
#> [16] e1071_1.7-3        DBI_1.1.0          ellipsis_0.3.0    
#> [19] htmltools_0.4.0    class_7.3-15       assertthat_0.2.1  
#> [22] yaml_2.2.0         digest_0.6.25      tibble_3.0.1      
#> [25] lifecycle_0.2.0    crayon_1.3.4       purrr_0.3.4       
#> [28] vctrs_0.2.99.9011  glue_1.4.0         evaluate_0.14     
#> [31] rmarkdown_1.14     stringi_1.4.3      pillar_1.4.3      
#> [34] compiler_3.6.1     generics_0.0.2     classInt_0.4-3    
#> [37] pkgconfig_2.0.3

Created on 2020-04-23 by the reprex package (v0.3.0)

@gregmacfarlane
Copy link
Author

@gregmacfarlane gregmacfarlane commented Apr 23, 2020

Seems to be confirmed by the breaking changes here: https://cran.r-project.org/web/packages/tibble/news/news.html

List classes are no longer automatically treated as vectors. Symptoms:

  • Error: All columns in a tibble must be vectors
  • Error: Expected a vector, not a ... object

If you implement a class that wraps a list as S3 vector, you need to include "list" in the class:
structure(x, class = c("your_s3_class", "list"))
Alternatively, implement a vec_proxy() method as described in https://vctrs.r-lib.org/reference/vec_data.html, or construct your class with list_of().

@Nowosad
Copy link
Contributor

@Nowosad Nowosad commented Apr 24, 2020

@gregmacfarlane I think the issue is with the outdated version of sf at your computer. You have version 0.7-7, and the current one is 0.9-2 (CRAN) and 0.9-3 (GitHub).

@gregmacfarlane
Copy link
Author

@gregmacfarlane gregmacfarlane commented Apr 28, 2020

Okay, confirmed. Hadn't seen any issues related to this, but glad to know you're on top of things.

@alankjackson
Copy link

@alankjackson alankjackson commented Oct 30, 2020

For anyone googling this, a further issue is if you have older saved sf data, it will need to be repaired to work now.

This works:

Zip_outlines <- sf::st_as_sf(Zip_outlines)

ptagliolato added a commit to ptagliolato/r-spatial-base that referenced this issue Nov 24, 2020
…w tibble and sf breaking the app solved upgrading sf (cf. r-spatial/sf#1381)
@francisbarton
Copy link

@francisbarton francisbarton commented Mar 19, 2021

Also for anyone arriving here via googling

Input must be a vector, not a sfc_MULTIPOLYGON/sfc object.

See https://community.rstudio.com/t/spatial-sf-function-fails-when-called-as-an-rstudio-job-works-otherwise/99462
If your RStudio job uses sf in a function, even if not explicitly called, it's best to include library(sf) in your job script.

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

4 participants