Skip to content

Tidyverse methods that remove nodes drop agr attributes #275

@loreabad6

Description

@loreabad6

Describe the bug
Those tidyverse methods in tidygraph that recreate the network when removing nodes do not preserve the agr attributes from the sf object. It comes from the igraph::delete_vertices() function that is recreating the network. So far I found this for

  • filter.tbl_graph()
  • slice.tbl_graph() and their slice_*.tbl_graph() functions
  • distinct.tbl_graph()

Reproducible example

library(sfnetworks)
library(tidygraph)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.9.3, PROJ 9.5.0; sf_use_s2() is TRUE

net = as_sfnetwork(roxel)

net |> 
  activate(edges) |> 
  st_agr()
#>     from       to     name     type 
#>     <NA>     <NA> constant constant 
#> Levels: constant aggregate identity

# filter.tbl_graph()
net |> 
  mutate(foo = sample(1:5, 987, replace = TRUE)) |> 
  filter(foo == 2) |> 
  activate(edges) |> 
  st_agr()
#> from   to name type 
#> <NA> <NA> <NA> <NA> 
#> Levels: constant aggregate identity

# slice.tbl_graph()
net |> 
  slice(3:7) |> 
  activate(edges) |> 
  st_agr()
#> from   to name type 
#> <NA> <NA> <NA> <NA> 
#> Levels: constant aggregate identity

# slice_sample.tbl_graph()
net |> 
  slice_sample(n = 20) |> 
  activate(edges) |> 
  st_agr()
#> from   to name type 
#> <NA> <NA> <NA> <NA> 
#> Levels: constant aggregate identity

# distinct.tbl_graph()
net |> 
  distinct() |> 
  activate(edges) |> 
  st_agr()
#> from   to name type 
#> <NA> <NA> <NA> <NA> 
#> Levels: constant aggregate identity

Expected behavior
The agr attributes are preserved.

R Session Info

