Sf: select() returns different position of geometry column for data.frame and tibble representation

Created on 17 Jun 2020  Β·  15Comments  Β·  Source: r-spatial/sf

In case of an sf object created with st_read(as_tibble = TRUE) (or with read_sf()), the geometry column is moved to the first position when a subsequent select() is run. This may have to do with recent changes regarding the renaming of the geometry column.

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
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2

# data.frame approach
nc <- st_read(system.file("shape/nc.shp", package="sf"),
              quiet = TRUE)
class(nc)
#> [1] "sf"         "data.frame"
colnames(nc)
#>  [1] "AREA"      "PERIMETER" "CNTY_"     "CNTY_ID"   "NAME"      "FIPS"     
#>  [7] "FIPSNO"    "CRESS_ID"  "BIR74"     "SID74"     "NWBIR74"   "BIR79"    
#> [13] "SID79"     "NWBIR79"   "geometry"
select(nc, NAME)
#> Simple feature collection with 100 features and 1 field
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> CRS:            4267
#> First 10 features:
#>           NAME                       geometry
#> 1         Ashe MULTIPOLYGON (((-81.47276 3...
#> 2    Alleghany MULTIPOLYGON (((-81.23989 3...
#> 3        Surry MULTIPOLYGON (((-80.45634 3...
#> 4    Currituck MULTIPOLYGON (((-76.00897 3...
#> 5  Northampton MULTIPOLYGON (((-77.21767 3...
#> 6     Hertford MULTIPOLYGON (((-76.74506 3...
#> 7       Camden MULTIPOLYGON (((-76.00897 3...
#> 8        Gates MULTIPOLYGON (((-76.56251 3...
#> 9       Warren MULTIPOLYGON (((-78.30876 3...
#> 10      Stokes MULTIPOLYGON (((-80.02567 3...

# tibble approach
nc <- st_read(system.file("shape/nc.shp", package="sf"),
              quiet = TRUE,
              as_tibble = TRUE)
class(nc)
#> [1] "sf"         "tbl_df"     "tbl"        "data.frame"
colnames(nc)
#>  [1] "AREA"      "PERIMETER" "CNTY_"     "CNTY_ID"   "NAME"      "FIPS"     
#>  [7] "FIPSNO"    "CRESS_ID"  "BIR74"     "SID74"     "NWBIR74"   "BIR79"    
#> [13] "SID79"     "NWBIR79"   "geometry"
select(nc, NAME)
#> Simple feature collection with 100 features and 1 field
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> CRS:            4267
#> # A tibble: 100 x 2
#>                                                               geometry NAME     
#>                                                     <MULTIPOLYGON [Β°]> <fct>    
#>  1 (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -81… Ashe     
#>  2 (((-81.23989 36.36536, -81.24069 36.37942, -81.26284 36.40504, -81… Alleghany
#>  3 (((-80.45634 36.24256, -80.47639 36.25473, -80.53688 36.25674, -80… Surry    
#>  4 (((-76.00897 36.3196, -76.01735 36.33773, -76.03288 36.33598, -76.… Currituck
#>  5 (((-77.21767 36.24098, -77.23461 36.2146, -77.29861 36.21153, -77.… Northamp…
#>  6 (((-76.74506 36.23392, -76.98069 36.23024, -76.99475 36.23558, -77… Hertford 
#>  7 (((-76.00897 36.3196, -75.95718 36.19377, -75.98134 36.16973, -76.… Camden   
#>  8 (((-76.56251 36.34057, -76.60424 36.31498, -76.64822 36.31532, -76… Gates    
#>  9 (((-78.30876 36.26004, -78.28293 36.29188, -78.32125 36.54553, -78… Warren   
#> 10 (((-80.02567 36.25023, -80.45301 36.25709, -80.43531 36.55104, -80… Stokes   
#> # … with 90 more rows

Created on 2020-06-17 by the reprex package (v0.3.0)

