Diff of /R/GatherGraphNode.R [000000] .. [0f2269]

Switch to unified view

a b/R/GatherGraphNode.R
1
#' Gather graph nodes from a data frame
2
#' Please note that this function is from the 'ggraph' package and has not been altered in functionality,
3
#' but it has been optimized and iterated.
4
#' It is not original content of 'TransProR'.
5
#' However, since 'ggraph' caused frequent GitHub Action errors during the creation of 'TransProR',
6
#' the author directly referenced the involved functions in 'TransProR'.
7
#' This is not the author's original creation. All users please be aware!
8
#' @inheritParams gather_graph_edge
9
#' @param value Column name used for summarizing node size, defaults to the last column
10
#' @return a tibble of graph nodes
11
#' @export
12
#' @importFrom dplyr group_by summarise mutate bind_rows n all_of across
13
#' @importFrom tidyr unite
14
#' @importFrom tibble as_tibble
15
#' @importFrom utils tail
16
gather_graph_node <- function(df, index = NULL, value = utils::tail(colnames(df), 1), root = NULL) {
17
  if (length(index) < 2) {
18
    stop("Please specify at least two index columns.")
19
  } else {
20
    nodes_list <- lapply(seq_along(index), function(i) {
21
      dots <- index[1:i]
22
      df %>%
23
        dplyr::group_by(dplyr::across(dplyr::all_of(dots))) %>%
24
        dplyr::summarise(node.size = sum(.data[[value]], na.rm = TRUE),
25
                         node.level = index[i],
26
                         node.count = dplyr::n(), .groups = 'drop') %>%
27
        dplyr::mutate(node.short_name = as.character(.data[[dots[length(dots)]]]),
28
                      node.branch = as.character(.data[[dots[1]]])) %>%
29
        tidyr::unite(col = "node.name", all_of(dots), sep = "/")
30
    })
31
32
    nodes <- dplyr::bind_rows(nodes_list) %>%
33
      tibble::as_tibble()
34
35
    nodes$node.level <- factor(nodes$node.level, levels = index)
36
37
    if (!is.null(root)) {
38
      root_data <- data.frame(
39
        node.name = root,
40
        node.size = sum(df[[value]], na.rm = TRUE),
41
        node.level = root,
42
        node.count = 1,
43
        node.short_name = root,
44
        node.branch = root,
45
        stringsAsFactors = FALSE
46
      )
47
48
      nodes <- dplyr::bind_rows(root_data, nodes)
49
      nodes$node.level <- factor(nodes$node.level, levels = c(root, index))
50
    }
51
52
    return(nodes)
53
  }
54
}