R/recursive_graph_join.R

Defines functions recursive_graph_join

Documented in recursive_graph_join

#' Recursively join a list of tidygraphs
#'
#' Joins the graphs into a single graph. This recursively implementes
#' \code{tidygraph::graph_join()}
#'
#' @param grs a list of tidygraph objects
#' @param by what to group by (passed to \code{tidygraph::graph_join()})
#'
#' @return a single tidygraph object
#'
#' @examples
#' set.seed(0)
#'
#' gr_list <- purrr::map(c(10, 15, 20), quick_forestfire)
#' gr <- recursive_graph_join(gr_list)
#' gr
#'
#' plot(gr)
#'
#' @export recursive_graph_join
recursive_graph_join <- function(grs, by = "name") {
    if (length(grs) == 1) {
        # base case
        return(grs[[1]])
    } else {
        # recurse
        GR <- tidygraph::graph_join(grs[[1]],
                                    recursive_graph_join(grs[-1], by = by),
                                    by = by)
    }
    return(GR)
}
jhrcook/jhcutils documentation built on Sept. 2, 2020, 7:16 a.m.