knitr::opts_chunk$set(echo = TRUE)
library(igraph)
library(visNetwork)


Sociogram

File name: r basename(params$gname).

data.frame(Directed = params$gdir,
           Nodes    = vcount(params$graph),
           Edges    = ecount(params$graph)) %>%
      DT::datatable(rownames = FALSE,
                escape   = FALSE,
                width    = "100%",
                options  = list(
                  dom          = "ti",
                  scrollX      = TRUE,
                  ordering     = FALSE,
                  autoWidth    = FALSE,
                  lengthChange = FALSE,
                  searching    = FALSE,
                  bInfo        = FALSE,
                  bPaginate    = TRUE,
                  bFilter      = FALSE
                  )
                )
if ("color" %in% edge_attr_names(params$graph)) {
  edge_color <- edge_attr(params$graph, "color")
} else {
  edge_color <- "slategrey"
}

params$graph %>%
  visIgraph() %>%
  visNodes(size = 10, color = list(
    background = "lightblue",
    border     = edge_color,
    highlight  = list(
      background = "orange",
      border     = "darkred"
    )
  )) %>%
  visEdges(color = edge_color) %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection =list(
    enabled = TRUE,
    style   = 'width: 200px; height: 26px; border:none; outline:none;')) %>%
  visInteraction(navigationButtons = TRUE
  )


Global Network Metrics

data.frame(Measure    = c("Density",
                          "Global Clustering Coefficient",
                          "Size",
                          "Number of Edges",
                          "Degree Centralization"
                          ),
           Score      = c(
             round(graph_attr(params$graph,
                              name = "density"),
                   digits = 3),
             round(graph_attr(params$graph,
                              name = "clustering"),
                   digits = 3),
             graph_attr(params$graph,
                        name = "size"),
             graph_attr(params$graph,
                        name = "edges"),
                round(graph_attr(params$graph, name = "degree_centralization"), digits = 3)
             ),
           Definition = c(
             "Density is formally defined as the total number of observed ties in a network divided by the total possible number of ties in that network. The output ranges from 0 to 1.",
             "The sum of each actor's clustering coefficient divided by the number of actors within the network.",
             "A count of the number of actors in a network.",
             "A count of the number of edges in the network.",
             "The standard measure of centralization uses the variation in actor degree centrality within the network to measure the level of centralization. More variation yields higher network centralization scores, while less variation yields lower scores. Formally, it is the ratio of the actual sum of differences in actor centrality over the theoretical maximum, yielding a score somewhere between 0.0 and 1.0."),
           Caveat      = c(
             "It should not be used to compare networks of different sizes.",
             "",
             "Metric outputs are dependent upon network size. Also, the manner in which an analyst sets an examination's boundaries may not reflect the actual size of a network.",
             "",
             "Many network analysts confuse centrality and centalization."
             ),
           stringsAsFactors = FALSE
    ) %>%
      DT::datatable(rownames = FALSE,
                escape   = FALSE,
                width    = "100%",
                options  = list(
                  dom          = "ti",
                  scrollX      = TRUE,
                  ordering     = FALSE,
                  autoWidth    = FALSE,
                  lengthChange = FALSE,
                  searching    = FALSE,
                  bInfo        = FALSE,
                  bPaginate    = TRUE,
                  bFilter      = FALSE
                  )
                )


Cohesive Subgroups

data.frame(Measure = c("Weak Components",
                       "Number of Cliques",
                       "Max K-Core"),
           Score = c(graph_attr(params$graph,
                                "components"),
                     graph_attr(params$graph,
                                "cliques"),
                     graph_attr(params$graph,
                                "kcore")),
           Definition = c("Subgroups of actors who can reach each other directly or indirectly.",
                          "Maximal number of subsets of three or more where each actor is directly connected to all others.",
                          "A maximal group of actors, all of whom are connected to some number (k) of other group members."),
           Caveat = c("Applies to both undirected and directed data. Often not very useful in a well-connected network.",
                      "Actors can belong to more than one clique, which can often makes identifying distinct subgroups impossible (i.e., where each actor is assigned to one and only one subgroup). Several alternative clique algorithms exist to ease the restrictions that each actor is directly connected to one another.",
                      "Actors of the same core do not necessarily have to be connected to one another."),
               stringsAsFactors = FALSE
    ) %>%
      DT::datatable(rownames = FALSE,
                escape   = FALSE,
                width    = "100%",
                options  = list(
                  dom          = "ti",
                  scrollX      = TRUE,
                  ordering     = FALSE,
                  pageLength   = 3,
                  autoWidth    = FALSE,
                  lengthChange = FALSE,
                  searching    = FALSE,
                  bInfo        = FALSE,
                  bPaginate    = TRUE,
                  bFilter      = FALSE
                ))


Vertex Level Metrics

if (params$gdir) {
  data.frame(
    ID                   = vertex_attr(params$graph, "name"),
    `In Degree`          = vertex_attr(params$graph, "in_degree"),
    `Out Degree`         = vertex_attr(params$graph, "out_degree"),
    `Total Degree`       = vertex_attr(params$graph, "total_degree"),
    Betweenness          = vertex_attr(params$graph, "betweenness"),
    Eigenvector          = vertex_attr(params$graph, "eigenvector"),
    `Reverse Constraint (Ego)` = vertex_attr(params$graph, "rconstraint_ego"),
    `Reverse Constraint (Extended)` = vertex_attr(params$graph, "rconstraint_extended"),
    ARD                  = vertex_attr(params$graph, "ARD"),
    stringsAsFactors = FALSE
    ) %>%
    DT::datatable(rownames = FALSE,
                  escape   = FALSE,
                  width    = "100%",
                  options  = list(
                    dom          = "tilfpr",
                    scrollX      = TRUE,
                    ordering     = TRUE,
                    pageLength   = 10,
                    autoWidth    = FALSE,
                    lengthChange = FALSE,
                    searching    = FALSE,
                    bInfo        = TRUE,
                    bPaginate    = TRUE,
                    bFilter      = FALSE
                    )
    )
} else {
      data.frame(
        ID                   = vertex_attr(params$graph, "name"),
        `Total Degree`       = vertex_attr(params$graph,"total_degree"),
        Betweenness          = vertex_attr(params$graph,"betweenness"),
        Eigenvector          = vertex_attr(params$graph, "eigenvector"),
        `Reverse Constraint (Ego)` = vertex_attr(params$graph, "rconstraint_ego"),
        `Reverse Constraint (Extended)` = vertex_attr(params$graph, "rconstraint_extended"),
        ARD                  = vertex_attr(params$graph, "ARD"),
        stringsAsFactors = FALSE
      ) %>%
  DT::datatable(rownames = FALSE,
                escape   = FALSE,
                width    = "100%",
                options  = list(
                  dom          = "tilfpr",
                  scrollX      = TRUE,
                  ordering     = TRUE,
                  pageLength   = 10,
                  autoWidth    = FALSE,
                  lengthChange = FALSE,
                  searching    = FALSE,
                  bInfo        = TRUE,
                  bPaginate    = TRUE,
                  bFilter      = FALSE
                  )
                ) 
  }


cjcallag/snExplorer documentation built on Dec. 31, 2022, 12:32 p.m.