R/gspace-subset.R

Defines functions .fix_edge_orientation .trim_graph_space .get_simplified_edge_ids .resolve_gs_index gs_subset_edges gs_subset_nodes

Documented in gs_subset_edges gs_subset_nodes

################################################################################
### Filtering methods for GraphSpace objects
################################################################################

#-------------------------------------------------------------------------------
#' @title Filter nodes and edges in a GraphSpace object
#'
#' @description
#' \code{gs_subset_nodes()} retains a subset of nodes and automatically
#' removes any edge whose endpoint is no longer present.
#'
#' \code{gs_subset_edges()} retains a subset of edges without modifying
#' the node set.
#'
#' @param x A \code{\link{GraphSpace}} object.
#' @param i A filter specification. Accepted forms:
#' \itemize{
#'   \item A \strong{character} vector of node names
#'     (\code{gs_subset_nodes()} only; edges are identified by
#'     integer position or predicate, not by name).
#'   \item An \strong{integer} vector of positional indices into the
#'     node or edge table.
#'   \item A \strong{logical} vector whose length must match the number
#'     of nodes or edges, respectively.
#'   \item An \strong{unquoted predicate} evaluated against the node or
#'     edge data frame using data masking, such as \code{nodeSize > 5}
#'     or \code{weight > 0.5}. Column names from the relevant table are
#'     available directly as variables inside the expression.
#' }
#'
#' @details
#'
#' \strong{Node filtering} preserves the normalized coordinate state.
#' Coordinates for surviving nodes remain in their current space
#' (\code{[0, 1]} if normalized, raw coordinates otherwise), so
#' \code{\link{normalizeGraphSpace}} does not need to be re-run.
#' The \code{@graph}, \code{@fdata}, \code{@nodes}, and \code{@edges}
#' slots are all updated consistently. The \code{@canvas} and
#' background image are not modified.
#'
#' \strong{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 \code{curve_weight},
#' \code{is_multiple}, and \code{is_loop} for the remaining members of
#' that group, the full edge table is recomputed from \code{@graph}
#' after deletion.
#'
#' \strong{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 \code{edgeColor}) will match
#' all parallel instances simultaneously, which is usually the intended
#' behavior.
#'
#' @return A \code{\link{GraphSpace}} object with the selected subset
#' of nodes or edges.
#'
#' @seealso
#' \code{\link{cropGraphSpace}}, \code{\link{gs_nodes}},
#' \code{\link{gs_edges}}, \code{\link{normalizeGraphSpace}}
#'
#' @examples
#' 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)
#'
#' @name gs_subset
#' @aliases gs_subset_nodes gs_subset_edges
NULL

#-------------------------------------------------------------------------------
#' @rdname gs_subset
#' @export
gs_subset_nodes <- function(x, i) {
  
  if (!inherits(x, "GraphSpace")) {
    rlang::abort("'x' must be a GraphSpace object.")
  }
  
  if (gs_vcount(x) == 0) {
    rlang::warn("The 'GraphSpace' object has no nodes to filter.")
    return(invisible(x))
  }
  
  if (missing(i)) {
    return(x)
  }
  
  i_quo <- rlang::enquo(i)
  fvars <- all.vars(rlang::quo_get_expr(i_quo))
  nodes <- gs_nodes(x, vars = fvars)
  idx <- .resolve_gs_index(i_quo, data = nodes, what = "node")
  
  # Nothing to do when every node survives in the same order
  if (length(idx) == nrow(nodes) && all(idx == seq_len(nrow(nodes)))) {
    return(x)
  }
  
  if (length(idx) == 0L) {
    rlang::warn(c(
      "No nodes matched the filter expression.",
      "i" = "The returned object contains no nodes or edges."
    ))
  }
  
  # Get nodes without vars: 'nodes' may carry feature columns
  # fetched from @fdata only to evaluate the filter
  nodes_kept <- gs_nodes(x)
  nodes_kept <- nodes_kept[idx, , drop = FALSE]
  
  # .trim_graph_space() prunes @edges, remaps vertex indices,
  # updates @graph, and trims @fdata — all in one consistent pass.
  # @pars (including is.normalized) and @canvas are intentionally
  # left untouched; surviving nodes retain their current coordinates.
  x <- .trim_graph_space(x, nodes_kept)
  
  validObject(x)
  
  x
  
}

