r-lib / vctrs

Generic programming with typed R vectors
https://vctrs.r-lib.org
Other
287 stars 66 forks source link

`rbind` error when using `add_row` on `sf` objects #1748

Open bart1 opened 1 year ago

bart1 commented 1 year ago

I'm not sure if this is an error of the vctrs implementation in sf or a problem in vtrcs, as the suggestion is to report it here I'll do that first. The error occurs when 0 rows are added to an sf data.frame (as part of code where the number of rows added is variable).

require(sf)
require(dplyr)
g<-data.frame(a=1, x=1,y=4)
add_row(g, a=numeric())
#>   a x y
#> 1 1 1 4
g<-st_as_sf(data.frame(a=1, x=1,y=4), coords = 2:3)
add_row(g, a=6)
#> Simple feature collection with 2 features and 1 field (with 1 geometry empty)
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 4 xmax: 1 ymax: 4
#> CRS:           NA
#>   a    geometry
#> 1 1 POINT (1 4)
#> 2 6 POINT EMPTY
add_row(g, a=numeric())
#> Error in `vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 3 instead of 1.
#> ℹ In file 'c.c' at line 412.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://https://tidyverse.org/help/>) and the full backtrace.

#> Backtrace:
#>     ▆
#>  1. ├─tibble::add_row(g, a = numeric())
#>  2. │ └─tibble:::rbind_at(.data, df, pos)
#>  3. │   └─vctrs::vec_rbind(old, new)
#>  4. └─rlang:::stop_internal_c_lib(...)
#>  5.   └─rlang::abort(message, call = call, .internal = TRUE, .frame = frame)
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/atlas/libblas.so.3.10.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=nl_NL.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=nl_NL.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=nl_NL.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.0.10 sf_1.0-9    
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.9         pillar_1.8.1       compiler_4.2.2     highr_0.9         
#>  [5] class_7.3-20       R.methodsS3_1.8.2  R.utils_2.12.0     tools_4.2.2       
#>  [9] digest_0.6.29      evaluate_0.17      lifecycle_1.0.3    tibble_3.1.8      
#> [13] R.cache_0.16.0     pkgconfig_2.0.3    rlang_1.0.6        reprex_2.0.2      
#> [17] cli_3.4.1          DBI_1.1.3          rstudioapi_0.14    yaml_2.3.6        
#> [21] xfun_0.33          fastmap_1.1.0      e1071_1.7-12       withr_2.5.0       
#> [25] styler_1.7.0       stringr_1.4.1      knitr_1.40         generics_0.1.3    
#> [29] fs_1.5.2           vctrs_0.5.1        tidyselect_1.2.0   grid_4.2.2        
#> [33] classInt_0.4-8     glue_1.6.2         R6_2.5.1           winch_0.0.11      
#> [37] fansi_1.0.3        rmarkdown_2.17     purrr_0.3.5        magrittr_2.0.3    
#> [41] units_0.8-0        htmltools_0.5.3    assertthat_0.2.1   KernSmooth_2.23-20
#> [45] utf8_1.2.2         stringi_1.7.8      proxy_0.4-27       R.oo_1.25.0

Created on 2022-11-25 with reprex v2.0.2

DanChaltiel commented 1 year ago

Same here with pckg_vrs columns so it might be a wider problem:

Sys.setenv(LANGUAGE="en")
a=tibble::tibble(y=package_version("1.0.0"))
b=tibble::tibble(x=character(0))
dplyr::bind_rows(a, b)
#> Warning in y[bad] <- rep.int(list(integer()), length(bad)): number of items to
#> replace is not a multiple of replacement length
#> Error in `vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 3 instead of 1.
#> i In file 'c.c' at line 414.
#> i This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
#> Backtrace:
#>     x
#>  1. +-dplyr::bind_rows(a, b)
#>  2. | \-vctrs::vec_rbind(!!!dots, .names_to = .id, .error_call = current_env())
#>  3. \-rlang:::stop_internal_c_lib(...)
#>  4.   \-rlang::abort(message, call = call, .internal = TRUE, .frame = frame)

Created on 2023-05-05 with reprex v2.0.2

Oddly, I don't get the Warning in y[bad] output outside of reprex.

avsdev-cw commented 1 year ago

Also seeing this, however for me it is caused by binding 2 tibbles where only 1 has a geometry column and either tibble is empty:

  df1 <- tibble::tibble(
    ref = c("143_2", "143_2", "143_2", "143_2", "143_2"),
    block = c("206/3", "217/8", "206/3", "206/3", "217/8"),
    date = structure(c(19448, 19449, 19449, 19450, 19451), class = "Date")
  )

  df2 <- tibble::tibble(
    ref = character(0),
    latitude = numeric(0),
    longitude = numeric(0),
    date = structure(numeric(0), class = "Date"),
    geometry = sf::st_sfc(crs = 4326)
  )

  dplyr::bind_rows(df1, df2)
#> Error in `vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 6 instead of 5.
#> ℹ In file 'c.c' at line 412.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.

Created on 2023-07-21 by the reprex package (v2.0.0)

  df1 <- tibble::tibble(
    ref = c("143_2", "143_2", "143_2", "143_2", "143_2"),
    block = c("206/3", "217/8", "206/3", "206/3", "217/8"),
    date = structure(c(19448, 19449, 19449, 19450, 19451), class = "Date"),
    geometry = sf::st_sfc(sf::st_polygon(), crs = 4326)
  )

  df2 <- tibble::tibble(
    ref = character(0),
    latitude = numeric(0),
    longitude = numeric(0),
    date = structure(numeric(0), class = "Date")
  )

  dplyr::bind_rows(df1, df2)
#> Error in `vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 7 instead of 5.
#> ℹ In file 'c.c' at line 412.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.

Created on 2023-07-21 by the reprex package (v2.0.0)

  df1 <- tibble::tibble(
    ref = c("143_2", "143_2", "143_2", "143_2", "143_2"),
    block = c("206/3", "217/8", "206/3", "206/3", "217/8"),
    date = structure(c(19448, 19449, 19449, 19450, 19451), class = "Date"),
    geometry = sf::st_sfc(sf::st_polygon(), crs = 4326)
  )

  df2 <- tibble::tibble(
    ref = character(0),
    latitude = numeric(0),
    longitude = numeric(0),
    date = structure(numeric(0), class = "Date"),
    geometry = sf::st_sfc(crs = 4326)
  )

  dplyr::bind_rows(df1, df2)
#> # A tibble: 5 × 6
#>   ref   block date            geometry latitude longitude
#>   <chr> <chr> <date>     <POLYGON [°]>    <dbl>     <dbl>
#> 1 143_2 206/3 2023-04-01         EMPTY       NA        NA
#> 2 143_2 217/8 2023-04-02         EMPTY       NA        NA
#> 3 143_2 206/3 2023-04-02         EMPTY       NA        NA
#> 4 143_2 206/3 2023-04-03         EMPTY       NA        NA
#> 5 143_2 217/8 2023-04-04         EMPTY       NA        NA

Created on 2023-07-21 by the reprex package (v2.0.0)

njtierney commented 5 months ago

I get this error when binding a tibble with some rows to a spatial data frame with 0 rows:

library(tibble)
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.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
dat <- tibble(x = 1:5, y = 11:15)
dat
#> # A tibble: 5 × 2
#>       x     y
#>   <int> <int>
#> 1     1    11
#> 2     2    12
#> 3     3    13
#> 4     4    14
#> 5     5    15
dat_sf <- st_sf(geometry=st_sfc())
dat_sf
#> Simple feature collection with 0 features and 0 fields
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> CRS:           NA
#> [1] geometry
#> <0 rows> (or 0-length row.names)
bind_rows(dat, dat_sf)
#> Error in `vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 6 instead of 5.
#> ℹ In file 'c.c' at line 414.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
bind_rows(dat_sf, dat)
#> Error in `vec_rbind()`:
#> ! `c()` method returned a vector of unexpected size 6 instead of 5.
#> ℹ In file 'c.c' at line 414.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
bind_rows(dat_sf, dat_sf)
#> Simple feature collection with 0 features and 0 fields
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> CRS:           NA
#> [1] geometry
#> <0 rows> (or 0-length row.names)
bind_rows(dat, dat)
#> # A tibble: 10 × 2
#>        x     y
#>    <int> <int>
#>  1     1    11
#>  2     2    12
#>  3     3    13
#>  4     4    14
#>  5     5    15
#>  6     1    11
#>  7     2    12
#>  8     3    13
#>  9     4    14
#> 10     5    15

Created on 2024-05-02 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.3 (2024-02-29) #> os macOS Sonoma 14.3.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Australia/Brisbane #> date 2024-05-02 #> pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> class 7.3-22 2023-05-03 [2] CRAN (R 4.3.3) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.0) #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.1) #> DBI 1.2.2 2024-02-16 [1] CRAN (R 4.3.1) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.1) #> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.1) #> e1071 1.7-14 2023-12-06 [1] CRAN (R 4.3.1) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.1) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.1) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.1) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.1) #> KernSmooth 2.23-22 2023-07-10 [2] CRAN (R 4.3.3) #> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) #> R.cache 0.16.0 2022-07-21 [2] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [2] CRAN (R 4.3.0) #> R.oo 1.26.0 2024-01-24 [2] CRAN (R 4.3.1) #> R.utils 2.12.3 2023-11-18 [2] CRAN (R 4.3.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.1) #> reprex 2.1.0 2024-01-11 [2] CRAN (R 4.3.1) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.1) #> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.3.1) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.3.0) #> sf * 1.0-16 2024-03-24 [1] CRAN (R 4.3.1) #> styler 1.10.3 2024-04-07 [2] CRAN (R 4.3.1) #> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.3.1) #> units 0.8-5 2023-11-28 [1] CRAN (R 4.3.1) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.1) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.1) #> winch 0.1.1 2024-02-19 [1] CRAN (R 4.3.1) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.1) #> xfun 0.43 2024-03-25 [1] CRAN (R 4.3.1) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.1) #> #> [1] /Users/nick/Library/R/arm64/4.3/library #> [2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```