R/make_nodelist.R

Defines functions merge_nodelist make_nodelist

Documented in make_nodelist

make_nodelist <- function(data, node_attributes = NULL, from = "from", to = "to", node = "node") {

  data <- process_from_to_data(data, from, to)

  nodelist <- data.frame(node = unique(c(data[[1]], data[[2]])))

  nodelist <- merge_nodelist(nodelist, node_attributes, node)

  class(nodelist) <- unique(c("netlit_nodelist", class(nodelist)))

  return(nodelist)
}

merge_nodelist <- function(nodelist, node_attributes, node) {
  names(nodelist) <- node

  if (!is_empty(node_attributes)) {

    if (!is.data.frame(node_attributes)) {
      abort("'node_attributes' must be a data frame of node attributes.")
    }
    if (!is_string(node)) {
      abort("'node' must be a string.")
    }
    if (!node %in% names(node_attributes)) {
      if (node == "node") {
        abort("The dataset supplied to 'node_attributes' must have a \"node\" column.")
      }
      else {
        abort("The name supplied to 'node' must be name of a variable in the dataset supplied to 'node_attributes'.")
      }
    }

    nodelist <- merge(nodelist, node_attributes, by = node,
                      all.x = TRUE, sort = FALSE)
  }

  attr(nodelist, "node") <- node

  nodelist
}
judgelord/netlit documentation built on Jan. 3, 2023, 2:42 p.m.