#-------------------------------------------------------------------------------
#' @rdname gs_subset
#' @export
gs_subset_edges <- function(x, i) {

  if (!inherits(x, "GraphSpace")) {
    rlang::abort("'x' must be a GraphSpace object.")
  }

  gg <- gs_graph(x)
  
  if (igraph::ecount(gg) == 0L) {
    rlang::warn("The 'GraphSpace' object has no edges to filter.")
    return(invisible(x))
  }
  
  if (missing(i)) {
    return(x)
  }
  
  # Tag each edge with its current id before building edge table.
  # Parallel edges share endpoints, so they can only be told apart
  # by identity; .etag carries that identity into edges.
  igraph::E(gg)$.etag <- seq_len(igraph::ecount(gg))
  edges <- .build_edges(gg)
  
  i_quo <- rlang::enquo(i)
  keep_idx <- .resolve_gs_index(i_quo, data = edges, what = "edge")
  
  # Nothing to do when every edge survives
  if (setequal(keep_idx, seq_len(nrow(edges)))) {
    return(x)
  }

  if (length(keep_idx) == 0L) {
    rlang::warn(c(
      "No edges matched the filter expression.",
      "i" = "The returned object contains no edges."
    ))
  }
  remove_idx <- setdiff(seq_len(nrow(edges)), keep_idx)
  
  if ( .is_simplified(x) && igraph::is_simple(gg) && 
      igraph::is_directed(gg) ) {
    # Simplified directed graphs do not preserve the original edge
    # order, but every edge is unique (no loops or parallels), so
    # removed edges are resolved by their endpoint names.
    edges_removed <- edges[remove_idx, , drop = FALSE]
    edge_ids <- .get_simplified_edge_ids(gg, edges_removed)
  } else {
    # Graphs not subject to simplification, including directed
    # and undirected graphs, preserve the original order from
    # igraph::as_edgelist(gg). Even so, .etag does not rely on that
    # order: it records each edge's id explicitly, so removal stays
    # correct even if the order were ever disturbed.
    edge_ids <- edges$.etag[remove_idx]
  }

  if (length(edge_ids) > 0L) {
    gg <- igraph::delete_edges(gg, edge_ids)
  }
  gg <- igraph::delete_edge_attr(gg, ".etag")
  
  # Rebuild @edges from the updated igraph
  x@edges <- .build_edges(gg)
  x@graph <- gg
    
  validObject(x)
  
  x

}

################################################################################
### Internal helpers
################################################################################

#-------------------------------------------------------------------------------
# Resolve the user-supplied index 'i_quo' (a captured quosure) against a node
# or edge data frame. Returns an integer vector of row positions to KEEP.
# Accepted forms (evaluated with data masking against 'data'):
#--character: matched against data[["name"]] (node table only for now)
#--logical: must have length == nrow(data); NA treated as FALSE
#--integer: unique positions in [1, nrow(data)]
#--predicate: any expression whose result is one of the above
.resolve_gs_index <- function(i_quo, data, what = "node") {

  n <- nrow(data)

  val <- tryCatch(
    rlang::eval_tidy(i_quo, data = data),
    error = function(e) {
      rlang::abort(c(
        x = sprintf(
          "Failed to evaluate the %s filter expression.", what),
        i = conditionMessage(e)
      ))
    }
  )

  #--- language: a quoted call (e.g. quote(name1 == "n1")) passed via [
  # eval_tidy returns the call object itself without further evaluation;
  # re-evaluate it against the data frame to resolve the actual result.
  if (is.language(val)) {
    val <- tryCatch(
      rlang::eval_tidy(val, data = data),
      error = function(e) {
        rlang::abort(c(
          x = sprintf(
            "Failed to evaluate the quoted %s expression.", what),
          i = conditionMessage(e)
        ))
      }
    )
  }

  #--- character: match against the 'name' identifier column
  if (is.character(val)) {

    if (what != "node") {
      rlang::abort(c(
        x = "Character indexing is not supported for edges.",
        i = "Use a logical vector, integer positions, or a predicate expression."
      ))
    }

    id_col <- data[["name"]]
    val_unique <- unique(val)
    idx <- match(val_unique, id_col)
    
    missing_ids <- setdiff(val, id_col)
    if (length(missing_ids) > 0L) {
      rlang::warn(c(
        sprintf(
          "%d node name(s) not found and will be ignored.",
          length(missing_ids)
        ),
        "i" = .gs_preview(missing_ids)
      ))
    }
    idx <- idx[!is.na(idx)]
  #--- logical: must be the same length as the table
  } else if (is.logical(val)) {

    if (length(val) != n) {
      rlang::abort(sprintf(
        "Logical filter length (%d) must equal the number of %ss (%d).",
        length(val), what, n
      ))
    }
    val[is.na(val)] <- FALSE
    idx <- which(val)

  #--- integer / numeric: positional indices
  } else if (is.numeric(val) || is.integer(val)) {

    val    <- as.integer(val)
    na_pos <- is.na(val)

    if (any(na_pos)) {
      rlang::warn(sprintf(
        "%d NA index value(s) removed.", sum(na_pos)
      ))
      val <- val[!na_pos]
    }

    if (length(val) > 0L && (any(val < 1L) || any(val > n))) {
      rlang::abort(sprintf(
        "Integer index out of range: valid positions for %ss are [1, %d].",
        what, n
      ))
    }

    idx <- unique(val)

  #--- anything else is an error
  } else {
    rlang::abort(c(
      x = sprintf(
        "Filter must resolve to a character, logical, or integer vector."),
      i = sprintf("Got an object of class '%s'.", class(val)[1])
    ))
  }

  idx

}

#-------------------------------------------------------------------------------
.get_simplified_edge_ids <- function(g, edges_df) {

  if (nrow(edges_df) == 0L) {
    return(integer(0L))
  }

  if(!igraph::is_simple(g) || !igraph::is_directed(g)){
    rlang::abort("'g' must be a simple directed graph.")
  }

  ids <- lapply(seq_len(nrow(edges_df)), function(i) {
    n1 <- edges_df$name1[i]
    n2 <- edges_df$name2[i]
    fwd <- igraph::get_edge_ids(g, vp = c(n1, n2), error = FALSE)
    # arrowType +/-3 signals a mutual pair: collect the reverse edge too
    at <- edges_df$arrowType[i]
    if (isTRUE(.get_emode(at) == 3L)) {
      bwd <- igraph::get_edge_ids(g, vp = c(n2, n1), error = FALSE)
      res <- c(fwd, bwd)
    } else {
      res <- fwd
    }
    res
  })

  ids <- unique(unlist(ids, use.names = FALSE))
  ids[ids > 0L]

}

#-------------------------------------------------------------------------------
.trim_graph_space <- function(gs, nodes) {
  
  # Re-map vertex index
  nodes$vertex <- seq_len(nrow(nodes))
  gs@nodes <- nodes
  
  # Update graph vertices; incident edges are removed with them
  idx <- V(gs@graph)$name %in% nodes$name
  gg <- igraph::delete_vertices(gs@graph, which(!idx))
  
  # The node index is the igraph vertex order: permute @graph to the
  # re-mapped @nodes order, then rebuild @edges from it
  perm <- match(igraph::V(gg)$name, nodes$name)
  el <- igraph::as_edgelist(gg)
  gg <- igraph::permute(gg, perm)
  gg <- .fix_edge_orientation(gg, el)
  
  # Update graph and edges
  gs@graph <- gg
  gs@edges <- .build_edges(gg)
  
  # Update coords
  if (nrow(gs@coords) > 0) {
    keep <- nodes$name[nodes$name %in% rownames(gs@coords)]
    gs@coords <- gs@coords[keep, , drop = FALSE]
  }
  
  # Update fdata
  if (nrow(gs@fdata) > 0) {
    keep <- nodes$name[nodes$name %in% rownames(gs@fdata)]
    gs@fdata <- gs@fdata[keep, , drop = FALSE]
  }
  
  gs
  
}

#-------------------------------------------------------------------------------
# Undirected igraph edges are stored lowest vertex index first, so permute()
# may reverse some of them. 'el' is the edge list (names) before permute();
# edge order is preserved, so reversed edges are found by name, and their
# arrowType is mirrored to keep each glyph at the same node.
.fix_edge_orientation <- function(g, el) {
  at <- igraph::E(g)$arrowType
  if (igraph::is_directed(g) || nrow(el) == 0 || is.null(at)) return(g)
  flip <- igraph::as_edgelist(g)[, 1] != el[, 1]
  if (!any(flip)) return(g)
  at[flip] <- .mirror_arrowtype(at[flip])
  igraph::E(g)$arrowType <- at
  g
}

Try the RGraphSpace package in your browser

Any scripts or data that you put into this service are public.

RGraphSpace documentation built on Oct. 9, 2026, 9:07 a.m.