-
Notifications
You must be signed in to change notification settings - Fork 21
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
Not all tidygraph functions work on sfnetwork objects #29
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I run into this very same error when I try to add a new vertex to a graph which has sfc object as attribute. Here is code to replace this error. # Create Demo Graph
g <- igraph::make_tree(20, 3, mode = "out")
nV <- length(V(g))
nE <- length(E(g))
V(g)$label <- sample(letters, nV, replace = T) # fix this latter NAME needs to not identical
V(g)$attr1_int <- sample(seq(10), nV, replace = T)
V(g)$attr2_letter <- sample(LETTERS, nV, replace = T)
V(g)$attr3_numb <- runif(nV) * 100
sp_df = data.frame(x = runif(nV), y= runif(nV)) |> sf::st_as_sf(coords=c('x', 'y'))
V(g)$attr4_geom <- sp_df$geom
E(g)$attr1 <- runif(nE) * 100
# Below code cause bug
g = g + vertex(13) I suspect something do with |
A simpler example: # packages
library(igraph)
library(sf)
g <- make_empty_graph()
(g <- add_vertices(g, 2))
#> IGRAPH 4de905f D--- 2 0 --
#> + edges from 4de905f:
V(g)$geom <- st_sfc(st_point(c(0, 0)), st_point(c(1, 1)))
add_vertices(g, 1)
#> Error in UseMethod("st_bbox"): no applicable method for 'st_bbox' applied to an object of class "logical" Created on 2023-06-10 with reprex v2.0.2 Should we open an issue in the igraph repository? |
Work around. It turns out oversubscribe a sf object cause it to add a new empty geometry. Say you have 10 points in object Based on this I wrote my own #' add_vertex_sf
#'
#' @description When there is sf object in attributes
#' sf comparable adding vertex function.
#'
#' @param g igraph object
#' @param id a chr or that is passing to igraph::vertex
#' @param ... other arg pass to igraph::vertex
#' @param geom a geometry that to add if any
#' @return new added igraph
#'
#' @noRd
add_vertex_sf = function(g, id, ..., geom=NULL) {
stopifnot(is.null(geom) || sf::st_is_valid(geom))
schema = purrr::imap(vertex_attr(g), ~ class(.x))
sf_attr_i = purrr::detect_index(schema, ~'sfc' %in% .x)
sf_attr_name = names(schema)[sf_attr_i]
if(sf_attr_i != 0) {
sf_attr = vertex_attr(g, sf_attr_name)
if(!is.null(geom)) sf_attr = c(sf_attr, geom)
g = delete_vertex_attr(g, sf_attr_name)
g = g + vertex(id, ...)
set_vertex_attr(g, name = sf_attr_name, value = sf_attr[seq(length(V(g)))])
} else {
g = g + vertex(id, ...)
}
} |
An update on this issue:
Some more (simplified) insight on the issue of reason number 1: In |
Although
sfnetwork
subclassestbl_graph
, not all of tidygraph functions work on sfnetwork objects. This is caused by the geometry list column, and the nodes and edges being sf objects.The problems occur because of two reasons:
1. Usage of
igraph::add_vertices
and/origraph::add_edges
inside functionsFor some reason these functions break when there are list columns in the nodes and/or edges. The error given when using
igraph::add_vertices
is:It is a mystery for me why that function would call
sf::st_bbox
, but I did not look into it yet. When usingigraph::add_edges
RStudio even crashes completely (i.e. aborted because of fatal error).The affected functions are:
bind_nodes
bind_edges
right_join
full_join
The latter two are generics and the issue could be solved by writing a sfnetwork method for them. The first two are not generics.
2. Recreating a tbl_graph object inside a function
Creating a tbl_graph with
tidygraph::tbl_graph
does not work when the edges are an sf object, because of the sticky geometry column.The affected functions are:
graph_join
reroute
The first is not a generic. The latter is a generic so we can solve the issue by writing a sfnetwork method for it.
The text was updated successfully, but these errors were encountered: