R/adv_tbl_graph_from_adv_tbl.R

Defines functions graph_from_adv_tbl

Documented in graph_from_adv_tbl

#' Convert adv_data in an igraph object
#'
#' Converts advanced format network data (a la ORA) into an igraph object
#'
#' @param adv_data adv_tbl data created by \code{\link{adv_tbl()}}
#'
#' @return an igraph graph object
#'
#' @author Chris Friedman, \email{[email protected]@gmail.com}
#' @seealso
#'
#' @examples graph_from_adv_tbl(adv_data)
#'
#' @import  dplyr
#' @importFrom tidyr spread
#' @importFrom igraph graph_from_data_frame
#'
#' @export
#'
graph_from_adv_tbl <- function(adv_data, directed = TRUE, ...){
  if(!"adv_tbl" %in% class(adv_data)) {
    stop("Input is not an adv_data")
  }
  # create the edge list
  adv_edge <- as_adv_edge(adv_data)
  # create the vertex list
  adv_vert <- as_adv_attr(adv_data, ...)
  if("attr_name" %in% colnames(adv_vert)) {
    adv_vert <-
      adv_vert %>%
      select(-attr_type) %>%
      spread(attr_name, attr_value)
  }
  adv_vert <-
    adv_vert %>%
    select(node_name, everything())
  # make the igraph object
  graph_from_data_frame(d = adv_edge,
                        directed = directed,
                        vertices = adv_vert)
}
chris-s-friedman/Friedman documentation built on Aug. 25, 2018, 2:17 p.m.