Session info

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.3 (2020-02-29)
#>  os       Linux Mint 18.1             
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  nl_BE.UTF-8                 
#>  ctype    nl_BE.UTF-8                 
#>  tz       Europe/Brussels             
#>  date     2020-06-17                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source                       
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.0)               
#>  backports     1.1.8   2020-06-17 [1] CRAN (R 3.6.3)               
#>  callr         3.4.3   2020-03-28 [1] CRAN (R 3.6.3)               
#>  class         7.3-17  2020-04-26 [4] CRAN (R 3.6.3)               
#>  classInt      0.4-3   2020-04-07 [1] CRAN (R 3.6.3)               
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 3.6.3)               
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.0)               
#>  DBI           1.1.0   2019-12-15 [1] CRAN (R 3.6.2)               
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.0)               
#>  devtools      2.3.0   2020-04-10 [1] CRAN (R 3.6.3)               
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 3.6.3)               
#>  dplyr       * 1.0.0   2020-05-29 [1] CRAN (R 3.6.3)               
#>  e1071         1.7-3   2019-11-26 [1] CRAN (R 3.6.2)               
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 3.6.3)               
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.1)               
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 3.6.2)               
#>  fs            1.4.1   2020-04-04 [1] CRAN (R 3.6.3)               
#>  generics      0.0.2   2018-11-29 [1] CRAN (R 3.6.0)               
#>  glue          1.4.1   2020-05-13 [1] CRAN (R 3.6.3)               
#>  highr         0.8     2019-03-20 [1] CRAN (R 3.6.0)               
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 3.6.3)               
#>  KernSmooth    2.23-17 2020-04-26 [4] CRAN (R 3.6.3)               
#>  knitr         1.28    2020-02-06 [1] CRAN (R 3.6.3)               
#>  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 3.6.3)               
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.0)               
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.0)               
#>  pillar        1.4.4   2020-05-05 [1] CRAN (R 3.6.3)               
#>  pkgbuild      1.0.8   2020-05-07 [1] CRAN (R 3.6.3)               
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 3.6.1)               
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 3.6.3)               
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 3.6.3)               
#>  processx      3.4.2   2020-02-09 [1] CRAN (R 3.6.3)               
#>  ps            1.3.3   2020-05-08 [1] CRAN (R 3.6.3)               
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 3.6.3)               
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 3.6.2)               
#>  Rcpp          1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3)               
#>  remotes       2.1.1   2020-02-15 [1] CRAN (R 3.6.3)               
#>  reprex        0.3.0   2019-05-16 [1] CRAN (R 3.6.1)               
#>  rlang         0.4.6   2020-05-02 [1] CRAN (R 3.6.3)               
#>  rmarkdown     2.2     2020-05-31 [1] CRAN (R 3.6.3)               
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.0)               
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.0)               
#>  sf          * 0.9-4   2020-06-17 [1] Github (r-spatial/sf@59de60e)
#>  stringi       1.4.6   2020-02-17 [1] CRAN (R 3.6.3)               
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.0)               
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 3.6.3)               
#>  tibble        3.0.1   2020-04-20 [1] CRAN (R 3.6.3)               
#>  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 3.6.3)               
#>  units         0.6-7   2020-06-13 [1] CRAN (R 3.6.3)               
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 3.6.3)               
#>  utf8          1.1.4   2018-05-24 [1] CRAN (R 3.6.0)               
#>  vctrs         0.3.1   2020-06-05 [1] CRAN (R 3.6.3)               
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 3.6.3)               
#>  xfun          0.14    2020-05-20 [1] CRAN (R 3.6.3)               
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 3.6.2)               
#> 
#> [1] /home/floris/lib/R/library
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

Most helpful comment

If I may add my 2-cents, I would be in favor of having it sticky to the back. It is what most sf users are used to, and also the geometry column is usually "large", so when printing an object it would take "most of the screen", making the printed object less readable .

All 15 comments

My guess is that this has to do with recent changes in dplyr 1.0 (and vctrs).

FWIW, re-ran this with sf 0.9-3, where the problem doesn't occur (with same versions of dplyr & vctrs).

Reprex with sf 0.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
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2

# data.frame approach
nc <- st_read(system.file("shape/nc.shp", package="sf"),
              quiet = TRUE)
