R/count_asymmetric_node_pairs.R

Defines functions count_asymmetric_node_pairs

Documented in count_asymmetric_node_pairs

#' Get the number of asymmetrically-connected node pairs
#'
#' @description
#'
#' Get the number of asymmetrically-connected node pairs. This works for
#' directed graphs.
#'
#' @inheritParams render_graph
#'
#' @return A single numeric value representing the number of
#'   asymmetrically-connected node pairs.
#'
#' @examples
#' # Create a cycle graph
#' graph <-
#'   create_graph() %>%
#'   add_cycle(n = 5)
#'
#' # Get a count of asymmetrically-
#' # connected node pairs
#' graph %>%
#'   count_asymmetric_node_pairs()
#'
#' # Create a full graph and then
#' # count the asymmetrically-
#' # connected node pairs
#' create_graph() %>%
#'   add_full_graph(n = 10) %>%
#'   count_asymmetric_node_pairs()
#'
#' @export
count_asymmetric_node_pairs <- function(graph) {

  # Get the name of the function
  fcn_name <- get_calling_fcn()

  # Validation: Graph object is valid
  if (graph_object_valid(graph) == FALSE) {

    emit_error(
      fcn_name = fcn_name,
      reasons = "The graph object is not valid")
  }

  # If the graph is empty, then return NA
  if (nrow(graph$nodes_df) == 0) {
    return(as.numeric(NA))
  }

  # Convert the graph to an igraph object
  ig_graph <- to_igraph(graph)

  # Get the number of asymmetrically-connected node pairs
  # in the graph
  unname(unlist(igraph::dyad_census(ig_graph)["asym"]))
}

Try the DiagrammeR package in your browser

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

DiagrammeR documentation built on May 31, 2023, 6:14 p.m.