1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#' Get metrics for a graph
#'
#' @description
#'
#' Get a data frame with metrics for a graph.
#'
#' @inheritParams render_graph
#'
#' @return A data frame containing metrics pertaining to the graph
#'
#' @examples
#' \dontrun{
#' # Import a GML graph file available
#' # in the DiagrammeR package
#' karate_club <-
#' system.file(
#' "extdata", "karate.gml",
#' package = "DiagrammeR") %>%
#' import_graph() %>%
#' set_graph_name("karate")
#'
#' # Display a data frame with
#' # graph information
#' karate_club %>%
#' get_graph_info()
#' }
#'
#' @export
get_graph_info <- function(graph) {
# Validation: Graph object is valid
check_graph_valid(graph)
# Get the graph density
density <-
round(count_nodes(graph) / ((count_nodes(graph) * (count_nodes(graph) - 1)) / 2), 4)
# Get a table of node degree values
degree_table <-
table(c(graph$edges_df$from,
graph$edges_df$to))
# Create a data frame with the graph metrics
data.frame(
name = as.character(graph$graph_info$graph_name),
n = as.integer(count_nodes(graph)),
e = as.integer(count_edges(graph)),
dens = as.numeric(density),
mn_deg = as.integer(min(degree_table)),
mx_deg = as.integer(max(degree_table)),
avg_deg = as.numeric(round(mean(degree_table))),
time = graph$graph_info$graph_time,
tz = as.character(graph$graph_info$graph_tz),
stringsAsFactors = FALSE)
}
|