sfnetworks v1.0 branch

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       Ubuntu 24.04.1 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Etc/UTC
#>  date     2024-11-03
#>  pandoc   3.5 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package          * version date (UTC) lib source
#>  abind              1.4-8   2024-09-12 [1] CRAN (R 4.4.2)
#>  class              7.3-22  2023-05-03 [2] CRAN (R 4.4.2)
#>  classInt           0.4-10  2023-09-05 [1] CRAN (R 4.4.2)
#>  cli                3.6.3   2024-06-21 [1] RSPM (R 4.4.0)
#>  colorspace         2.1-1   2024-07-26 [1] RSPM (R 4.4.0)
#>  DBI                1.2.3   2024-06-02 [1] RSPM (R 4.4.0)
#>  deldir             2.0-4   2024-02-28 [1] RSPM (R 4.4.0)
#>  digest             0.6.37  2024-08-19 [1] RSPM (R 4.4.0)
#>  dplyr            * 1.1.4   2023-11-17 [1] RSPM (R 4.4.0)
#>  e1071              1.7-16  2024-09-16 [1] CRAN (R 4.4.2)
#>  evaluate           1.0.1   2024-10-10 [1] RSPM (R 4.4.0)
#>  fansi              1.0.6   2023-12-08 [1] RSPM (R 4.4.0)
#>  fastmap            1.2.0   2024-05-15 [1] RSPM (R 4.4.0)
#>  forcats          * 1.0.0   2023-01-29 [1] RSPM (R 4.4.0)
#>  fs                 1.6.5   2024-10-30 [1] RSPM (R 4.4.0)
#>  generics           0.1.3   2022-07-05 [1] RSPM (R 4.4.0)
#>  ggplot2          * 3.5.1   2024-04-23 [1] RSPM (R 4.4.0)
#>  glue               1.8.0   2024-09-30 [1] RSPM (R 4.4.0)
#>  goftest            1.2-3   2021-10-07 [1] RSPM (R 4.4.0)
#>  gtable             0.3.6   2024-10-25 [1] RSPM (R 4.4.0)
#>  hms                1.1.3   2023-03-21 [1] RSPM (R 4.4.0)
#>  htmltools          0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0)
#>  igraph             2.1.1   2024-10-19 [1] RSPM (R 4.4.0)
#>  KernSmooth         2.23-24 2024-05-17 [2] CRAN (R 4.4.2)
#>  knitr              1.48    2024-07-07 [1] RSPM (R 4.4.0)
#>  lattice            0.22-6  2024-03-20 [2] CRAN (R 4.4.2)
#>  lifecycle          1.0.4   2023-11-07 [1] RSPM (R 4.4.0)
#>  lubridate        * 1.9.3   2023-09-27 [1] RSPM (R 4.4.0)
#>  lwgeom             0.2-15  2024-11-03 [1] Github (r-spatial/lwgeom@4569f09)
#>  magrittr           2.0.3   2022-03-30 [1] RSPM (R 4.4.0)
#>  Matrix             1.7-1   2024-10-18 [2] CRAN (R 4.4.2)
#>  mgcv               1.9-1   2023-12-21 [2] CRAN (R 4.4.2)
#>  munsell            0.5.1   2024-04-01 [1] RSPM (R 4.4.0)
#>  nlme               3.1-166 2024-08-14 [2] CRAN (R 4.4.2)
#>  pillar             1.9.0   2023-03-22 [1] RSPM (R 4.4.0)
#>  pkgconfig          2.0.3   2019-09-22 [1] RSPM (R 4.4.0)
#>  polyclip           1.10-7  2024-07-23 [1] RSPM (R 4.4.0)
#>  proxy              0.4-27  2022-06-09 [1] CRAN (R 4.4.2)
#>  purrr            * 1.0.2   2023-08-10 [1] RSPM (R 4.4.0)
#>  R6                 2.5.1   2021-08-19 [1] RSPM (R 4.4.0)
#>  Rcpp               1.0.13  2024-07-17 [1] RSPM (R 4.4.0)
#>  readr            * 2.1.5   2024-01-10 [1] RSPM (R 4.4.0)
#>  reprex             2.1.1   2024-07-06 [1] RSPM (R 4.4.0)
#>  rlang              1.1.4   2024-06-04 [1] RSPM (R 4.4.0)
#>  rmarkdown          2.28    2024-08-17 [1] RSPM (R 4.4.0)
#>  rpart              4.1.23  2023-12-05 [2] CRAN (R 4.4.2)
#>  rstudioapi         0.17.1  2024-10-22 [1] RSPM (R 4.4.0)
#>  s2                 1.1.7   2024-07-17 [1] CRAN (R 4.4.2)
#>  scales             1.3.0   2023-11-28 [1] RSPM (R 4.4.0)
#>  sessioninfo        1.2.2   2021-12-06 [1] RSPM (R 4.4.0)
#>  sf               * 1.0-19  2024-11-03 [1] Github (r-spatial/sf@dc02f71)
#>  sfheaders          0.4.4   2024-01-17 [1] RSPM (R 4.4.0)
#>  sfnetworks       * 0.6.4   2024-11-03 [1] local
#>  spatstat.data      3.1-2   2024-06-21 [1] RSPM (R 4.4.0)
#>  spatstat.explore   3.3-3   2024-10-22 [1] RSPM (R 4.4.0)
#>  spatstat.geom      3.3-3   2024-09-18 [1] RSPM (R 4.4.0)
#>  spatstat.linnet    3.2-2   2024-09-20 [1] RSPM (R 4.4.0)
#>  spatstat.model     3.3-2   2024-09-19 [1] RSPM (R 4.4.0)
#>  spatstat.random    3.3-2   2024-09-18 [1] RSPM (R 4.4.0)
#>  spatstat.sparse    3.1-0   2024-06-21 [1] RSPM (R 4.4.0)
#>  spatstat.univar    3.0-1   2024-09-05 [1] RSPM (R 4.4.0)
#>  spatstat.utils     3.1-0   2024-08-17 [1] RSPM (R 4.4.0)
#>  stringi            1.8.4   2024-05-06 [1] RSPM (R 4.4.0)
#>  stringr          * 1.5.1   2023-11-14 [1] RSPM (R 4.4.0)
#>  tensor             1.5     2012-05-05 [1] RSPM (R 4.4.0)
#>  tibble           * 3.2.1   2023-03-20 [1] RSPM (R 4.4.0)
#>  tidygraph        * 1.3.1   2024-01-30 [1] RSPM (R 4.4.0)
#>  tidyr            * 1.3.1   2024-01-24 [1] RSPM (R 4.4.0)
#>  tidyselect         1.2.1   2024-03-11 [1] RSPM (R 4.4.0)
#>  tidyverse        * 2.0.0   2023-02-22 [1] RSPM (R 4.4.0)
#>  timechange         0.3.0   2024-01-18 [1] RSPM (R 4.4.0)
#>  tzdb               0.4.0   2023-05-12 [1] RSPM (R 4.4.0)
#>  units              0.8-5   2023-11-28 [1] RSPM (R 4.4.0)
#>  utf8               1.2.4   2023-10-22 [1] RSPM (R 4.4.0)
#>  vctrs              0.6.5   2023-12-01 [1] RSPM (R 4.4.0)
#>  withr              3.0.2   2024-10-28 [1] RSPM (R 4.4.0)
#>  wk                 0.9.4   2024-10-11 [1] CRAN (R 4.4.2)
#>  xfun               0.48    2024-10-03 [1] RSPM (R 4.4.0)
#>  yaml               2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] /usr/local/lib/R/site-library
#>  [2] /usr/local/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Metadata

Metadata

Assignees

Labels

bug 🐛Something isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions