R/get_roots.R

Defines functions get_all_roots.DirectedAcyclicGraph get_all_roots.DirectedGraph get_all_roots get_roots_over.DirectedAcyclicGraph get_roots_over.DirectedGraph get_roots_over

Documented in get_all_roots get_roots_over

#' @title Get the root nodes of a graph over some nodes
#'
#' @description
#' Retrieves the nodes in a graph that have no parents
#' over a certain node or group of nodes
#'
#' @param graph A graph object
#' @param nodes A character vector of nodes to find roots for
#' @return A character vector of nodes
#' @export
#' @family analyze graphs
#' @examples
#' graph <- graph_builder() |>
#'   add_path(c("A", "B", "C")) |>
#'   add_path(c("A", "D", "C")) |>
#'   add_path(c("Z", "B", "C")) |>
#'   build_directed()
#'
#' get_roots_over(graph, "D")
get_roots_over <- function(graph, nodes) {
  UseMethod("get_roots_over")
}

#' @export
get_roots_over.DirectedGraph <- function(graph, nodes) {
  throw_if_error(graph$get_roots_over(nodes))
}

#' @export
get_roots_over.DirectedAcyclicGraph <- function(graph, nodes) {
  throw_if_error(graph$get_roots_over(nodes))
}

#' @title Get the all the root nodes of a graph
#'
#' @description
#' Retrieves the nodes in a graph that have no parents
#'
#' @param graph A graph object
#' @param ... Unused
#' @return A character vector of nodes
#' @export
#' @family analyze graphs
#' @examples
#' graph <- graph_builder() |>
#'   add_path(c("A", "B", "C")) |>
#'   add_path(c("A", "D", "C")) |>
#'   add_path(c("Z", "B", "C")) |>
#'   build_directed()
#'
#' get_all_roots(graph)
get_all_roots <- function(graph, ...) {
  UseMethod("get_all_roots")
}

#' @export
get_all_roots.DirectedGraph <- function(graph, ...) {
  throw_if_error(graph$get_all_roots())
}

#' @export
get_all_roots.DirectedAcyclicGraph <- function(graph, ...) {
  throw_if_error(graph$get_all_roots())
}

Try the orbweaver package in your browser

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

orbweaver documentation built on June 8, 2025, 11:40 a.m.