R/review.R

Defines functions print.netlit_review review

Documented in review

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

  #Process data and put from and to at the beginning
  data <- process_from_to_data(data, from, to)

  #Make edgelist; only from, to, and edge attrbiutes
  edgelist <- pare_edgelist(data, edge_attributes)

  #Make a graph from edges to compute edge betweenness
  graph <- igraph::graph_from_data_frame(edgelist, directed = TRUE)

  edgelist$edge_betweenness <- igraph::edge_betweenness(graph)
  class(edgelist) <- unique(c("netlit_edgelist", class(edgelist)))

  #Create nodelist by merging unique nodes with supplied node attrbiutes
  nodelist <- data.frame(node = unique(c(edgelist[[1]], edgelist[[2]])))
  nodelist <- merge_nodelist(nodelist, node_attributes, node)
  class(nodelist) <- unique(c("netlit_nodelist", class(nodelist)))

  #Add graph-based statistics to nodes
  nodelist <- augment_nodelist(nodelist, graph = graph)

  out <- list(edgelist = edgelist,
              nodelist = nodelist,
              graph = igraph::graph_from_data_frame(edgelist, vertices = nodelist, directed = TRUE))

  class(out) <- "netlit_review"

  return(out)
}

print.netlit_review <- function(x, ...) {
  cat("A netlit_review object with the following components:\n\n")

  cat("$edgelist\n")
  cat(paste0(" - ", nrow(x$edgelist), " edges\n"))
  cat(paste0(" - edge attributes: ", if (ncol(x$edgelist) > 2) paste(names(x$edgelist)[-(1:2)], collapse = ", ") else "<none>", "\n"))

  cat("$nodelist\n")
  cat(paste0(" - ", nrow(x$nodelist), " nodes\n"))
  cat(paste0(" - node attributes: ", if (ncol(x$nodelist) > 1) paste(names(x$nodelist)[-1], collapse = ", ") else "<none>", "\n"))

  cat("$graph\n")
  cat("   an igraph object\n")

  invisible(x)
}
judgelord/netlit documentation built on Jan. 3, 2023, 2:42 p.m.