Skip to content

Commit

Permalink
Refactored object convergence to an internal function convert_to_igra…
Browse files Browse the repository at this point in the history
…ph()
  • Loading branch information
jhollway committed Jan 18, 2021
1 parent 1253ebf commit 72056ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
52 changes: 14 additions & 38 deletions R/centrality.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Centrality for one- and two-mode networks
#' Centrality for one- and two-mode graphs
#'
#' These functions calculate common centrality measures for both one- and two-mode networks.
#' They accept as objects matrices and `igraph` graphs,
Expand All @@ -21,20 +21,12 @@
#' @return Depending on how and what kind of an object is passed to the function,
#' the function will return a `tidygraph` object where the nodes have been updated
#' @export
centrality_degree <- function (object, weights = NULL, mode = "out", loops = TRUE, normalized = FALSE){

# Converge inputs
if(missing(object)){
expect_nodes()
graph <- .G()
weights <- rlang::enquo(weights)
weights <- rlang::eval_tidy(weights, .E())
} else if (is.igraph(object)) {
graph <- object
} else if (is.matrix(object)) {
graph <- igraph::graph_from_incidence_matrix(object)
}
centrality_degree <- function (object,
weights = NULL, mode = "out",
loops = TRUE, normalized = FALSE){

graph <- converge_to_igraph(object)

# Do the calculations
if (is.null(weights)) {
weights <- NA
Expand Down Expand Up @@ -63,19 +55,11 @@ centrality_degree <- function (object, weights = NULL, mode = "out", loops = TRU
#' centrality_closeness(southern_women)
#' mpn_powerelite %>% tidygraph::mutate(closeness = centrality_closeness())
#' @export
centrality_closeness <- function (object, weights = NULL, mode = "out", normalized = FALSE, cutoff = NULL){
centrality_closeness <- function (object,
weights = NULL, mode = "out",
normalized = FALSE, cutoff = NULL){

# Converge inputs
if(missing(object)){
expect_nodes()
graph <- .G()
weights <- rlang::enquo(weights)
weights <- rlang::eval_tidy(weights, .E())
} else if (is.igraph(object)) {
graph <- object
} else if (is.matrix(object)) {
graph <- igraph::graph_from_incidence_matrix(object)
}
graph <- converge_to_igraph(object)

# Do the calculations
if (is.null(weights)) {
Expand Down Expand Up @@ -110,19 +94,11 @@ centrality_closeness <- function (object, weights = NULL, mode = "out", normaliz
#' mpn_powerelite %>% tidygraph::mutate(betweenness = centrality_betweenness())
#' @return A numeric vector giving the betweenness centrality measure of each node.
#' @export
centrality_betweenness <- function(object, weights = NULL, directed = TRUE, cutoff = NULL, nobigint = TRUE, normalized = FALSE){
centrality_betweenness <- function(object,
weights = NULL, directed = TRUE,
cutoff = NULL, nobigint = TRUE, normalized = FALSE){

# Converge inputs
if(missing(object)){
expect_nodes()
graph <- .G()
weights <- rlang::enquo(weights)
weights <- rlang::eval_tidy(weights, .E())
} else if (is.igraph(object)) {
graph <- object
} else if (is.matrix(object)) {
graph <- igraph::graph_from_incidence_matrix(object)
}
graph <- converge_to_igraph(object)

# Do the calculations
if (is.null(weights)) {
Expand Down
17 changes: 16 additions & 1 deletion R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ as_incidence_matrix <- function(df){
}
}
out
}
}

converge_to_igraph <- function(object){
if(missing(object)){
expect_nodes()
graph <- .G()
weights <- rlang::enquo(weights)
weights <- rlang::eval_tidy(weights, .E())
} else if (is.igraph(object)) {
graph <- object
} else if (is.matrix(object)) {
graph <- igraph::graph_from_incidence_matrix(object)
}
graph
}

0 comments on commit 72056ad

Please sign in to comment.