| gs_subset | R Documentation |
gs_subset_nodes() retains a subset of nodes and automatically
removes any edge whose endpoint is no longer present.
gs_subset_edges() retains a subset of edges without modifying
the node set.
gs_subset_nodes(x, i)
gs_subset_edges(x, i)
x |
A |
i |
A filter specification. Accepted forms:
|
Node filtering preserves the normalized coordinate state.
Coordinates for surviving nodes remain in their current space
([0, 1] if normalized, raw coordinates otherwise), so
normalizeGraphSpace does not need to be re-run.
The @graph, @fdata, @nodes, and @edges
slots are all updated consistently. The @canvas and
background image are not modified.
Edge filtering leaves the node set and the layout entirely
intact. Because removing an edge from a group of parallel edges
invalidates the derived attributes curve_weight,
is_multiple, and is_loop for the remaining members of
that group, the full edge table is recomputed from @graph
after deletion.
Note on parallel edges: in non-simplified graphs containing
parallel edges between the same vertex pair, integer or logical
indexing is the most reliable approach. A predicate expression that
matches a shared attribute (such as edgeColor) will match
all parallel instances simultaneously, which is usually the intended
behavior.
A GraphSpace object with the selected subset
of nodes or edges.
cropGraphSpace, gs_nodes,
gs_edges, normalizeGraphSpace
library(RGraphSpace)
library(igraph)
# Create a directed star graph with numeric attributes
g <- make_star(10, mode = "out")
V(g)$nodeSize <- runif(vcount(g), 1, 10)
E(g)$weight <- runif(ecount(g), 0, 1)
gs <- GraphSpace(g)
gs <- normalizeGraphSpace(gs)
#--- gs_subset_nodes examples ---
# By node name (character vector)
gs2 <- gs_subset_nodes(gs, c("n1", "n2", "n3"))
# By integer position
gs2 <- gs_subset_nodes(gs, 1:5)
# By predicate (data masking against @nodes columns)
gs2 <- gs_subset_nodes(gs, nodeSize > 5)
# By pre-evaluated logical vector
keep <- gs$nodeSize > 5
gs2 <- gs_subset_nodes(gs, keep)
# Combining with pipes
gs2 <- gs |>
gs_subset_nodes(nodeSize > 5) |>
gs_subset_edges(weight > 0.3)
#--- gs_subset_edges examples ---
# By predicate on an edge attribute
gs3 <- gs_subset_edges(gs, weight > 0.5)
# By endpoint names: 'name1' and 'name2' are columns in '@edges'
# and can be used directly inside any predicate expression
gs3 <- gs_subset_edges(gs, name1 == "n1" & name2 == "n2")
# Combining endpoint and attribute conditions
gs3 <- gs_subset_edges(gs, name1 == "n1" & weight > 0.5)
# By integer position
gs3 <- gs_subset_edges(gs, 1:3)
# By logical vector
gs3 <- gs_subset_edges(gs, gs_edges(gs)$weight > 0.5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.