thomasp85 / tidygraph

A tidy API for graph manipulation
https://tidygraph.data-imaginist.com
Other
547 stars 62 forks source link

filter() fails on grouped data #193

Open hughjonesd opened 5 months ago

hughjonesd commented 5 months ago

Here's a reprex:

library(ggraph)
data(whigs)

whigs <- tidygraph::as_tbl_graph(whigs) |> 
  activate(nodes) |> 
  mutate(AM = grepl("^[A-M]", name)) 

# Works fine:
whigs |> filter(grepl("Austin", name))
# A tbl_graph: 2 nodes and 0 edges
#
# A rooted forest with 2 trees
#
# Node Data: 2 × 3 (active)
# ... etcetera

whigs |> group_by(AM) |> filter(grepl("Austin", name))
# A tbl_graph: 2 nodes and 0 edges
#
# A rooted forest with 2 trees
Error in `vec_slice()`:
! Column `type` (size 2) must match the data frame (size 261).
ℹ In file slice.c at line 191.
ℹ Install the winch package to get additional debugging info the next time you get this error.
ℹ 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 and the full backtrace.
Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. ├─tidygraph:::print.tbl_graph(x)
  3. │ ├─base::print(new_name_tibble(x, NULL, top, " (active)"), ...)
  4. │ └─pillar:::print.tbl(...)
  5. │   └─pillar:::print_tbl(...)
  6. │     ├─base::writeLines(...)
  7. │     ├─base::format(...)
  8. │     └─pillar:::format.tbl(...)
  9. │       └─pillar:::format_tbl(...)
 10. │         └─pillar::tbl_format_setup(...)
 11. │           ├─pillar:::tbl_format_setup_dispatch(...)
 12. │           └─pillar:::tbl_format_setup.tbl(...)
 13. │             └─pillar:::df_head(x, n)
 14. │               └─pillar:::vec_head(as.data.frame(x), n)
 15. │                 └─vctrs::vec_slice(x, seq_len(n))
 16. └─rlang:::stop_internal_c_lib(...)
 17.   └─rlang::abort(message, call = call, .internal = TRUE, .frame = frame)

This is with tidygraph 1.3.1, R 4.4.0 and dplyr 1.1.4. Also confirmed on github as of today.

I'd expect filter to work as normal on grouped data, but I may be missing something....