class(nc)
#> [1] "sf"         "data.frame"
colnames(nc)
#>  [1] "AREA"      "PERIMETER" "CNTY_"     "CNTY_ID"   "NAME"      "FIPS"     
#>  [7] "FIPSNO"    "CRESS_ID"  "BIR74"     "SID74"     "NWBIR74"   "BIR79"    
#> [13] "SID79"     "NWBIR79"   "geometry"
select(nc, NAME)
#> Simple feature collection with 100 features and 1 field
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> CRS:            4267
#> First 10 features:
#>           NAME                       geometry
#> 1         Ashe MULTIPOLYGON (((-81.47276 3...
#> 2    Alleghany MULTIPOLYGON (((-81.23989 3...
#> 3        Surry MULTIPOLYGON (((-80.45634 3...
#> 4    Currituck MULTIPOLYGON (((-76.00897 3...
#> 5  Northampton MULTIPOLYGON (((-77.21767 3...
#> 6     Hertford MULTIPOLYGON (((-76.74506 3...
#> 7       Camden MULTIPOLYGON (((-76.00897 3...
#> 8        Gates MULTIPOLYGON (((-76.56251 3...
#> 9       Warren MULTIPOLYGON (((-78.30876 3...
#> 10      Stokes MULTIPOLYGON (((-80.02567 3...

# tibble approach
nc <- st_read(system.file("shape/nc.shp", package="sf"),
              quiet = TRUE,
              as_tibble = TRUE)
class(nc)
#> [1] "sf"         "tbl_df"     "tbl"        "data.frame"
colnames(nc)
#>  [1] "AREA"      "PERIMETER" "CNTY_"     "CNTY_ID"   "NAME"      "FIPS"     
#>  [7] "FIPSNO"    "CRESS_ID"  "BIR74"     "SID74"     "NWBIR74"   "BIR79"    
#> [13] "SID79"     "NWBIR79"   "geometry"
select(nc, NAME)
#> Simple feature collection with 100 features and 1 field
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> CRS:            4267
#> # A tibble: 100 x 2
#>    NAME                                                                 geometry
#>    <fct>                                                      <MULTIPOLYGON [Β°]>
#>  1 Ashe       (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -8…
#>  2 Alleghany  (((-81.23989 36.36536, -81.24069 36.37942, -81.26284 36.40504, -8…
#>  3 Surry      (((-80.45634 36.24256, -80.47639 36.25473, -80.53688 36.25674, -8…
#>  4 Currituck  (((-76.00897 36.3196, -76.01735 36.33773, -76.03288 36.33598, -76…
#>  5 Northampt… (((-77.21767 36.24098, -77.23461 36.2146, -77.29861 36.21153, -77…
#>  6 Hertford   (((-76.74506 36.23392, -76.98069 36.23024, -76.99475 36.23558, -7…
#>  7 Camden     (((-76.00897 36.3196, -75.95718 36.19377, -75.98134 36.16973, -76…
#>  8 Gates      (((-76.56251 36.34057, -76.60424 36.31498, -76.64822 36.31532, -7…
#>  9 Warren     (((-78.30876 36.26004, -78.28293 36.29188, -78.32125 36.54553, -7…
#> 10 Stokes     (((-80.02567 36.25023, -80.45301 36.25709, -80.43531 36.55104, -8…
#> # … with 90 more rows

Created on 2020-06-18 by the reprex package (v0.3.0)

Session info

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.3 (2020-02-29)
#>  os       Linux Mint 18.1             
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  nl_BE.UTF-8                 
#>  ctype    nl_BE.UTF-8                 
#>  tz       Europe/Brussels             
#>  date     2020-06-18                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.8   2020-06-17 [1] CRAN (R 3.6.3)
#>  callr         3.4.3   2020-03-28 [1] CRAN (R 3.6.3)
#>  class         7.3-17  2020-04-26 [4] CRAN (R 3.6.3)
#>  classInt      0.4-3   2020-04-07 [1] CRAN (R 3.6.3)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 3.6.3)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.0)
#>  DBI           1.1.0   2019-12-15 [1] CRAN (R 3.6.2)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.0)
#>  devtools      2.3.0   2020-04-10 [1] CRAN (R 3.6.3)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 3.6.3)
#>  dplyr       * 1.0.0   2020-05-29 [1] CRAN (R 3.6.3)
#>  e1071         1.7-3   2019-11-26 [1] CRAN (R 3.6.2)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 3.6.3)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.1)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 3.6.2)
#>  fs            1.4.1   2020-04-04 [1] CRAN (R 3.6.3)
#>  generics      0.0.2   2018-11-29 [1] CRAN (R 3.6.0)
#>  glue          1.4.1   2020-05-13 [1] CRAN (R 3.6.3)
#>  highr         0.8     2019-03-20 [1] CRAN (R 3.6.0)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 3.6.3)
#>  KernSmooth    2.23-17 2020-04-26 [4] CRAN (R 3.6.3)
#>  knitr         1.28    2020-02-06 [1] CRAN (R 3.6.3)
#>  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 3.6.3)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.0)
#>  pillar        1.4.4   2020-05-05 [1] CRAN (R 3.6.3)
#>  pkgbuild      1.0.8   2020-05-07 [1] CRAN (R 3.6.3)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 3.6.1)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 3.6.3)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 3.6.3)
#>  processx      3.4.2   2020-02-09 [1] CRAN (R 3.6.3)
#>  ps            1.3.3   2020-05-08 [1] CRAN (R 3.6.3)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 3.6.3)
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 3.6.2)
#>  Rcpp          1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3)
#>  remotes       2.1.1   2020-02-15 [1] CRAN (R 3.6.3)
#>  reprex        0.3.0   2019-05-16 [1] CRAN (R 3.6.1)
#>  rlang         0.4.6   2020-05-02 [1] CRAN (R 3.6.3)
#>  rmarkdown     2.2     2020-05-31 [1] CRAN (R 3.6.3)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.0)
#>  sf          * 0.9-3   2020-05-04 [1] CRAN (R 3.6.3)
#>  stringi       1.4.6   2020-02-17 [1] CRAN (R 3.6.3)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 3.6.3)
#>  tibble        3.0.1   2020-04-20 [1] CRAN (R 3.6.3)
#>  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 3.6.3)
#>  units         0.6-7   2020-06-13 [1] CRAN (R 3.6.3)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 3.6.3)
#>  utf8          1.1.4   2018-05-24 [1] CRAN (R 3.6.0)
#>  vctrs         0.3.1   2020-06-05 [1] CRAN (R 3.6.3)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 3.6.3)
#>  xfun          0.14    2020-05-20 [1] CRAN (R 3.6.3)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 3.6.2)
#> 
#> [1] /home/floris/lib/R/library
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

I also noticed that! One for the dplyr issue tracker, or should it be fixed by changing code in this repo? I suspect the former. I tried and failed to reproduce the issue on the latest version of dplyr, were you using the dev version @florisvdh ?

Reproducible example starting with a recent docker container that has the previous dplyr version:

docker pull rockerdev/geospatial@sha256:82d822b021d2638b512e252c9ae551fcdfc3a95907315769c5f4f60c8533350a

docker run --rm -it rockerdev/geospatial@sha256:82d822b021d2638b512e252c9ae551fcdfc3a95907315769c5f4f60c8533350a /bin/bash
R
library(dplyr)
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
select(nc, "NAME")
quit()

R
remotes::install_version("dplyr", version = "1.0.0")
library(dplyr)
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
select(nc, NAME)

@Robinlovelace no I was using dplyr from CRAN built for R 3.6.3, see session info:

dplyr       * 1.0.0   2020-05-29 [1] CRAN (R 3.6.3)

Different behaviour in R >= 4 ?

Update: I can reproduce the issue in the docker container and it seems to be caused by updating sf.

On the previous version of sf I got:

Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
> nc <- st_read(system.file("shape/nc.shp", package="sf"),
+               quiet = TRUE,
+               as_tibble = TRUE)
> select(nc, NAME)
Error in select(nc, NAME) : could not find function "select"
> 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

> select(nc, NAME)
Simple feature collection with 100 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
# A tibble: 100 x 2
   NAME                                                                 geometry
   <chr>                                                      <MULTIPOLYGON [Β°]>
 1 Ashe       (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -8…
 2 Alleghany  (((-81.23989 36.36536, -81.24069 36.37942, -81.26284 36.40504, -8…
 3 Surry      (((-80.45634 36.24256, -80.47639 36.25473, -80.53688 36.25674, -8…
 4 Currituck  (((-76.00897 36.3196, -76.01735 36.33773, -76.03288 36.33598, -76…
 5 Northampt… (((-77.21767 36.24098, -77.23461 36.2146, -77.29861 36.21153, -77…
 6 Hertford   (((-76.74506 36.23392, -76.98069 36.23024, -76.99475 36.23558, -7…
 7 Camden     (((-76.00897 36.3196, -75.95718 36.19377, -75.98134 36.16973, -76…
 8 Gates      (((-76.56251 36.34057, -76.60424 36.31498, -76.64822 36.31532, -7…
 9 Warren     (((-78.30876 36.26004, -78.28293 36.29188, -78.32125 36.54553, -7…
10 Stokes     (((-80.02567 36.25023, -80.45301 36.25709, -80.43531 36.55104, -8…

Then I ran the following commands:

remotes::install_github("r-spatial/sf")
q() # restart the session
R # start a new session
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

> library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
> nc = st_read(system.file("shape/nc.shp", package="sf"))
Reading layer `nc' from data source `/usr/local/lib/R/site-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
geographic CRS: NAD27
> select(nc, NAME)
Simple feature collection with 100 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
First 10 features:
          NAME                       geometry
1         Ashe MULTIPOLYGON (((-81.47276 3...
2    Alleghany MULTIPOLYGON (((-81.23989 3...
3        Surry MULTIPOLYGON (((-80.45634 3...
4    Currituck MULTIPOLYGON (((-76.00897 3...
5  Northampton MULTIPOLYGON (((-77.21767 3...
6     Hertford MULTIPOLYGON (((-76.74506 3...
7       Camden MULTIPOLYGON (((-76.00897 3...
8        Gates MULTIPOLYGON (((-76.56251 3...
9       Warren MULTIPOLYGON (((-78.30876 3...
10      Stokes MULTIPOLYGON (((-80.02567 3...
> nc <- st_read(system.file("shape/nc.shp", package="sf"),
+               quiet = TRUE,
+               as_tibble = TRUE)
> select(nc, NAME)
Simple feature collection with 100 features and 1 field
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
# A tibble: 100 x 2
                                                              geometry NAME     
                                                    <MULTIPOLYGON [Β°]> <chr>    
 1 (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -81… Ashe     
 2 (((-81.23989 36.36536, -81.24069 36.37942, -81.26284 36.40504, -81… Alleghany
 3 (((-80.45634 36.24256, -80.47639 36.25473, -80.53688 36.25674, -80… Surry    
 4 (((-76.00897 36.3196, -76.01735 36.33773, -76.03288 36.33598, -76.… Currituck
 5 (((-77.21767 36.24098, -77.23461 36.2146, -77.29861 36.21153, -77.… Northamp…
 6 (((-76.74506 36.23392, -76.98069 36.23024, -76.99475 36.23558, -77… Hertford 
 7 (((-76.00897 36.3196, -75.95718 36.19377, -75.98134 36.16973, -76.… Camden   
 8 (((-76.56251 36.34057, -76.60424 36.31498, -76.64822 36.31532, -76… Gates    
 9 (((-78.30876 36.26004, -78.28293 36.29188, -78.32125 36.54553, -78… Warren   
10 (((-80.02567 36.25023, -80.45301 36.25709, -80.43531 36.55104, -80… Stokes   
# … with 90 more rows

It is not related to the R version, as illustrated in the reprex on my work desktop that is still on good old R 3.6.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
    library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0
    nc = st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/home/robin/R/x86_64-pc-linux-gnu-library/3.6/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
#> geographic CRS: NAD27
    select(nc, NAME)
#> Simple feature collection with 100 features and 1 field
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> geographic CRS: NAD27
#> First 10 features:
#>           NAME                       geometry
#> 1         Ashe MULTIPOLYGON (((-81.47276 3...
#> 2    Alleghany MULTIPOLYGON (((-81.23989 3...
#> 3        Surry MULTIPOLYGON (((-80.45634 3...
#> 4    Currituck MULTIPOLYGON (((-76.00897 3...
#> 5  Northampton MULTIPOLYGON (((-77.21767 3...
#> 6     Hertford MULTIPOLYGON (((-76.74506 3...
#> 7       Camden MULTIPOLYGON (((-76.00897 3...
#> 8        Gates MULTIPOLYGON (((-76.56251 3...
#> 9       Warren MULTIPOLYGON (((-78.30876 3...
#> 10      Stokes MULTIPOLYGON (((-80.02567 3...
    nc <- st_read(system.file("shape/nc.shp", package="sf"),
                  quiet = TRUE,
                  as_tibble = TRUE)
    select(nc, NAME)
#> Simple feature collection with 100 features and 1 field
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> geographic CRS: NAD27
#> # A tibble: 100 x 2
#>                                                               geometry NAME     
#>                                                     <MULTIPOLYGON [Β°]> <fct>    
#>  1 (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -81… Ashe     
#>  2 (((-81.23989 36.36536, -81.24069 36.37942, -81.26284 36.40504, -81… Alleghany
#>  3 (((-80.45634 36.24256, -80.47639 36.25473, -80.53688 36.25674, -80… Surry    
#>  4 (((-76.00897 36.3196, -76.01735 36.33773, -76.03288 36.33598, -76.… Currituck
#>  5 (((-77.21767 36.24098, -77.23461 36.2146, -77.29861 36.21153, -77.… Northamp…
#>  6 (((-76.74506 36.23392, -76.98069 36.23024, -76.99475 36.23558, -77… Hertford 
#>  7 (((-76.00897 36.3196, -75.95718 36.19377, -75.98134 36.16973, -76.… Camden   
#>  8 (((-76.56251 36.34057, -76.60424 36.31498, -76.64822 36.31532, -76… Gates    
#>  9 (((-78.30876 36.26004, -78.28293 36.29188, -78.32125 36.54553, -78… Warren   
#> 10 (((-80.02567 36.25023, -80.45301 36.25709, -80.43531 36.55104, -80… Stokes   
#> # … with 90 more rows

Created on 2020-06-18 by the reprex package (v0.3.0)

Session info

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.3 (2020-02-29)
#>  os       Ubuntu 18.04.4 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_GB:en                    
#>  collate  en_GB.UTF-8                 
#>  ctype    en_GB.UTF-8                 
#>  tz       Europe/London               
#>  date     2020-06-18                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                             
#>  assertthat    0.2.1      2019-03-21 [2] CRAN (R 3.6.0)                     
#>  backports     1.1.7      2020-05-13 [1] CRAN (R 3.6.3)                     
#>  callr         3.4.3      2020-03-28 [1] CRAN (R 3.6.3)                     
#>  class         7.3-17     2020-04-26 [2] CRAN (R 3.6.3)                     
#>  classInt      0.4-3      2020-04-06 [1] Github (r-spatial/classInt@d024051)
#>  cli           2.0.2      2020-02-28 [1] CRAN (R 3.6.2)                     
#>  crayon        1.3.4      2017-09-16 [2] standard (@1.3.4)                  
#>  DBI           1.1.0      2019-12-15 [2] CRAN (R 3.6.2)                     
#>  desc          1.2.0      2018-05-01 [2] standard (@1.2.0)                  
#>  devtools      2.3.0      2020-04-10 [1] CRAN (R 3.6.3)                     
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 3.6.2)                     
#>  dplyr       * 1.0.0.9000 2020-06-16 [1] Github (tidyverse/dplyr@fd08fe9)   
#>  e1071         1.7-3      2019-11-26 [2] CRAN (R 3.6.1)                     
#>  ellipsis      0.3.1      2020-05-15 [3] CRAN (R 3.6.3)                     
#>  evaluate      0.14       2019-05-28 [2] CRAN (R 3.6.0)                     
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.2)                     
#>  fs            1.4.1      2020-04-04 [2] CRAN (R 3.6.3)                     
#>  generics      0.0.2      2018-11-29 [3] CRAN (R 3.5.1)                     
#>  glue          1.4.1      2020-05-13 [2] CRAN (R 3.6.3)                     
#>  highr         0.8        2019-03-20 [3] CRAN (R 3.5.3)                     
#>  htmltools     0.4.0.9003 2020-04-09 [1] Github (rstudio/htmltools@1a7d0dc) 
#>  KernSmooth    2.23-17    2020-04-26 [4] CRAN (R 3.6.3)                     
#>  knitr         1.28       2020-02-06 [1] CRAN (R 3.6.2)                     
#>  lifecycle     0.2.0.9000 2020-03-16 [1] Github (r-lib/lifecycle@355dcba)   
#>  magrittr      1.5        2014-11-22 [2] CRAN (R 3.5.2)                     
#>  memoise       1.1.0      2017-04-21 [3] CRAN (R 3.5.0)                     
#>  pillar        1.4.4      2020-05-05 [1] CRAN (R 3.6.3)                     
#>  pkgbuild      1.0.8      2020-05-07 [1] CRAN (R 3.6.3)                     
#>  pkgconfig     2.0.3      2019-09-22 [2] CRAN (R 3.6.1)                     
#>  pkgload       1.1.0      2020-05-29 [3] CRAN (R 3.6.3)                     
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 3.6.2)                     
#>  processx      3.4.2      2020-02-09 [1] CRAN (R 3.6.3)                     
#>  ps            1.3.3      2020-05-08 [1] CRAN (R 3.6.3)                     
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 3.6.3)                     
#>  R6            2.4.1      2019-11-12 [2] CRAN (R 3.6.1)                     
#>  Rcpp          1.0.4.6    2020-04-09 [1] CRAN (R 3.6.3)                     
#>  remotes       2.1.1      2020-02-15 [1] CRAN (R 3.6.2)                     
#>  rlang         0.4.6.9000 2020-06-16 [1] Github (r-lib/rlang@eecbd90)       
#>  rmarkdown     2.2        2020-05-31 [1] CRAN (R 3.6.3)                     
#>  rprojroot     1.3-2      2018-01-03 [2] CRAN (R 3.5.3)                     
#>  sessioninfo   1.1.1      2018-11-05 [3] CRAN (R 3.5.1)                     
#>  sf          * 0.9-4      2020-06-16 [1] Github (r-spatial/sf@aa6dba2)      
#>  stringi       1.4.6      2020-02-17 [1] CRAN (R 3.6.2)                     
#>  stringr       1.4.0      2019-02-10 [2] standard (@1.4.0)                  
#>  testthat      2.3.2      2020-03-02 [1] CRAN (R 3.6.3)                     
#>  tibble        3.0.1      2020-04-20 [1] CRAN (R 3.6.3)                     
#>  tidyselect    1.1.0      2020-05-11 [1] CRAN (R 3.6.3)                     
#>  units         0.6-7      2020-06-13 [1] CRAN (R 3.6.3)                     
#>  usethis       1.6.1      2020-04-29 [1] CRAN (R 3.6.3)                     
#>  utf8          1.1.4      2018-05-24 [2] CRAN (R 3.5.3)                     
#>  vctrs         0.3.1      2020-06-05 [1] CRAN (R 3.6.3)                     
#>  withr         2.2.0      2020-04-20 [2] CRAN (R 3.6.3)                     
#>  xfun          0.14       2020-05-20 [1] CRAN (R 3.6.3)                     
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 3.6.2)                     
#> 
#> [1] /home/robin/R/x86_64-pc-linux-gnu-library/3.6
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

OK, you're on latest dplyr master. From comparison with my reprexes (sf dev + 0.9-3, always with dplyr from CRAN) the dplyr version difference doesn't matter. The problem occurred at least from CRAN's sf version 0.9-4 on, where I first met it (not shown).

It might be due to changes in the vctrs methods in package sf, which were modified before the latest sf release by @lionel- ; I'll have a look at them.

Probably unrelated to vctrs, I think it's from #1417.

Note that it is theoretically the correct behaviour. According to dplyr semantics, sticky columns that aren't part of the selection should be added back to the front. We're planning to add a generic mechanism for managing sticky columns so that subclasses don't have to manually do it, and this will be the behaviour that we'll implement.

I understand that it is likely too late to switch to this behaviour though, given it's a breaking change. I didn't notice it because it wasn't covered by unit tests.

Actually maybe the generic mechanism should be configurable, this way a class can choose to make them sticky to the front or the back?

@edzer I can also take a look at reviewing 1417 if you'd like.

If I may add my 2-cents, I would be in favor of having it sticky to the back. It is what most sf users are used to, and also the geometry column is usually "large", so when printing an object it would take "most of the screen", making the printed object less readable .

You're right, I think it makes sense to be able to change the default behaviour. I take back my comment about front-stickiness being the correct behaviour for a dplyr interface.

I also like it more when the geometry columns sticks at the end, which is where st_read/read_sf puts it. If you want to modify the order, you can always do a select() with all the names specified in the order favored.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kendonB picture kendonB  Β·  4Comments

happyshows picture happyshows  Β·  3Comments

Nowosad picture Nowosad  Β·  3Comments

Nosferican picture Nosferican  Β·  3Comments

adrfantini picture adrfantini  Β·  4Comments