R/make_connections.R

Defines functions make_connections

Documented in make_connections

#' Create a dataframe listing nodes and details of how they connect to other nodes
#'
#' Creates a dataframe of unique nodes and vertices combinations. For example, it might list all team members/nodes working on projects, connected via a project ID.
#'
#' @param data Dataframe holding the information you'd like to reformat
#' @param node_col The \code{data} column containing identifiers for nodes
#' @param connect_col The \code{data} column containing the ids of objects connecting nodes, for example, project IDs, locations, mentees
#' @param weight_col A column from `data` containing quantitative information to set edge width for connections in `make_edges()`. Optional.
#' @return A dataframe containing details of connections between nodes via 'connecting objects'. Will include a unique `node_number` column for use in downstream functions. Note this dataframe can have duplicate node-connecting object rows depending on the nature of the source dataframe.
#' @export

make_connections <- function(
  data,
  node_col,
  connect_col,
  weight_col = NULL
  ) {

  weight_col_quo <- rlang::enquo(weight_col)

  #error checking
  if(!rlang::quo_is_null(weight_col_quo)) {

    if(!is.numeric(dplyr::pull(data, {{weight_col}})))

    stop("`weight_col` must be numeric.")

  }

  data |>
    dplyr::select({{node_col}}, {{connect_col}}, {{weight_col}}) |>
    dplyr::mutate(node_number = match({{node_col}}, unique({{node_col}})))
}
shanej90/nodemaker documentation built on Feb. 22, 2025, 2:39 p.m.