From c6ad21fe5cc8ef72adbc8b521a8424518d18778d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 20 Mar 2023 13:48:16 +0100 Subject: [PATCH 01/16] refactor: create ensure_igraph() --- R/aaa-auto.R | 1 - R/attributes.R | 95 +++++++++++++++------------------------------ R/basic.R | 4 +- R/bipartite.R | 4 +- R/centrality.R | 27 +++++-------- R/centralization.R | 10 ++--- R/cliques.R | 24 +++--------- R/cocitation.R | 10 ++--- R/cohesive.blocks.R | 4 +- R/utils-ensure.R | 5 +++ 10 files changed, 63 insertions(+), 121 deletions(-) create mode 100644 R/utils-ensure.R diff --git a/R/aaa-auto.R b/R/aaa-auto.R index 291d1346dad..be802bd2ae7 100644 --- a/R/aaa-auto.R +++ b/R/aaa-auto.R @@ -2249,4 +2249,3 @@ greedy_vertex_coloring_impl <- function(graph, heuristic=c("colored_neighbors")) } res } - diff --git a/R/attributes.R b/R/attributes.R index 24e4af01bbc..d9b7301809b 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -51,9 +51,7 @@ #' graph_attr(g) #' graph_attr(g, "name") graph_attr <- function(graph, name) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (missing(name)) { graph.attributes(graph) } else { @@ -112,26 +110,20 @@ graph_attr <- function(graph, name) { #' g #' plot(g) set_graph_attr <- function(graph, name, value) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) .Call(C_R_igraph_mybracket3_set, graph, igraph_t_idx_attr, igraph_attr_idx_graph, name, value) } #' @export graph.attributes <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) .Call(C_R_igraph_mybracket2_copy, graph, igraph_t_idx_attr, igraph_attr_idx_graph) } #' @export "graph.attributes<-" <- function(graph, value) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.list(value) || (length(value) > 0 && is.null(names(value))) || any(names(value) == "") || any(duplicated(names(value)))) { stop("Value must be a named list with unique names") @@ -163,9 +155,7 @@ graph.attributes <- function(graph) { #' vertex_attr(g) #' plot(g) vertex_attr <- function(graph, name, index = V(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (missing(name)) { if (missing(index)) { vertex.attributes(graph) @@ -246,9 +236,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { } i_set_vertex_attr <- function(graph, name, index = V(graph), value, check = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(value)) { return(graph) @@ -296,9 +284,7 @@ i_set_vertex_attr <- function(graph, name, index = V(graph), value, check = TRUE #' @export vertex.attributes <- function(graph, index = V(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!missing(index)) { index <- as.igraph.vs(graph, index) @@ -317,9 +303,8 @@ vertex.attributes <- function(graph, index = V(graph)) { #' @export "vertex.attributes<-" <- function(graph, index = V(graph), value) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (!is.list(value) || (length(value) > 0 && is.null(names(value))) || any(names(value) == "") || any(duplicated(names(value)))) { stop("Value must be a named list with unique names") @@ -373,9 +358,8 @@ vertex.attributes <- function(graph, index = V(graph)) { #' g #' plot(g, edge.width = E(g)$weight) edge_attr <- function(graph, name, index = E(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (missing(name)) { if (missing(index)) { edge.attributes(graph) @@ -456,9 +440,7 @@ set_edge_attr <- function(graph, name, index = E(graph), value) { } i_set_edge_attr <- function(graph, name, index = E(graph), value, check = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(value)) { return(graph) @@ -506,9 +488,7 @@ i_set_edge_attr <- function(graph, name, index = E(graph), value, check = TRUE) #' @export edge.attributes <- function(graph, index = E(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!missing(index)) { index <- as.igraph.es(graph, index) @@ -527,9 +507,7 @@ edge.attributes <- function(graph, index = E(graph)) { #' @export "edge.attributes<-" <- function(graph, index = E(graph), value) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.list(value) || (length(value) > 0 && is.null(names(value))) || any(names(value) == "") || any(duplicated(names(value)))) { @@ -574,9 +552,8 @@ edge.attributes <- function(graph, index = E(graph)) { #' g <- make_ring(10) #' graph_attr_names(g) graph_attr_names <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + res <- .Call(C_R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_graph) if (is.null(res)) { res <- character() @@ -600,9 +577,8 @@ graph_attr_names <- function(graph) { #' vertex_attr_names(g) #' plot(g) vertex_attr_names <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + res <- .Call(C_R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) if (is.null(res)) { @@ -626,9 +602,8 @@ vertex_attr_names <- function(graph) { #' edge_attr_names(g) #' plot(g) edge_attr_names <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + res <- .Call(C_R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_edge) if (is.null(res)) { res <- character() @@ -652,9 +627,8 @@ edge_attr_names <- function(graph) { #' g2 <- delete_graph_attr(g, "name") #' graph_attr_names(g2) delete_graph_attr <- function(graph, name) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + name <- as.character(name) if (!name %in% graph_attr_names(graph)) { stop("No such graph attribute: ", name) @@ -683,9 +657,8 @@ delete_graph_attr <- function(graph, name) { #' g2 <- delete_vertex_attr(g, "name") #' vertex_attr_names(g2) delete_vertex_attr <- function(graph, name) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + name <- as.character(name) if (!name %in% vertex_attr_names(graph)) { stop("No such vertex attribute: ", name) @@ -714,9 +687,8 @@ delete_vertex_attr <- function(graph, name) { #' g2 <- delete_edge_attr(g, "name") #' edge_attr_names(g2) delete_edge_attr <- function(graph, name) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + name <- as.character(name) if (!name %in% edge_attr_names(graph)) { stop("No such edge attribute: ", name) @@ -764,9 +736,8 @@ delete_edge_attr <- function(graph, name) { #' neighbors(g, "a") #' is_named <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + "name" %in% vertex_attr_names(graph) } @@ -802,9 +773,8 @@ is_named <- function(graph) { #' shortest_paths(g, 8, 2) #' is_weighted <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + "weight" %in% edge_attr_names(graph) } @@ -812,9 +782,8 @@ is_weighted <- function(graph) { #' @family bipartite #' @export is_bipartite <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + "type" %in% vertex_attr_names(graph) } diff --git a/R/basic.R b/R/basic.R index 93f875ee299..04fa543aea3 100644 --- a/R/basic.R +++ b/R/basic.R @@ -46,9 +46,7 @@ get.edge <- function(graph, id) { "'ends' instead." )) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) id <- as.numeric(id) ec <- ecount(graph) diff --git a/R/bipartite.R b/R/bipartite.R index 86ba96361e1..bc1d5b0c9b8 100644 --- a/R/bipartite.R +++ b/R/bipartite.R @@ -94,9 +94,7 @@ bipartite_projection <- function(graph, types = NULL, which = c("both", "true", "false"), remove.type = TRUE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph) if (!is.null(probe1)) { probe1 <- as.igraph.vs(graph, probe1) - 1 diff --git a/R/centrality.R b/R/centrality.R index 2d43cb4bd44..43b317c5aff 100644 --- a/R/centrality.R +++ b/R/centrality.R @@ -117,9 +117,8 @@ estimate_betweenness <- function(graph, vids = V(graph), directed = TRUE, cutoff #' betweenness <- function(graph, v = V(graph), directed = TRUE, weights = NULL, nobigint = TRUE, normalized = FALSE, cutoff = -1) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + v <- as.igraph.vs(graph, v) directed <- as.logical(directed) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { @@ -161,9 +160,8 @@ betweenness <- function(graph, v = V(graph), directed = TRUE, weights = NULL, edge_betweenness <- function(graph, e = E(graph), directed = TRUE, weights = NULL, cutoff = -1) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + e <- as.igraph.es(graph, e) directed <- as.logical(directed) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { @@ -262,9 +260,8 @@ closeness <- function(graph, vids = V(graph), mode = c("out", "in", "all", "total"), weights = NULL, normalized = FALSE, cutoff = -1) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out" = 1, @@ -1060,9 +1057,7 @@ harmonic_centrality <- harmonic_centrality_impl bonpow.dense <- function(graph, nodes = V(graph), loops = FALSE, exponent = 1, rescale = FALSE, tol = 1e-7) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) d <- as_adj(graph) if (!loops) { @@ -1238,9 +1233,7 @@ power_centrality <- function(graph, nodes = V(graph), alpha.centrality.dense <- function(graph, nodes = V(graph), alpha = 1, loops = FALSE, exo = 1, weights = NULL, tol = 1e-7) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) exo <- rep(exo, length.out = vcount(graph)) exo <- matrix(exo, ncol = 1) @@ -1278,9 +1271,7 @@ alpha.centrality.dense <- function(graph, nodes = V(graph), alpha = 1, alpha.centrality.sparse <- function(graph, nodes = V(graph), alpha = 1, loops = FALSE, exo = 1, weights = NULL, tol = 1e-7) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) vc <- vcount(graph) diff --git a/R/centralization.R b/R/centralization.R index 6487d0cb954..94dd5287eae 100644 --- a/R/centralization.R +++ b/R/centralization.R @@ -153,9 +153,8 @@ centr_degree_tmax <- function(graph = NULL, nodes = 0, mode = c("all", "out", "i warning("centr_degree_tmax() will require an explicit value for its 'loops' argument from igraph 1.4.0. Assuming FALSE now.") } # Argument checks - if (!is.null(graph) && !is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + nodes <- as.integer(nodes) mode <- switch(igraph.match.arg(mode), "out" = 1, @@ -207,9 +206,8 @@ centr_degree_tmax <- function(graph = NULL, nodes = 0, mode = c("all", "out", "i #' centr_eigen(g, directed = FALSE)$centralization centr_betw <- function(graph, directed = TRUE, nobigint = TRUE, normalized = TRUE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + directed <- as.logical(directed) normalized <- as.logical(normalized) diff --git a/R/cliques.R b/R/cliques.R index f7497cc4b34..5d40bb3bc27 100644 --- a/R/cliques.R +++ b/R/cliques.R @@ -108,9 +108,7 @@ largest_cliques <- largest_cliques_impl #' @family cliques #' @export max_cliques <- function(graph, min = NULL, max = NULL, subset = NULL, file = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(min)) { min <- 0 @@ -164,9 +162,7 @@ max_cliques <- function(graph, min = NULL, max = NULL, subset = NULL, file = NUL count_max_cliques <- function(graph, min = NULL, max = NULL, subset = NULL) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(min)) { min <- 0 @@ -325,9 +321,7 @@ weighted_clique_num <- weighted_clique_num_impl #' #' length(maximal_ivs(g)) ivs <- function(graph, min = NULL, max = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(min)) { min <- 0 @@ -354,9 +348,7 @@ ivs <- function(graph, min = NULL, max = NULL) { #' @family cliques #' @export largest_ivs <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) res <- .Call(C_R_igraph_largest_independent_vertex_sets, graph) @@ -372,9 +364,7 @@ largest_ivs <- function(graph) { #' @family cliques #' @export maximal_ivs <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) res <- .Call(C_R_igraph_maximal_independent_vertex_sets, graph) @@ -390,9 +380,7 @@ maximal_ivs <- function(graph) { #' @family cliques #' @export ivs_size <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_independence_number, graph) diff --git a/R/cocitation.R b/R/cocitation.R index 955cc3e6ffa..4dfdba98216 100644 --- a/R/cocitation.R +++ b/R/cocitation.R @@ -57,9 +57,8 @@ #' bibcoupling(g) #' cocitation <- function(graph, v = V(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + v <- as.igraph.vs(graph, v) on.exit(.Call(C_R_igraph_finalizer)) res <- .Call(C_R_igraph_cocitation, graph, v - 1) @@ -73,9 +72,8 @@ cocitation <- function(graph, v = V(graph)) { #' @family cocitation #' @export bibcoupling <- function(graph, v = V(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + v <- as.igraph.vs(graph, v) on.exit(.Call(C_R_igraph_finalizer)) res <- .Call(C_R_igraph_bibcoupling, graph, v - 1) diff --git a/R/cohesive.blocks.R b/R/cohesive.blocks.R index b344b8d3fda..fb2ca169a66 100644 --- a/R/cohesive.blocks.R +++ b/R/cohesive.blocks.R @@ -247,9 +247,7 @@ #' cohesive_blocks <- function(graph, labels = TRUE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) # Function call diff --git a/R/utils-ensure.R b/R/utils-ensure.R new file mode 100644 index 00000000000..39d643b4ebf --- /dev/null +++ b/R/utils-ensure.R @@ -0,0 +1,5 @@ +ensure_igraph <- function(graph) { + if (!is.null(graph) && !is_igraph(graph)) { + cli::cli_abort("Not a graph object.") + } +} From 1e19ae32b39a32da5563fa9eeddbe5bd4ded3a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 20 Mar 2023 13:56:09 +0100 Subject: [PATCH 02/16] same --- R/community.R | 55 +++++---------- R/components.R | 12 +--- R/conversion.R | 40 +++-------- R/decomposition.R | 4 +- R/flow.R | 28 ++------ R/foreign.R | 4 +- R/games.R | 4 +- R/glet.R | 4 +- R/hrg.R | 8 +-- R/interface.R | 44 ++++-------- R/iterators.R | 8 +-- R/layout.R | 70 +++++------------- R/layout_drl.R | 4 +- R/make.R | 4 +- R/minimum.spanning.tree.R | 4 +- R/motifs.R | 12 +--- R/operators.R | 4 +- R/paths.R | 2 +- R/plot.R | 8 +-- R/rewire.R | 12 +--- R/scg.R | 4 +- R/simple.R | 4 +- R/structural.properties.R | 117 ++++++++----------------------- R/structure.info.R | 4 +- R/tkplot.R | 4 +- R/topology.R | 4 +- tests/testthat/test-constraint.R | 4 +- 27 files changed, 125 insertions(+), 347 deletions(-) diff --git a/R/community.R b/R/community.R index b3c5b8d46b6..cdd2ca01545 100644 --- a/R/community.R +++ b/R/community.R @@ -475,9 +475,8 @@ modularity.communities <- function(x, ...) { #' @export modularity_matrix <- function(graph, membership, weights = NULL, resolution = 1, directed = TRUE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (!missing(membership)) { warning("The membership argument is deprecated; modularity_matrix does not need it") } @@ -970,9 +969,7 @@ cluster_spinglass <- function(graph, weights = NULL, vertex = NULL, spins = 25, update.rule = c("config", "random", "simple"), gamma = 1.0, implementation = c("orig", "neg"), gamma.minus = 1.0) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -1128,9 +1125,7 @@ cluster_spinglass <- function(graph, weights = NULL, vertex = NULL, spins = 25, cluster_leiden <- function(graph, objective_function = c("CPM", "modularity"), weights = NULL, resolution_parameter = 1, beta = 0.01, initial_membership = NULL, n_iterations = 2, vertex_weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) # Parse objective function argument objective_function <- igraph.match.arg(objective_function) @@ -1247,9 +1242,8 @@ cluster_leiden <- function(graph, objective_function = c("CPM", "modularity"), #' comms <- cluster_fluid_communities(g, 2) cluster_fluid_communities <- function(graph, no.of.communities) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + no.of.communities <- as.integer(no.of.communities) on.exit(.Call(C_R_igraph_finalizer)) @@ -1322,9 +1316,7 @@ cluster_fluid_communities <- function(graph, no.of.communities) { cluster_walktrap <- function(graph, weights = NULL, steps = 4, merges = TRUE, modularity = TRUE, membership = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object!") - } + ensure_igraph(graph) if (membership && !modularity) { modularity <- TRUE @@ -1451,9 +1443,7 @@ cluster_edge_betweenness <- function(graph, weights = NULL, merges = TRUE, bridges = TRUE, modularity = TRUE, membership = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object!") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -1536,9 +1526,7 @@ cluster_edge_betweenness <- function(graph, weights = NULL, #' cluster_fast_greedy <- function(graph, merges = TRUE, modularity = TRUE, membership = TRUE, weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -1673,9 +1661,8 @@ cluster_leading_eigen <- function(graph, steps = -1, weights = NULL, callback = NULL, extra = NULL, env = parent.frame()) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + steps <- as.integer(steps) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -1770,9 +1757,8 @@ cluster_leading_eigen <- function(graph, steps = -1, weights = NULL, #' cluster_label_prop <- function(graph, weights = NULL, initial = NULL, fixed = NULL) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -1864,9 +1850,8 @@ cluster_label_prop <- function(graph, weights = NULL, initial = NULL, fixed = NU #' cluster_louvain <- function(graph, weights = NULL, resolution = 1) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -1957,9 +1942,8 @@ cluster_louvain <- function(graph, weights = NULL, resolution = 1) { #' @keywords graphs cluster_optimal <- function(graph, weights = NULL) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -2036,9 +2020,8 @@ cluster_optimal <- function(graph, weights = NULL) { cluster_infomap <- function(graph, e.weights = NULL, v.weights = NULL, nb.trials = 10, modularity = TRUE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) + if (is.null(e.weights) && "weight" %in% edge_attr_names(graph)) { e.weights <- E(graph)$weight } diff --git a/R/components.R b/R/components.R index 4aea4623ce1..133f6ce9362 100644 --- a/R/components.R +++ b/R/components.R @@ -26,9 +26,7 @@ #' @family components #' @export count_components <- function(graph, mode = c("weak", "strong")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "weak" = 1, @@ -49,9 +47,7 @@ count_components <- function(graph, mode = c("weak", "strong")) { #' @importFrom graphics hist component_distribution <- function(graph, cumulative = FALSE, mul.size = FALSE, ...) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) cs <- components(graph, ...)$csize hi <- hist(cs, -1:max(cs), plot = FALSE)$density @@ -103,9 +99,7 @@ component_distribution <- function(graph, cumulative = FALSE, mul.size = FALSE, #' decompose <- function(graph, mode = c("weak", "strong"), max.comps = NA, min.vertices = 0) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "weak" = 1, diff --git a/R/conversion.R b/R/conversion.R index 9ab68859714..329946be12d 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -21,9 +21,7 @@ get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"), attr = NULL, edges = FALSE, names = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) type <- igraph.match.arg(type) type <- switch(type, @@ -96,9 +94,7 @@ get.adjacency.dense <- function(graph, type = c("both", "upper", "lower"), get.adjacency.sparse <- function(graph, type = c("both", "upper", "lower"), attr = NULL, edges = FALSE, names = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) type <- igraph.match.arg(type) @@ -212,9 +208,7 @@ get.adjacency.sparse <- function(graph, type = c("both", "upper", "lower"), as_adjacency_matrix <- function(graph, type = c("both", "upper", "lower"), attr = NULL, edges = FALSE, names = TRUE, sparse = igraph_opt("sparsematrices")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!missing(edges)) { warning("The `edges` argument of `as_adjacency_matrix` is deprecated; it will be removed in igraph 1.4.0") @@ -258,9 +252,7 @@ as_adj <- as_adjacency_matrix #' @family conversion #' @export as_edgelist <- function(graph, names = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) res <- matrix(.Call(C_R_igraph_get_edgelist, graph, TRUE), ncol = 2 @@ -366,9 +358,7 @@ as.directed <- as.directed_impl #' @export as.undirected <- function(graph, mode = c("collapse", "each", "mutual"), edge.attr.comb = igraph_opt("edge.attr.comb")) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "collapse" = 1, "each" = 0, @@ -429,9 +419,7 @@ as_adj_list <- function(graph, mode = c("all", "out", "in", "total"), loops = c("twice", "once", "ignore"), multiple = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- as.numeric(switch(mode, @@ -469,9 +457,7 @@ as_adj_list <- function(graph, as_adj_edge_list <- function(graph, mode = c("all", "out", "in", "total"), loops = c("twice", "once", "ignore")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- as.numeric(switch(mode, @@ -639,9 +625,7 @@ graph_from_graphnel <- function(graphNEL, name = TRUE, weight = TRUE, #' @family conversion #' @export as_graphnel <- function(graph) { - if (!is_igraph(graph)) { - stop("Not an igraph graph") - } + ensure_igraph(graph) if (any_multiple(graph)) { stop("multiple edges are not supported in graphNEL graphs") @@ -862,9 +846,7 @@ get.incidence.sparse <- function(graph, types, names, attr) { as_incidence_matrix <- function(graph, types = NULL, attr = NULL, names = TRUE, sparse = FALSE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph) names <- as.logical(names) @@ -998,9 +980,7 @@ graph_from_adj_list <- graph_from_adj_list_impl #' ) #' as_long_data_frame(g) as_long_data_frame <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) ver <- .Call(C_R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) class(ver) <- "data.frame" diff --git a/R/decomposition.R b/R/decomposition.R index 6215653a045..a8c630e51a8 100644 --- a/R/decomposition.R +++ b/R/decomposition.R @@ -87,9 +87,7 @@ #' is_chordal <- function(graph, alpha = NULL, alpham1 = NULL, fillin = FALSE, newgraph = FALSE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(alpha)) { alpha <- as.numeric(alpha) - 1 } diff --git a/R/flow.R b/R/flow.R index 1977df4d51e..039796ba3a9 100644 --- a/R/flow.R +++ b/R/flow.R @@ -80,9 +80,7 @@ #' @family flow #' @export min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value.only = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(capacity)) { if ("capacity" %in% edge_attr_names(graph)) { capacity <- E(graph)$capacity @@ -223,9 +221,7 @@ min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value. #' cohesion(g) #' vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (length(source) == 0) { source <- NULL @@ -315,9 +311,7 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR #' adhesion(g) #' edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (length(source) == 0) { source <- NULL @@ -343,9 +337,7 @@ edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE #' @family flow #' @export edge_disjoint_paths <- function(graph, source, target) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (length(source) == 0) { source <- NULL @@ -364,9 +356,7 @@ edge_disjoint_paths <- function(graph, source, target) { #' @family flow #' @export vertex_disjoint_paths <- function(graph, source = NULL, target = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (length(source) == 0) { source <- NULL @@ -385,9 +375,7 @@ vertex_disjoint_paths <- function(graph, source = NULL, target = NULL) { #' @family flow #' @export adhesion <- function(graph, checks = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_adhesion, graph, as.logical(checks)) @@ -555,9 +543,7 @@ st_min_cuts <- st_min_cuts_impl #' @export dominator_tree <- function(graph, root, mode = c("out", "in", "all", "total")) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) root <- as.igraph.vs(graph, root) if (length(root) == 0) { stop("No vertex was specified") diff --git a/R/foreign.R b/R/foreign.R index 48da13ba546..1d67aebbf0f 100644 --- a/R/foreign.R +++ b/R/foreign.R @@ -173,9 +173,7 @@ write_graph <- function(graph, file, "graphml", "dimacs", "gml", "dot", "leda" ), ...) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.character(file) || length(grep("://", file, fixed = TRUE)) > 0 || length(grep("~", file, fixed = TRUE)) > 0) { tmpfile <- TRUE diff --git a/R/games.R b/R/games.R index 1f6afd50163..97d3fc8d283 100644 --- a/R/games.R +++ b/R/games.R @@ -1073,9 +1073,7 @@ asym_pref <- function(...) constructor_spec(sample_asym_pref, ...) #' @export #' @family functions for manipulating graph structure connect <- function(graph, order, mode = c("all", "out", "in", "total")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, diff --git a/R/glet.R b/R/glet.R index 67acdce893e..c4ccda95631 100644 --- a/R/glet.R +++ b/R/glet.R @@ -80,9 +80,7 @@ #' @export graphlet_basis <- function(graph, weights = NULL) { ## Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } diff --git a/R/hrg.R b/R/hrg.R index af4521eafc1..92aad6fe499 100644 --- a/R/hrg.R +++ b/R/hrg.R @@ -114,9 +114,7 @@ NULL #' @family hierarchical random graph functions fit_hrg <- function(graph, hrg = NULL, start = FALSE, steps = 0) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(hrg)) { hrg <- list( left = c(), right = c(), prob = c(), edges = c(), @@ -282,9 +280,7 @@ sample_hrg <- sample_hrg_impl predict_edges <- function(graph, hrg = NULL, start = FALSE, num.samples = 10000, num.bins = 25) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(hrg)) { hrg <- list( left = c(), right = c(), prob = c(), edges = c(), diff --git a/R/interface.R b/R/interface.R index 02d626e7826..62713d67844 100644 --- a/R/interface.R +++ b/R/interface.R @@ -62,9 +62,7 @@ #' E(g)[[]] #' plot(g) add_edges <- function(graph, edges, ..., attr = list()) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) attrs <- list(...) attrs <- append(attrs, attr) @@ -128,9 +126,7 @@ add_edges <- function(graph, edges, ..., attr = list()) { #' V(g)[[]] #' plot(g) add_vertices <- function(graph, nv, ..., attr = list()) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) attrs <- list(...) attrs <- append(attrs, attr) @@ -186,9 +182,7 @@ add_vertices <- function(graph, nv, ..., attr = list()) { #' g <- delete_edges(g, get.edge.ids(g, c(1, 5, 4, 5))) #' g delete_edges <- function(graph, edges) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_delete_edges, graph, as.igraph.es(graph, edges) - 1) } @@ -214,9 +208,7 @@ delete_edges <- function(graph, edges) { #' g2 #' V(g2) delete_vertices <- function(graph, v) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_delete_vertices, graph, as.igraph.vs(graph, v) - 1) } @@ -246,9 +238,7 @@ delete_vertices <- function(graph, v) { #' vapply(gsize, 0) %>% #' hist() gsize <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_ecount, graph) } @@ -277,9 +267,7 @@ ecount <- gsize #' n34 <- neighbors(g, 34) #' intersection(n1, n34) neighbors <- function(graph, v, mode = c("out", "in", "all", "total")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.character(mode)) { mode <- igraph.match.arg(mode) mode <- switch(mode, @@ -319,9 +307,7 @@ neighbors <- function(graph, v, mode = c("out", "in", "all", "total")) { #' incident(g, 1) #' incident(g, 34) incident <- function(graph, v, mode = c("all", "out", "in", "total")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is_directed(graph)) { mode <- igraph.match.arg(mode) mode <- switch(mode, @@ -361,9 +347,7 @@ incident <- function(graph, v, mode = c("all", "out", "in", "total")) { #' g2 <- make_ring(10, directed = TRUE) #' is_directed(g2) is_directed <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_is_directed, graph) } @@ -385,9 +369,7 @@ is_directed <- function(graph) { #' g <- make_ring(5) #' ends(g, E(g)) ends <- function(graph, es, names = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) es2 <- as.igraph.es(graph, na.omit(es)) - 1 res <- matrix(NA_integer_, ncol = length(es), nrow = 2) @@ -470,9 +452,7 @@ get.edges <- function(graph, es) { #' E(g)[eim] #' get.edge.ids <- function(graph, vp, directed = TRUE, error = FALSE, multi = FALSE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call( C_R_igraph_get_eids, graph, as.igraph.vs(graph, vp) - 1, @@ -520,7 +500,7 @@ vcount <- gorder_impl #' adjacent_vertices(g, c(1, 34)) adjacent_vertices <- function(graph, v, mode = c("out", "in", "all", "total")) { - if (!is_igraph(graph)) stop("Not a graph object") + ensure_igraph(graph) vv <- as.igraph.vs(graph, v) - 1 mode <- switch(match.arg(mode), @@ -563,7 +543,7 @@ adjacent_vertices <- function(graph, v, #' incident_edges(g, c(1, 34)) incident_edges <- function(graph, v, mode = c("out", "in", "all", "total")) { - if (!is_igraph(graph)) stop("Not a graph object") + ensure_igraph(graph) vv <- as.igraph.vs(graph, v) - 1 mode <- switch(match.arg(mode), diff --git a/R/iterators.R b/R/iterators.R index ae6d548b401..3b9db16e6c1 100644 --- a/R/iterators.R +++ b/R/iterators.R @@ -205,9 +205,7 @@ set_complete_iterator <- function(x, value = TRUE) { #' set_vertex_attr("name", value = letters[1:10]) #' V(g2) V <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) update_vs_ref(graph) @@ -304,9 +302,7 @@ unsafe_create_es <- function(graph, idx, es = NULL) { #' set_vertex_attr("name", value = letters[1:10]) #' E(g2) E <- function(graph, P = NULL, path = NULL, directed = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) update_es_ref(graph) diff --git a/R/layout.R b/R/layout.R index 0aa213fead8..046f62990dd 100644 --- a/R/layout.R +++ b/R/layout.R @@ -283,9 +283,7 @@ normalize <- function(xmin = -1, xmax = 1, ymin = xmin, ymax = xmax, layout_as_bipartite <- function(graph, types = NULL, hgap = 1, vgap = 1, maxiter = 100) { ## Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph) hgap <- as.numeric(hgap) vgap <- as.numeric(vgap) @@ -341,9 +339,7 @@ as_bipartite <- function(...) layout_spec(layout_as_bipartite, ...) #' layout_(g, as_star()) layout_as_star <- function(graph, center = V(graph)[1], order = NULL) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (vcount(graph) == 0) { # Any other layout will do so just pick one that supports graphs with no # vertices @@ -431,9 +427,7 @@ as_star <- function(...) layout_spec(layout_as_star, ...) layout_as_tree <- function(graph, root = numeric(), circular = FALSE, rootlevel = numeric(), mode = c("out", "in", "all"), flip.y = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) root <- as.igraph.vs(graph, root) - 1 circular <- as.logical(circular) rootlevel <- as.double(rootlevel) @@ -503,9 +497,7 @@ layout.reingold.tilford <- function(..., params = list()) { #' V(karate)$shape <- "none" #' plot(karate, layout = coords) layout_in_circle <- function(graph, order = V(graph)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) order <- as.igraph.vs(graph, order) - 1L on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_layout_circle, graph, order) @@ -666,9 +658,7 @@ nicely <- function(...) layout_spec(layout_nicely, ...) #' } layout_on_grid <- function(graph, width = 0, height = 0, dim = 2) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) width <- as.integer(width) dim <- as.integer(dim) stopifnot(dim == 2 || dim == 3) @@ -703,9 +693,7 @@ layout.grid.3d <- function(graph, width = 0, height = 0) { "igraph 0.8.0, please use layout_on_grid instead" )) # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) width <- as.integer(width) height <- as.integer(height) @@ -738,9 +726,7 @@ layout.grid.3d <- function(graph, width = 0, height = 0) { #' @export #' @family graph layouts layout_on_sphere <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_layout_sphere, graph) } @@ -778,9 +764,7 @@ layout.sphere <- function(..., params = list()) { #' @export #' @family graph layouts layout_randomly <- function(graph, dim = 2) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (dim == 2) { on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_layout_random, graph) @@ -924,9 +908,7 @@ layout_with_dh <- function(graph, coords = NULL, maxiter = 10, weight.edge.crossings = 1.0 - sqrt(edge_density(graph)), weight.node.edge.dist = 0.2 * (1 - edge_density(graph))) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(coords)) { coords <- as.matrix(structure(as.double(coords), dim = dim(coords))) use.seed <- TRUE @@ -1049,9 +1031,7 @@ layout_with_fr <- function(graph, coords = NULL, dim = 2, minz = NULL, maxz = NULL, coolexp, maxdelta, area, repulserad, maxiter) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(coords)) { coords <- as.matrix(structure(as.double(coords), dim = dim(coords))) } @@ -1174,9 +1154,7 @@ layout_with_gem <- function(graph, coords = NULL, maxiter = 40 * vcount(graph)^2 temp.max = max(vcount(graph), 1), temp.min = 1 / 10, temp.init = sqrt(max(vcount(graph), 1))) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(coords)) { coords <- as.matrix(structure(as.double(coords), dim = dim(coords))) use.seed <- TRUE @@ -1256,9 +1234,7 @@ with_gem <- function(...) layout_spec(layout_with_gem, ...) layout_with_graphopt <- function(graph, start = NULL, niter = 500, charge = 0.001, mass = 30, spring.length = 0, spring.constant = 1, max.sa.movement = 5) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(start)) { start <- structure(as.numeric(start), dim = dim(start)) } @@ -1358,9 +1334,7 @@ layout_with_kk <- function(graph, coords = NULL, dim = 2, } if (!missing(start)) coords <- start - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(coords)) { coords <- as.matrix(structure(as.double(coords), dim = dim(coords))) } @@ -1466,9 +1440,7 @@ layout_with_lgl <- function(graph, maxiter = 150, maxdelta = vcount(graph), area = vcount(graph)^2, coolexp = 1.5, repulserad = area * vcount(graph), cellsize = sqrt(sqrt(area)), root = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(root)) { root <- -1 } else { @@ -1514,7 +1486,7 @@ layout.lgl <- function(..., params = list()) { #' #' Warning: If the graph is symmetric to the exchange of two vertices (as is the #' case with leaves of a tree connecting to the same parent), classical -#' multidimensional scaling may assign the same coordinates to these vertices. +#' multidimensional scaling may assign the same coordinates to these vertices. #' #' This function generates the layout separately for each graph component and #' then merges them via [merge_coords()]. @@ -1546,9 +1518,7 @@ layout.lgl <- function(..., params = list()) { layout_with_mds <- function(graph, dist = NULL, dim = 2, options = arpack_defaults) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(dist)) dist <- structure(as.double(dist), dim = dim(dist)) dim <- as.integer(dim) @@ -1775,9 +1745,7 @@ layout_with_sugiyama <- function(graph, layers = NULL, hgap = 1, vgap = 1, maxiter = 100, weights = NULL, attributes = c("default", "all", "none")) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!is.null(layers)) layers <- as.numeric(layers) - 1 hgap <- as.numeric(hgap) vgap <- as.numeric(vgap) @@ -2033,9 +2001,7 @@ norm_coords <- function(layout, xmin = -1, xmax = 1, ymin = -1, ymax = 1, #' @param graph The input graph. #' @export layout_components <- function(graph, layout = layout_with_kk, ...) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) V(graph)$id <- seq(vcount(graph)) gl <- decompose(graph) diff --git a/R/layout_drl.R b/R/layout_drl.R index 6906537386a..3c4ee393cf8 100644 --- a/R/layout_drl.R +++ b/R/layout_drl.R @@ -91,9 +91,7 @@ layout_with_drl <- function(graph, use.seed = FALSE, weights = NULL, fixed = NULL, dim = 2) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (dim != 2 && dim != 3) { stop("`dim' must be 2 or 3") diff --git a/R/make.R b/R/make.R index 15276afcc40..a07e5254ed3 100644 --- a/R/make.R +++ b/R/make.R @@ -1382,9 +1382,7 @@ chordal_ring <- function(...) constructor_spec(make_chordal_ring, ...) #' #' @export make_line_graph <- function(graph) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) res <- .Call(C_R_igraph_linegraph, graph) diff --git a/R/minimum.spanning.tree.R b/R/minimum.spanning.tree.R index 8a6910e9d6a..85d651a68bf 100644 --- a/R/minimum.spanning.tree.R +++ b/R/minimum.spanning.tree.R @@ -64,9 +64,7 @@ #' mst <- function(graph, weights = NULL, algorithm = NULL, ...) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(algorithm)) { if (!is.null(weights) || "weight" %in% edge_attr_names(graph)) { diff --git a/R/motifs.R b/R/motifs.R index b0ee7ba4ee1..8f6c4664d7d 100644 --- a/R/motifs.R +++ b/R/motifs.R @@ -52,9 +52,7 @@ #' count_motifs(g, 3) #' sample_motifs(g, 3) motifs <- function(graph, size = 3, cut.prob = rep(0, size)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) cut.prob <- as.numeric(cut.prob) if (length(cut.prob) != size) { cut.prob <- c( @@ -98,9 +96,7 @@ motifs <- function(graph, size = 3, cut.prob = rep(0, size)) { #' count_motifs(g, 3) #' sample_motifs(g, 3) count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) cut.prob <- as.numeric(cut.prob) if (length(cut.prob) != size) { cut.prob <- c( @@ -149,9 +145,7 @@ count_motifs <- function(graph, size = 3, cut.prob = rep(0, size)) { #' sample_motifs(g, 3) sample_motifs <- function(graph, size = 3, cut.prob = rep(0, size), sample.size = vcount(graph) / 10, sample = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) cut.prob <- as.numeric(cut.prob) if (length(cut.prob) != size) { cut.prob <- c( diff --git a/R/operators.R b/R/operators.R index 3ed14b74cc1..5f2992018c2 100644 --- a/R/operators.R +++ b/R/operators.R @@ -604,9 +604,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) { #' graph.isomorphic(gu, make_full_graph(vcount(g))) #' complementer <- function(graph, loops = FALSE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_complementer, graph, as.logical(loops)) } diff --git a/R/paths.R b/R/paths.R index ff682ebeb49..a8b0547fa94 100644 --- a/R/paths.R +++ b/R/paths.R @@ -59,7 +59,7 @@ all_simple_paths <- function(graph, from, to = V(graph), mode = c("out", "in", "all", "total"), cutoff = -1) { ## Argument checks - if (!is_igraph(graph)) stop("Not a graph object") + ensure_igraph(graph) from <- as.igraph.vs(graph, from) to <- as.igraph.vs(graph, to) mode <- switch(igraph.match.arg(mode), diff --git a/R/plot.R b/R/plot.R index 0911eed5741..7dfd423f01a 100644 --- a/R/plot.R +++ b/R/plot.R @@ -92,9 +92,7 @@ plot.igraph <- function(x, mark.expand = 15, loop.size = 1, ...) { graph <- x - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) vc <- vcount(graph) @@ -543,9 +541,7 @@ rglplot <- function(x, ...) { #' @export rglplot.igraph <- function(x, ...) { graph <- x - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) create.edge <- function(v1, v2, r1, r2, ec, ew, am, as) { ## these could also be parameters: diff --git a/R/rewire.R b/R/rewire.R index 37d1b1339bb..67c00a854cc 100644 --- a/R/rewire.R +++ b/R/rewire.R @@ -82,9 +82,7 @@ keeping_degseq <- function(loops = FALSE, niter = 100) { } rewire_keeping_degseq <- function(graph, loops, niter) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) loops <- as.logical(loops) mode <- if (loops) 1 else 0 @@ -154,9 +152,7 @@ each_edge <- function(prob, loops = FALSE, multiple = FALSE, mode = c("all", "ou } rewire_each_edge <- function(graph, prob, loops, multiple) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call( C_R_igraph_rewire_edges, graph, as.numeric(prob), as.logical(loops), @@ -165,9 +161,7 @@ rewire_each_edge <- function(graph, prob, loops, multiple) { } rewire_each_directed_edge <- function(graph, prob, loops, mode) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call( C_R_igraph_rewire_directed_edges, graph, as.numeric(prob), as.logical(loops), diff --git a/R/scg.R b/R/scg.R index 19e16d9f0c8..261c66b7a03 100644 --- a/R/scg.R +++ b/R/scg.R @@ -105,9 +105,7 @@ NULL #' stochastic_matrix <- function(graph, column.wise = FALSE, sparse = igraph_opt("sparsematrices")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) column.wise <- as.logical(column.wise) if (length(column.wise) != 1) { diff --git a/R/simple.R b/R/simple.R index 1150036c75f..abddc9c510a 100644 --- a/R/simple.R +++ b/R/simple.R @@ -87,9 +87,7 @@ is_simple <- is_simple_impl #' @rdname simplify simplify_and_colorize <- function(graph) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) # Function call diff --git a/R/structural.properties.R b/R/structural.properties.R index 80ddc181ec4..1c60e490fcf 100644 --- a/R/structural.properties.R +++ b/R/structural.properties.R @@ -78,9 +78,7 @@ #' get_diameter(g, weights = NA) #' diameter <- function(graph, directed = TRUE, unconnected = TRUE, weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -102,9 +100,7 @@ diameter <- function(graph, directed = TRUE, unconnected = TRUE, weights = NULL) #' @export get_diameter <- function(graph, directed = TRUE, unconnected = TRUE, weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -132,9 +128,7 @@ get_diameter <- function(graph, directed = TRUE, unconnected = TRUE, #' @export farthest_vertices <- function(graph, directed = TRUE, unconnected = TRUE, weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -204,9 +198,7 @@ mean_distance <- mean_distance_impl degree <- function(graph, v = V(graph), mode = c("all", "out", "in", "total"), loops = TRUE, normalized = FALSE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) v <- as.igraph.vs(graph, v) mode <- igraph.match.arg(mode) mode <- switch(mode, @@ -237,9 +229,7 @@ degree <- function(graph, v = V(graph), #' @export #' @importFrom graphics hist degree_distribution <- function(graph, cumulative = FALSE, ...) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) cs <- degree(graph, ...) hi <- hist(cs, -1:max(cs), plot = FALSE)$density if (!cumulative) { @@ -431,9 +421,7 @@ distances <- function(graph, v = V(graph), to = V(graph), "automatic", "unweighted", "dijkstra", "bellman-ford", "johnson" )) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) # make sure that the lower-level function in C gets mode == "out" # unconditionally when the graph is undirected; this is used for @@ -519,9 +507,7 @@ shortest_paths <- function(graph, from, to = V(graph), output = c("vpath", "epath", "both"), predecessors = FALSE, inbound.edges = FALSE, algorithm = c("automatic", "unweighted", "dijkstra", "bellman-ford")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -613,9 +599,7 @@ all_shortest_paths <- function(graph, from, to = V(graph), mode = c("out", "all", "in"), weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -686,9 +670,7 @@ all_shortest_paths <- function(graph, from, #' subcomponent(g, 1, "out") #' subcomponent(g, 1, "all") subcomponent <- function(graph, v, mode = c("all", "out", "in")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -759,9 +741,7 @@ subgraph <- function(graph, vids) { #' @export induced_subgraph <- function(graph, vids, impl = c("auto", "copy_and_delete", "create_from_scratch")) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) impl <- switch(igraph.match.arg(impl), "auto" = 0, @@ -784,9 +764,7 @@ induced_subgraph <- function(graph, vids, impl = c("auto", "copy_and_delete", "c #' @export subgraph.edges <- function(graph, eids, delete.vertices = TRUE) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) eids <- as.igraph.es(graph, eids) delete.vertices <- as.logical(delete.vertices) @@ -902,9 +880,7 @@ transitivity <- function(graph, type = c( "barrat", "weighted" ), vids = NULL, weights = NULL, isolates = c("NaN", "zero")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) type <- igraph.match.arg(type) type <- switch(type, "undirected" = 0, @@ -1012,9 +988,7 @@ transitivity <- function(graph, type = c( #' constraint(g) #' constraint <- function(graph, nodes = V(graph), weights = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) nodes <- as.igraph.vs(graph, nodes) if (is.null(weights)) { @@ -1069,9 +1043,7 @@ constraint <- function(graph, nodes = V(graph), weights = NULL) { #' reciprocity <- function(graph, ignore.loops = TRUE, mode = c("default", "ratio")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "default" = 0, "ratio" = 1 @@ -1124,9 +1096,7 @@ reciprocity <- function(graph, ignore.loops = TRUE, #' edge_density(simplify(g), loops = FALSE) # this is also right, but different #' edge_density <- function(graph, loops = FALSE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call(C_R_igraph_density, graph, as.logical(loops)) @@ -1137,9 +1107,7 @@ edge_density <- function(graph, loops = FALSE) { #' @export ego_size <- function(graph, order = 1, nodes = V(graph), mode = c("all", "out", "in"), mindist = 0) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -1228,9 +1196,7 @@ ego_size <- function(graph, order = 1, nodes = V(graph), #' ego <- function(graph, order = 1, nodes = V(graph), mode = c("all", "out", "in"), mindist = 0) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -1259,9 +1225,7 @@ ego <- function(graph, order = 1, nodes = V(graph), #' @export make_ego_graph <- function(graph, order = 1, nodes = V(graph), mode = c("all", "out", "in"), mindist = 0) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -1318,9 +1282,7 @@ make_ego_graph <- function(graph, order = 1, nodes = V(graph), #' coreness(g) # small core triangle in a ring #' coreness <- function(graph, mode = c("all", "out", "in")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -1369,9 +1331,7 @@ coreness <- function(graph, mode = c("all", "out", "in")) { #' topo_sort(g) #' topo_sort <- function(graph, mode = c("out", "all", "in")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- igraph.match.arg(mode) mode <- switch(mode, "out" = 1, @@ -1469,9 +1429,7 @@ feedback_arc_set <- feedback_arc_set_impl #' girth(g) #' girth <- function(graph, circle = TRUE) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) res <- .Call(C_R_igraph_girth, graph, as.logical(circle)) if (igraph_opt("return.vs.es") && circle) { @@ -1657,9 +1615,7 @@ bfs <- function(graph, root, mode = c("out", "in", "all", "total"), pred = FALSE, succ = FALSE, dist = FALSE, callback = NULL, extra = NULL, rho = parent.frame(), neimode) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (!missing(neimode)) { warning("Argument `neimode' is deprecated; use `mode' instead") @@ -1822,10 +1778,7 @@ dfs <- function(graph, root, mode = c("out", "in", "all", "total"), order = TRUE, order.out = FALSE, father = FALSE, dist = FALSE, in.callback = NULL, out.callback = NULL, extra = NULL, rho = parent.frame(), neimode) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } - + ensure_igraph(graph) if (!missing(neimode)) { warning("Argument `neimode' is deprecated; use `mode' instead") if (missing(mode)) { @@ -1929,9 +1882,7 @@ dfs <- function(graph, root, mode = c("out", "in", "all", "total"), #' components <- function(graph, mode = c("weak", "strong")) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "weak" = 1, "strong" = 2 @@ -1992,9 +1943,7 @@ count_components <- count_components #' unfold_tree <- function(graph, mode = c("all", "out", "in", "total"), roots) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "out" = 1, "in" = 2, @@ -2054,9 +2003,7 @@ unfold_tree <- function(graph, mode = c("all", "out", "in", "total"), roots) { laplacian_matrix <- function(graph, normalized = FALSE, weights = NULL, sparse = igraph_opt("sparsematrices")) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) normalized <- as.logical(normalized) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -2172,9 +2119,7 @@ laplacian_matrix <- function(graph, normalized = FALSE, weights = NULL, #' @export is_matching <- function(graph, matching, types = NULL) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph, required = F) matching <- as.igraph.vs(graph, matching, na.ok = TRUE) - 1 matching[is.na(matching)] <- -1 @@ -2191,9 +2136,7 @@ is_matching <- function(graph, matching, types = NULL) { #' @rdname matching is_max_matching <- function(graph, matching, types = NULL) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph, required = F) matching <- as.igraph.vs(graph, matching, na.ok = TRUE) - 1 matching[is.na(matching)] <- -1 @@ -2211,9 +2154,7 @@ is_max_matching <- function(graph, matching, types = NULL) { max_bipartite_match <- function(graph, types = NULL, weights = NULL, eps = .Machine$double.eps) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight diff --git a/R/structure.info.R b/R/structure.info.R index 5deb6ae44ec..f64c81171c4 100644 --- a/R/structure.info.R +++ b/R/structure.info.R @@ -46,9 +46,7 @@ #' are_adjacent(ug, 1, 2) #' are_adjacent(ug, 2, 1) are_adjacent <- function(graph, v1, v2) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) on.exit(.Call(C_R_igraph_finalizer)) .Call( C_R_igraph_are_connected, graph, as.igraph.vs(graph, v1) - 1, diff --git a/R/tkplot.R b/R/tkplot.R index 31bc949fe3a..162451630ce 100644 --- a/R/tkplot.R +++ b/R/tkplot.R @@ -167,9 +167,7 @@ assign(".next", 1, .tkplot.env) #' } #' tkplot <- function(graph, canvas.width = 450, canvas.height = 450, ...) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) # Libraries requireNamespace("tcltk", quietly = TRUE) || diff --git a/R/topology.R b/R/topology.R index 4e551d06274..4ff8c8eaf6a 100644 --- a/R/topology.R +++ b/R/topology.R @@ -147,9 +147,7 @@ graph.get.subisomorphisms.vf2 <- function(graph1, graph2, vertex.color1, #' @export graph.isoclass.subgraph <- function(graph, vids) { # Argument checks - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) - 1 on.exit(.Call(C_R_igraph_finalizer)) diff --git a/tests/testthat/test-constraint.R b/tests/testthat/test-constraint.R index df2551470c3..4408033b15c 100644 --- a/tests/testthat/test-constraint.R +++ b/tests/testthat/test-constraint.R @@ -1,8 +1,6 @@ test_that("constraint works", { constraint.orig <- function(graph, nodes = V(graph), attr = NULL) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) idx <- degree(graph) != 0 A <- as_adj(graph, attr = attr, sparse = FALSE) A <- A[idx, idx] From 745adbaf3dce68c485e6f625dea9b64b109307a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 20 Mar 2023 14:02:32 +0100 Subject: [PATCH 03/16] add test --- tests/testthat/_snaps/utils-ensure.md | 4 ++++ tests/testthat/test-utils-ensure.R | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 tests/testthat/_snaps/utils-ensure.md create mode 100644 tests/testthat/test-utils-ensure.R diff --git a/tests/testthat/_snaps/utils-ensure.md b/tests/testthat/_snaps/utils-ensure.md new file mode 100644 index 00000000000..831cf36da25 --- /dev/null +++ b/tests/testthat/_snaps/utils-ensure.md @@ -0,0 +1,4 @@ +# ensure_igraph() works + + Not a graph object. + diff --git a/tests/testthat/test-utils-ensure.R b/tests/testthat/test-utils-ensure.R new file mode 100644 index 00000000000..5b747c129cd --- /dev/null +++ b/tests/testthat/test-utils-ensure.R @@ -0,0 +1,5 @@ +test_that("ensure_igraph() works", { + expect_snapshot_error(ensure_igraph(1)) + expect_silent(ensure_igraph(NULL)) + expect_silent(ensure_igraph(make_empty_graph())) +}) From 56ce728c510218abbb8e7cd2ae8a222f7f942729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 20 Mar 2023 14:02:59 +0100 Subject: [PATCH 04/16] add dep on cli --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 3c044d2ecf0..bab9ee00105 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,6 +6,7 @@ Description: Routines for simple graphs and network analysis. It can and regular graphs, graph visualization, centrality methods and much more. Depends: methods Imports: + cli, graphics, grDevices, magrittr, From a01fe4155060a1927d3749c4d34382980a9296c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 20 Mar 2023 14:13:52 +0100 Subject: [PATCH 05/16] more --- R/aaa-auto.R | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/R/aaa-auto.R b/R/aaa-auto.R index be802bd2ae7..dd2980e8a10 100644 --- a/R/aaa-auto.R +++ b/R/aaa-auto.R @@ -241,7 +241,7 @@ hsbm_list_game_impl <- function(n, mlist, rholist, Clist, p) { sample_correlated_gnp_impl <- function(old.graph, corr, p=edge_density(old.graph), permutation=NULL) { # Argument checks - if (!is_igraph(old.graph)) { stop("Not a graph object") } + ensure_igraph(old.graph) corr <- as.numeric(corr) p <- as.numeric(p) if (!is.null(permutation)) permutation <- as.numeric(permutation)-1 @@ -1636,8 +1636,8 @@ graph.isoclass_impl <- function(graph) { graph.isomorphic_impl <- function(graph1, graph2) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) on.exit( .Call(C_R_igraph_finalizer) ) # Function call @@ -1661,8 +1661,8 @@ graph_from_isomorphism_class_impl <- function(size, number, directed=TRUE) { graph.isomorphic.vf2_impl <- function(graph1, graph2, vertex.color1, vertex.color2, edge.color1, edge.color2) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(vertex.color1)) { if ("color" %in% vertex_attr_names(graph1)) { vertex.color1 <- V(graph1)$color @@ -1713,8 +1713,8 @@ graph.isomorphic.vf2_impl <- function(graph1, graph2, vertex.color1, vertex.colo graph.count.isomorphisms.vf2_impl <- function(graph1, graph2, vertex.color1, vertex.color2, edge.color1, edge.color2) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(vertex.color1)) { if ("color" %in% vertex_attr_names(graph1)) { vertex.color1 <- V(graph1)$color @@ -1765,8 +1765,8 @@ graph.count.isomorphisms.vf2_impl <- function(graph1, graph2, vertex.color1, ver graph.subisomorphic.vf2_impl <- function(graph1, graph2, vertex.color1, vertex.color2, edge.color1, edge.color2) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(vertex.color1)) { if ("color" %in% vertex_attr_names(graph1)) { vertex.color1 <- V(graph1)$color @@ -1817,8 +1817,8 @@ graph.subisomorphic.vf2_impl <- function(graph1, graph2, vertex.color1, vertex.c graph.count.subisomorphisms.vf2_impl <- function(graph1, graph2, vertex.color1, vertex.color2, edge.color1, edge.color2) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(vertex.color1)) { if ("color" %in% vertex_attr_names(graph1)) { vertex.color1 <- V(graph1)$color @@ -1869,8 +1869,8 @@ graph.count.subisomorphisms.vf2_impl <- function(graph1, graph2, vertex.color1, graph.isomorphic.34_impl <- function(graph1, graph2) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) on.exit( .Call(C_R_igraph_finalizer) ) # Function call @@ -1915,8 +1915,8 @@ permute_impl <- function(graph, permutation) { graph.isomorphic.bliss_impl <- function(graph1, graph2, colors1, colors2, sh=c("fm", "f", "fs", "fl", "flm", "fsm")) { # Argument checks - if (!is_igraph(graph1)) { stop("Not a graph object") } - if (!is_igraph(graph2)) { stop("Not a graph object") } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(colors1)) { if ("color" %in% vertex_attr_names(graph1)) { colors1 <- V(graph1)$color From ae3c4d577a20a7d4ff062ff0a7d5f05146320dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 30 Mar 2023 12:53:09 +0200 Subject: [PATCH 06/16] refactor --- R/utils-ensure.R | 13 +++++++++++-- tools/stimulus/types-RR.yaml | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/R/utils-ensure.R b/R/utils-ensure.R index 39d643b4ebf..d51663cee34 100644 --- a/R/utils-ensure.R +++ b/R/utils-ensure.R @@ -1,5 +1,14 @@ ensure_igraph <- function(graph) { - if (!is.null(graph) && !is_igraph(graph)) { - cli::cli_abort("Not a graph object.") + + if (rlang::is_missing(graph)) { + cli::cli_abort("Must provide a graph object (missing argument).") + } + + if (is.null(graph)) { + cli::cli_abort("Must provide a graph object (provided {.code NULL}).") + } + + if (!is_igraph(graph)) { + cli::cli_abort("Must provide a graph object (provided wrong object type).") } } diff --git a/tools/stimulus/types-RR.yaml b/tools/stimulus/types-RR.yaml index 17193d117f0..84a1d10c2da 100644 --- a/tools/stimulus/types-RR.yaml +++ b/tools/stimulus/types-RR.yaml @@ -1,9 +1,9 @@ GRAPH: - INCONV: if (!is_igraph(%I%)) { stop("Not a graph object") } + INCONV: ensure_igraph(%I%) GRAPH_OR_0: - INCONV: if (!is.null(graph) && !is_igraph(%I%)) { stop("Not a graph object") } + INCONV: ensure_igraph(%I%) INTEGER: DEFAULT: From 57f120b5d2858b71c3d0fea2d0ec7fa1a99e77a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 30 Mar 2023 12:57:59 +0200 Subject: [PATCH 07/16] refactor --- R/utils-ensure.R | 2 +- tests/testthat/_snaps/utils-ensure.md | 10 +++++++++- tests/testthat/test-utils-ensure.R | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/R/utils-ensure.R b/R/utils-ensure.R index d51663cee34..69b1f9d3710 100644 --- a/R/utils-ensure.R +++ b/R/utils-ensure.R @@ -5,7 +5,7 @@ ensure_igraph <- function(graph) { } if (is.null(graph)) { - cli::cli_abort("Must provide a graph object (provided {.code NULL}).") + cli::cli_abort("Must provide a graph object (provided {.code NULL}).") } if (!is_igraph(graph)) { diff --git a/tests/testthat/_snaps/utils-ensure.md b/tests/testthat/_snaps/utils-ensure.md index 831cf36da25..69b6ac09d8c 100644 --- a/tests/testthat/_snaps/utils-ensure.md +++ b/tests/testthat/_snaps/utils-ensure.md @@ -1,4 +1,12 @@ # ensure_igraph() works - Not a graph object. + Must provide a graph object (provided wrong object type). + +--- + + Must provide a graph object (provided wrong object type). + +--- + + Must provide a graph object (provided `NULL`). diff --git a/tests/testthat/test-utils-ensure.R b/tests/testthat/test-utils-ensure.R index 5b747c129cd..2b9b23d51fa 100644 --- a/tests/testthat/test-utils-ensure.R +++ b/tests/testthat/test-utils-ensure.R @@ -1,5 +1,6 @@ test_that("ensure_igraph() works", { expect_snapshot_error(ensure_igraph(1)) - expect_silent(ensure_igraph(NULL)) + expect_snapshot_error(ensure_igraph(NA)) + expect_snapshot_error(ensure_igraph(NULL)) expect_silent(ensure_igraph(make_empty_graph())) }) From 3b838170f4033d0caee576fd7bc3cd93c3b639ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:08:41 +0200 Subject: [PATCH 08/16] optional = TRUE --- R/aaa-auto.R | 6 +++--- R/centralization.R | 2 +- R/utils-ensure.R | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/aaa-auto.R b/R/aaa-auto.R index da249123dfc..1d75153e3c6 100644 --- a/R/aaa-auto.R +++ b/R/aaa-auto.R @@ -735,7 +735,7 @@ centralization_betweenness_impl <- function(graph, directed=TRUE, normalized=TRU centralization_betweenness_tmax_impl <- function(graph=NULL, nodes=0, directed=TRUE) { # Argument checks - if (!is.null(graph) && !is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph, optional = TRUE) nodes <- as.integer(nodes) directed <- as.logical(directed) @@ -761,7 +761,7 @@ centralization_closeness_impl <- function(graph, mode=c("out", "in", "all", "tot centralization_closeness_tmax_impl <- function(graph=NULL, nodes=0, mode=c("out", "in", "all", "total")) { # Argument checks - if (!is.null(graph) && !is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph, optional = TRUE) nodes <- as.integer(nodes) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) @@ -789,7 +789,7 @@ centralization_eigenvector_centrality_impl <- function(graph, directed=FALSE, sc centralization_eigenvector_centrality_tmax_impl <- function(graph=NULL, nodes=0, directed=FALSE, scale=TRUE) { # Argument checks - if (!is.null(graph) && !is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph, optional = TRUE) nodes <- as.integer(nodes) directed <- as.logical(directed) scale <- as.logical(scale) diff --git a/R/centralization.R b/R/centralization.R index 700c8170d8d..4e07a41d908 100644 --- a/R/centralization.R +++ b/R/centralization.R @@ -153,7 +153,7 @@ centr_degree_tmax <- function(graph = NULL, nodes = 0, mode = c("all", "out", "i warning("centr_degree_tmax() will require an explicit value for its 'loops' argument from igraph 1.4.0. Assuming FALSE now.") } # Argument checks - ensure_igraph(graph) + ensure_igraph(graph, optional = TRUE) nodes <- as.integer(nodes) mode <- switch(igraph.match.arg(mode), diff --git a/R/utils-ensure.R b/R/utils-ensure.R index 69b1f9d3710..d4d607f65a6 100644 --- a/R/utils-ensure.R +++ b/R/utils-ensure.R @@ -1,10 +1,10 @@ -ensure_igraph <- function(graph) { +ensure_igraph <- function(graph, optional = FALSE) { if (rlang::is_missing(graph)) { cli::cli_abort("Must provide a graph object (missing argument).") } - if (is.null(graph)) { + if (!optional && is.null(graph)) { cli::cli_abort("Must provide a graph object (provided {.code NULL}).") } From f19ff1d735780e60e37f118245bdc9b8fc09c9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:09:18 +0200 Subject: [PATCH 09/16] for auto --- tools/stimulus/types-RR.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/stimulus/types-RR.yaml b/tools/stimulus/types-RR.yaml index f410e401a7c..13d8ecf4a9c 100644 --- a/tools/stimulus/types-RR.yaml +++ b/tools/stimulus/types-RR.yaml @@ -3,7 +3,7 @@ GRAPH: INCONV: ensure_igraph(%I%) GRAPH_OR_0: - INCONV: ensure_igraph(%I%) + INCONV: ensure_igraph(%I%, optional = TRUE) INTEGER: DEFAULT: From c06b525bbed78cb95938928efbc1783906b26d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:14:10 +0200 Subject: [PATCH 10/16] rm duplicate code cc @krlmlr --- R/attributes.R | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index b935b9321d7..71274306aeb 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -118,10 +118,6 @@ set_graph_attr <- function(graph, name, value) { #' @export graph.attributes <- function(graph) { ensure_igraph(graph) - .Call(C_R_igraph_mybracket2_copy, graph, igraph_t_idx_attr, igraph_attr_idx_graph) - if (!is_igraph(graph)) { - stop("Not a graph object") - } .Call(R_igraph_mybracket2_copy, graph, igraph_t_idx_attr, igraph_attr_idx_graph) } @@ -557,11 +553,6 @@ edge.attributes <- function(graph, index = E(graph)) { #' graph_attr_names(g) graph_attr_names <- function(graph) { ensure_igraph(graph) - - res <- .Call(C_R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_graph) - if (!is_igraph(graph)) { - stop("Not a graph object") - } res <- .Call(R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_graph) if (is.null(res)) { res <- character() @@ -587,10 +578,6 @@ graph_attr_names <- function(graph) { vertex_attr_names <- function(graph) { ensure_igraph(graph) - res <- .Call(C_R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) - if (!is_igraph(graph)) { - stop("Not a graph object") - } res <- .Call(R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) if (is.null(res)) { @@ -615,11 +602,6 @@ vertex_attr_names <- function(graph) { #' plot(g) edge_attr_names <- function(graph) { ensure_igraph(graph) - - res <- .Call(C_R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_edge) - if (!is_igraph(graph)) { - stop("Not a graph object") - } res <- .Call(R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_edge) if (is.null(res)) { res <- character() From 0a9948c47e082b7dd76e5389d6b03aec54d43f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:14:39 +0200 Subject: [PATCH 11/16] use ensure in aaa-auto too --- R/aaa-auto.R | 174 +++++++++++++++++++++++++-------------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/R/aaa-auto.R b/R/aaa-auto.R index 1d75153e3c6..71cebbcfd39 100644 --- a/R/aaa-auto.R +++ b/R/aaa-auto.R @@ -2,7 +2,7 @@ vcount_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -328,7 +328,7 @@ sample_dirichlet_impl <- function(n, alpha) { harmonic_centrality_cutoff_impl <- function(graph, vids=V(graph), mode=c("out", "in", "all", "total"), weights=NULL, normalized=FALSE, cutoff=-1) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { @@ -353,7 +353,7 @@ harmonic_centrality_cutoff_impl <- function(graph, vids=V(graph), mode=c("out", personalized_pagerank_impl <- function(graph, algo=c("prpack", "arpack"), vids=V(graph), directed=TRUE, damping=0.85, personalized=NULL, weights=NULL, options=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) algo <- switch(igraph.match.arg(algo), "arpack"=1L, "prpack"=2L) vids <- as.igraph.vs(graph, vids) directed <- as.logical(directed) @@ -388,7 +388,7 @@ personalized_pagerank_impl <- function(graph, algo=c("prpack", "arpack"), vids=V reverse_edges_impl <- function(graph, eids=E(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) eids <- as.igraph.es(graph, eids) on.exit( .Call(R_igraph_finalizer) ) @@ -400,7 +400,7 @@ reverse_edges_impl <- function(graph, eids=E(graph)) { average_path_length_dijkstra_impl <- function(graph, weights=NULL, directed=TRUE, unconnected=TRUE, details=FALSE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -423,7 +423,7 @@ average_path_length_dijkstra_impl <- function(graph, weights=NULL, directed=TRUE path_length_hist_impl <- function(graph, directed=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) directed <- as.logical(directed) on.exit( .Call(R_igraph_finalizer) ) @@ -435,7 +435,7 @@ path_length_hist_impl <- function(graph, directed=TRUE) { simplify_impl <- function(graph, remove.multiple=TRUE, remove.loops=TRUE, edge.attr.comb=igraph_opt("edge.attr.comb")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) remove.multiple <- as.logical(remove.multiple) remove.loops <- as.logical(remove.loops) edge.attr.comb <- igraph.i.attribute.combination(edge.attr.comb) @@ -449,7 +449,7 @@ simplify_impl <- function(graph, remove.multiple=TRUE, remove.loops=TRUE, edge.a feedback_arc_set_impl <- function(graph, weights=NULL, algo=c("approx_eades", "exact_ip")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -471,7 +471,7 @@ feedback_arc_set_impl <- function(graph, weights=NULL, algo=c("approx_eades", "e is_loop_impl <- function(graph, eids=E(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) eids <- as.igraph.es(graph, eids) on.exit( .Call(R_igraph_finalizer) ) @@ -483,7 +483,7 @@ is_loop_impl <- function(graph, eids=E(graph)) { is_dag_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -494,7 +494,7 @@ is_dag_impl <- function(graph) { is_simple_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -505,7 +505,7 @@ is_simple_impl <- function(graph) { is_multiple_impl <- function(graph, eids=E(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) eids <- as.igraph.es(graph, eids) on.exit( .Call(R_igraph_finalizer) ) @@ -517,7 +517,7 @@ is_multiple_impl <- function(graph, eids=E(graph)) { has_loop_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -528,7 +528,7 @@ has_loop_impl <- function(graph) { has_multiple_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -539,7 +539,7 @@ has_multiple_impl <- function(graph) { count_multiple_impl <- function(graph, eids=E(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) eids <- as.igraph.es(graph, eids) on.exit( .Call(R_igraph_finalizer) ) @@ -551,7 +551,7 @@ count_multiple_impl <- function(graph, eids=E(graph)) { eigenvector_centrality_impl <- function(graph, directed=FALSE, scale=TRUE, weights=NULL, options=arpack_defaults) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) directed <- as.logical(directed) scale <- as.logical(scale) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { @@ -575,7 +575,7 @@ eigenvector_centrality_impl <- function(graph, directed=FALSE, scale=TRUE, weigh hub_score_impl <- function(graph, scale=TRUE, weights=NULL, options=arpack_defaults) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) scale <- as.logical(scale) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -598,7 +598,7 @@ hub_score_impl <- function(graph, scale=TRUE, weights=NULL, options=arpack_defau authority_score_impl <- function(graph, scale=TRUE, weights=NULL, options=arpack_defaults) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) scale <- as.logical(scale) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -621,7 +621,7 @@ authority_score_impl <- function(graph, scale=TRUE, weights=NULL, options=arpack is_mutual_impl <- function(graph, eids=E(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) eids <- as.igraph.es(graph, eids) on.exit( .Call(R_igraph_finalizer) ) @@ -633,7 +633,7 @@ is_mutual_impl <- function(graph, eids=E(graph)) { maximum_cardinality_search_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -646,7 +646,7 @@ maximum_cardinality_search_impl <- function(graph) { avg_nearest_neighbor_degree_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", "total"), neighbor.degree.mode=c("all", "out", "in", "total"), weights=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) neighbor.degree.mode <- switch(igraph.match.arg(neighbor.degree.mode), "out"=1, "in"=2, "all"=3, "total"=3) @@ -670,7 +670,7 @@ avg_nearest_neighbor_degree_impl <- function(graph, vids=V(graph), mode=c("all", strength_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", "total"), loops=TRUE, weights=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) loops <- as.logical(loops) @@ -708,7 +708,7 @@ centralization_impl <- function(scores, theoretical.max=0, normalized=TRUE) { centralization_degree_impl <- function(graph, mode=c("all", "out", "in", "total"), loops=TRUE, normalized=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) loops <- as.logical(loops) normalized <- as.logical(normalized) @@ -722,7 +722,7 @@ centralization_degree_impl <- function(graph, mode=c("all", "out", "in", "total" centralization_betweenness_impl <- function(graph, directed=TRUE, normalized=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) directed <- as.logical(directed) normalized <- as.logical(normalized) @@ -748,7 +748,7 @@ centralization_betweenness_tmax_impl <- function(graph=NULL, nodes=0, directed=T centralization_closeness_impl <- function(graph, mode=c("out", "in", "all", "total"), normalized=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) normalized <- as.logical(normalized) @@ -774,7 +774,7 @@ centralization_closeness_tmax_impl <- function(graph=NULL, nodes=0, mode=c("out" centralization_eigenvector_centrality_impl <- function(graph, directed=FALSE, scale=TRUE, options=arpack_defaults, normalized=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) directed <- as.logical(directed) scale <- as.logical(scale) options.tmp <- arpack_defaults; options.tmp[ names(options) ] <- options ; options <- options.tmp @@ -803,7 +803,7 @@ centralization_eigenvector_centrality_tmax_impl <- function(graph=NULL, nodes=0, assortativity_nominal_impl <- function(graph, types, directed=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) types <- as.numeric(types)-1 directed <- as.logical(directed) @@ -816,7 +816,7 @@ assortativity_nominal_impl <- function(graph, types, directed=TRUE) { assortativity_impl <- function(graph, types1, types2=NULL, directed=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) types1 <- as.numeric(types1) if (!is.null(types2)) types2 <- as.numeric(types2) directed <- as.logical(directed) @@ -830,7 +830,7 @@ assortativity_impl <- function(graph, types1, types2=NULL, directed=TRUE) { assortativity_degree_impl <- function(graph, directed=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) directed <- as.logical(directed) on.exit( .Call(R_igraph_finalizer) ) @@ -842,7 +842,7 @@ assortativity_degree_impl <- function(graph, directed=TRUE) { contract_vertices_impl <- function(graph, mapping, vertex.attr.comb=igraph_opt("vertex.attr.comb")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mapping <- as.numeric(mapping)-1 vertex.attr.comb <- igraph.i.attribute.combination(vertex.attr.comb) @@ -855,7 +855,7 @@ contract_vertices_impl <- function(graph, mapping, vertex.attr.comb=igraph_opt(" eccentricity_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", "total")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) @@ -870,7 +870,7 @@ eccentricity_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", " radius_impl <- function(graph, mode=c("all", "out", "in", "total")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) on.exit( .Call(R_igraph_finalizer) ) @@ -882,7 +882,7 @@ radius_impl <- function(graph, mode=c("all", "out", "in", "total")) { diversity_impl <- function(graph, weights=NULL, vids=V(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -904,7 +904,7 @@ diversity_impl <- function(graph, weights=NULL, vids=V(graph)) { random_walk_impl <- function(graph, start, steps, mode=c("out", "in", "all", "total"), stuck=c("return", "error")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) start <- as.igraph.vs(graph, start) if (length(start) == 0) { stop("No vertex was specified") @@ -924,7 +924,7 @@ random_walk_impl <- function(graph, start, steps, mode=c("out", "in", "all", "to random_edge_walk_impl <- function(graph, start, steps, weights=NULL, mode=c("out", "in", "all", "total"), stuck=c("return", "error")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -952,7 +952,7 @@ random_edge_walk_impl <- function(graph, start, steps, weights=NULL, mode=c("out global_efficiency_impl <- function(graph, weights=NULL, directed=TRUE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -972,7 +972,7 @@ global_efficiency_impl <- function(graph, weights=NULL, directed=TRUE) { local_efficiency_impl <- function(graph, vids=V(graph), weights=NULL, directed=TRUE, mode=c("all", "out", "in", "total")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -996,7 +996,7 @@ local_efficiency_impl <- function(graph, vids=V(graph), weights=NULL, directed=T average_local_efficiency_impl <- function(graph, weights=NULL, directed=TRUE, mode=c("all", "out", "in", "total")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -1031,7 +1031,7 @@ is_graphical_impl <- function(out.deg, in.deg=NULL, allowed.edge.types=c("simple bipartite_projection_size_impl <- function(graph, types=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) types <- handle_vertex_type_arg(types, graph) on.exit( .Call(R_igraph_finalizer) ) @@ -1043,7 +1043,7 @@ bipartite_projection_size_impl <- function(graph, types=NULL) { is_bipartite_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1054,7 +1054,7 @@ is_bipartite_impl <- function(graph) { is_connected_impl <- function(graph, mode=c("weak", "strong")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "weak"=1, "strong"=2) on.exit( .Call(R_igraph_finalizer) ) @@ -1066,7 +1066,7 @@ is_connected_impl <- function(graph, mode=c("weak", "strong")) { articulation_points_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1079,7 +1079,7 @@ articulation_points_impl <- function(graph) { biconnected_components_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1110,7 +1110,7 @@ biconnected_components_impl <- function(graph) { bridges_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1123,7 +1123,7 @@ bridges_impl <- function(graph) { cliques_impl <- function(graph, min=0, max=0) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) min <- as.integer(min) max <- as.integer(max) @@ -1141,7 +1141,7 @@ cliques_impl <- function(graph, min=0, max=0) { clique_size_hist_impl <- function(graph, min.size=0, max.size=0) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) min.size <- as.integer(min.size) max.size <- as.integer(max.size) @@ -1154,7 +1154,7 @@ clique_size_hist_impl <- function(graph, min.size=0, max.size=0) { largest_cliques_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1170,7 +1170,7 @@ largest_cliques_impl <- function(graph) { maximal_cliques_hist_impl <- function(graph, min.size=0, max.size=0) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) min.size <- as.integer(min.size) max.size <- as.integer(max.size) @@ -1183,7 +1183,7 @@ maximal_cliques_hist_impl <- function(graph, min.size=0, max.size=0) { clique_number_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1194,7 +1194,7 @@ clique_number_impl <- function(graph) { weighted_cliques_impl <- function(graph, vertex.weights=NULL, min.weight=0, max.weight=0, maximal=FALSE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(vertex.weights) && "weight" %in% vertex_attr_names(graph)) { vertex.weights <- V(graph)$weight } @@ -1221,7 +1221,7 @@ weighted_cliques_impl <- function(graph, vertex.weights=NULL, min.weight=0, max. largest_weighted_cliques_impl <- function(graph, vertex.weights=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(vertex.weights) && "weight" %in% vertex_attr_names(graph)) { vertex.weights <- V(graph)$weight } @@ -1245,7 +1245,7 @@ largest_weighted_cliques_impl <- function(graph, vertex.weights=NULL) { weighted_clique_number_impl <- function(graph, vertex.weights=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(vertex.weights) && "weight" %in% vertex_attr_names(graph)) { vertex.weights <- V(graph)$weight } @@ -1264,7 +1264,7 @@ weighted_clique_number_impl <- function(graph, vertex.weights=NULL) { similarity_jaccard_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", "total"), loops=FALSE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) loops <- as.logical(loops) @@ -1278,7 +1278,7 @@ similarity_jaccard_impl <- function(graph, vids=V(graph), mode=c("all", "out", " similarity_dice_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", "total"), loops=FALSE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) loops <- as.logical(loops) @@ -1292,7 +1292,7 @@ similarity_dice_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in" similarity_inverse_log_weighted_impl <- function(graph, vids=V(graph), mode=c("all", "out", "in", "total")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) @@ -1337,7 +1337,7 @@ hrg_dendrogram_impl <- function(hrg) { hrg_consensus_impl <- function(graph, hrg=NULL, start=FALSE, num.samples=10000) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(hrg)) { hrg <- list(left=c(), right=c(), prob=c(), edges=c(), vertices=c()) } @@ -1354,7 +1354,7 @@ hrg_consensus_impl <- function(graph, hrg=NULL, start=FALSE, num.samples=10000) hrg_create_impl <- function(graph, prob) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) prob <- as.numeric(prob) on.exit( .Call(R_igraph_finalizer) ) @@ -1367,7 +1367,7 @@ hrg_create_impl <- function(graph, prob) { graphlets_impl <- function(graph, weights=NULL, niter=1000) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } @@ -1392,7 +1392,7 @@ graphlets_impl <- function(graph, weights=NULL, niter=1000) { to_directed_impl <- function(graph, mode=c("mutual", "arbitrary", "random", "acyclic")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "arbitrary"=0, "mutual"=1, "random"=2, "acyclic"=3) on.exit( .Call(R_igraph_finalizer) ) @@ -1404,7 +1404,7 @@ to_directed_impl <- function(graph, mode=c("mutual", "arbitrary", "random", "acy dyad_census_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1415,7 +1415,7 @@ dyad_census_impl <- function(graph) { triad_census_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1426,7 +1426,7 @@ triad_census_impl <- function(graph) { adjacent_triangles_impl <- function(graph, vids=V(graph)) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vids <- as.igraph.vs(graph, vids) on.exit( .Call(R_igraph_finalizer) ) @@ -1438,7 +1438,7 @@ adjacent_triangles_impl <- function(graph, vids=V(graph)) { list_triangles_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1451,7 +1451,7 @@ list_triangles_impl <- function(graph) { maxflow_impl <- function(graph, source, target, capacity=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) source <- as.igraph.vs(graph, source) if (length(source) == 0) { stop("No vertex was specified") @@ -1483,7 +1483,7 @@ maxflow_impl <- function(graph, source, target, capacity=NULL) { dominator_tree_impl <- function(graph, root, mode=c("out", "in", "all", "total")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) root <- as.igraph.vs(graph, root) if (length(root) == 0) { stop("No vertex was specified") @@ -1501,7 +1501,7 @@ dominator_tree_impl <- function(graph, root, mode=c("out", "in", "all", "total") all_st_cuts_impl <- function(graph, source, target) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) source <- as.igraph.vs(graph, source) if (length(source) == 0) { stop("No vertex was specified") @@ -1531,7 +1531,7 @@ all_st_cuts_impl <- function(graph, source, target) { all_st_mincuts_impl <- function(graph, source, target, capacity=NULL) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) source <- as.igraph.vs(graph, source) if (length(source) == 0) { stop("No vertex was specified") @@ -1569,7 +1569,7 @@ all_st_mincuts_impl <- function(graph, source, target, capacity=NULL) { is_separator_impl <- function(graph, candidate) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) candidate <- as.igraph.vs(graph, candidate) on.exit( .Call(R_igraph_finalizer) ) @@ -1581,7 +1581,7 @@ is_separator_impl <- function(graph, candidate) { is_minimal_separator_impl <- function(graph, candidate) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) candidate <- as.igraph.vs(graph, candidate) on.exit( .Call(R_igraph_finalizer) ) @@ -1593,7 +1593,7 @@ is_minimal_separator_impl <- function(graph, candidate) { all_minimal_st_separators_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1609,7 +1609,7 @@ all_minimal_st_separators_impl <- function(graph) { minimum_size_separators_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1625,7 +1625,7 @@ minimum_size_separators_impl <- function(graph) { isoclass_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -1881,7 +1881,7 @@ isomorphic_34_impl <- function(graph1, graph2) { canonical_permutation_impl <- function(graph, colors, sh=c("fm", "f", "fs", "fl", "flm", "fsm")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (missing(colors)) { if ("color" %in% vertex_attr_names(graph)) { colors <- V(graph)$color @@ -1903,7 +1903,7 @@ canonical_permutation_impl <- function(graph, colors, sh=c("fm", "f", "fs", "fl" permute_vertices_impl <- function(graph, permutation) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) permutation <- as.numeric(permutation)-1 on.exit( .Call(R_igraph_finalizer) ) @@ -1948,7 +1948,7 @@ isomorphic_bliss_impl <- function(graph1, graph2, colors1, colors2, sh=c("fm", " automorphisms_impl <- function(graph, colors, sh=c("fm", "f", "fs", "fl", "flm", "fsm")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (missing(colors)) { if ("color" %in% vertex_attr_names(graph)) { colors <- V(graph)$color @@ -1970,7 +1970,7 @@ automorphisms_impl <- function(graph, colors, sh=c("fm", "f", "fs", "fl", "flm", automorphism_group_impl <- function(graph, colors, sh=c("fm", "f", "fs", "fl", "flm", "fsm"), details=FALSE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) if (missing(colors)) { if ("color" %in% vertex_attr_names(graph)) { colors <- V(graph)$color @@ -2015,7 +2015,7 @@ scg_norm_eps_impl <- function(V, groups, mtype=c("symmetric", "laplacian", "stoc adjacency_spectral_embedding_impl <- function(graph, no, weights=NULL, which=c("lm", "la", "sa"), scaled=TRUE, cvec=graph.strength(graph, weights=weights)/(vcount(graph)-1), options=igraph.arpack.default) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) no <- as.integer(no) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -2039,7 +2039,7 @@ adjacency_spectral_embedding_impl <- function(graph, no, weights=NULL, which=c(" laplacian_spectral_embedding_impl <- function(graph, no, weights=NULL, which=c("lm", "la", "sa"), type=c("default", "D-A", "DAD", "I-DAD", "OAP"), scaled=TRUE, options=igraph.arpack.default) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) no <- as.integer(no) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight @@ -2066,7 +2066,7 @@ laplacian_spectral_embedding_impl <- function(graph, no, weights=NULL, which=c(" eigen_adjacency_impl <- function(graph, algorithm=c("arpack", "auto", "lapack", "comp_auto", "comp_lapack", "comp_arpack"), which=list(), options=arpack_defaults) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) algorithm <- switch(igraph.match.arg(algorithm), "auto"=0, "lapack"=1, "arpack"=2, "comp_auto"=3, "comp_lapack"=4, "comp_arpack"=5) @@ -2083,7 +2083,7 @@ eigen_adjacency_impl <- function(graph, algorithm=c("arpack", "auto", "lapack", sir_impl <- function(graph, beta, gamma, no.sim=100) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) beta <- as.numeric(beta) gamma <- as.numeric(gamma) no.sim <- as.integer(no.sim) @@ -2120,7 +2120,7 @@ dim_select_impl <- function(sv) { is_eulerian_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -2131,7 +2131,7 @@ is_eulerian_impl <- function(graph) { eulerian_path_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -2147,7 +2147,7 @@ eulerian_path_impl <- function(graph) { eulerian_cycle_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -2163,7 +2163,7 @@ eulerian_cycle_impl <- function(graph) { is_tree_impl <- function(graph, mode=c("out", "in", "all", "total"), details=FALSE) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) mode <- switch(igraph.match.arg(mode), "out"=1, "in"=2, "all"=3, "total"=3) on.exit( .Call(R_igraph_finalizer) ) @@ -2196,7 +2196,7 @@ from_prufer_impl <- function(prufer) { to_prufer_impl <- function(graph) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) on.exit( .Call(R_igraph_finalizer) ) # Function call @@ -2207,7 +2207,7 @@ to_prufer_impl <- function(graph) { random_spanning_tree_impl <- function(graph, vid=0) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) vid <- as.igraph.vs(graph, vid) if (length(vid) == 0) { stop("No vertex was specified") @@ -2237,7 +2237,7 @@ tree_game_impl <- function(n, directed=FALSE, method=c("lerw", "prufer")) { vertex_coloring_greedy_impl <- function(graph, heuristic=c("colored_neighbors")) { # Argument checks - if (!is_igraph(graph)) { stop("Not a graph object") } + ensure_igraph(graph) heuristic <- switch(igraph.match.arg(heuristic), "colored_neighbors"=0L) on.exit( .Call(R_igraph_finalizer) ) From cc67829ce943d30ca97bfbc3a0d385408e378cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:17:10 +0200 Subject: [PATCH 12/16] more fixes --- R/community.R | 4 +--- R/conversion.R | 9 +-------- R/flow.R | 4 +--- R/glet.R | 4 +--- R/interface.R | 29 +++++------------------------ 5 files changed, 9 insertions(+), 41 deletions(-) diff --git a/R/community.R b/R/community.R index 704c34eb9aa..021920036e9 100644 --- a/R/community.R +++ b/R/community.R @@ -440,9 +440,7 @@ modularity <- function(x, ...) { #' modularity.igraph <- function(x, membership, weights = NULL, resolution = 1, directed = TRUE, ...) { # Argument checks - if (!is_igraph(x)) { - stop("Not a graph object") - } + ensure_igraph(x) if (is.null(membership) || (!is.numeric(membership) && !is.factor(membership))) { stop("Membership is not a numerical vector") } diff --git a/R/conversion.R b/R/conversion.R index b058da704a4..f31b788d304 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -253,11 +253,6 @@ as_adj <- as_adjacency_matrix #' @export as_edgelist <- function(graph, names = TRUE) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - res <- matrix(.Call(C_R_igraph_get_edgelist, graph, TRUE), - if (!is_igraph(graph)) { - stop("Not a graph object") - } on.exit(.Call(R_igraph_finalizer)) res <- matrix(.Call(R_igraph_get_edgelist, graph, TRUE), ncol = 2 @@ -871,9 +866,7 @@ as_incidence_matrix <- function(graph, types = NULL, attr = NULL, #' @family conversion #' @export as_data_frame <- function(x, what = c("edges", "vertices", "both")) { - if (!is_igraph(x)) { - stop("Not a graph object") - } + ensure_igraph(x) what <- igraph.match.arg(what) if (what %in% c("vertices", "both")) { diff --git a/R/flow.R b/R/flow.R index 2c9c9ce2e6e..c30d71c3249 100644 --- a/R/flow.R +++ b/R/flow.R @@ -386,9 +386,7 @@ adhesion <- function(graph, checks = TRUE) { #' @family flow #' @export cohesion.igraph <- function(x, checks = TRUE, ...) { - if (!is_igraph(x)) { - stop("Not a graph object") - } + ensure_igraph(x) on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_cohesion, x, as.logical(checks)) diff --git a/R/glet.R b/R/glet.R index f89a35240c6..c11e292859b 100644 --- a/R/glet.R +++ b/R/glet.R @@ -107,9 +107,7 @@ graphlet_basis <- function(graph, weights = NULL) { graphlet_proj <- function(graph, weights = NULL, cliques, niter = 1000, Mu = rep(1, length(cliques))) { # Argument checks - if (!is.igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } diff --git a/R/interface.R b/R/interface.R index ff2e01c3a19..e52261c6746 100644 --- a/R/interface.R +++ b/R/interface.R @@ -183,11 +183,7 @@ add_vertices <- function(graph, nv, ..., attr = list()) { #' g delete_edges <- function(graph, edges) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - .Call(C_R_igraph_delete_edges, graph, as.igraph.es(graph, edges) - 1) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_delete_edges, graph, as.igraph.es(graph, edges) - 1) } @@ -214,11 +210,7 @@ delete_edges <- function(graph, edges) { #' V(g2) delete_vertices <- function(graph, v) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - .Call(C_R_igraph_delete_vertices, graph, as.igraph.vs(graph, v) - 1) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_delete_vertices, graph, as.igraph.vs(graph, v) - 1) } @@ -249,11 +241,7 @@ delete_vertices <- function(graph, v) { #' hist() gsize <- function(graph) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - .Call(C_R_igraph_ecount, graph) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_ecount, graph) } @@ -363,11 +351,7 @@ incident <- function(graph, v, mode = c("all", "out", "in", "total")) { #' is_directed(g2) is_directed <- function(graph) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - .Call(C_R_igraph_is_directed, graph) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_is_directed, graph) } @@ -473,10 +457,7 @@ get.edges <- function(graph, es) { #' get.edge.ids <- function(graph, vp, directed = TRUE, error = FALSE, multi = FALSE) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_get_eids, graph, as.igraph.vs(graph, vp) - 1, From 4b1ab645713543a2472f4888a1f6801514f58fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:21:55 +0200 Subject: [PATCH 13/16] more --- R/iterators.R | 8 ++------ R/layout.R | 10 ++-------- R/operators.R | 24 +++++++----------------- R/print.R | 8 ++------ R/rewire.R | 10 ++-------- R/structural.properties.R | 6 +----- R/structure.info.R | 5 +---- R/topology.R | 32 ++++++++------------------------ 8 files changed, 25 insertions(+), 78 deletions(-) diff --git a/R/iterators.R b/R/iterators.R index 44d349606f5..09cf4730be2 100644 --- a/R/iterators.R +++ b/R/iterators.R @@ -1178,9 +1178,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { #' @name igraph-vs-attributes #' @export `V<-` <- function(x, value) { - if (!is_igraph(x)) { - stop("Not a graph object") - } + ensure_igraph(x) if (!"name" %in% names(attributes(value)) || !"value" %in% names(attributes(value))) { stop("invalid indexing") @@ -1199,9 +1197,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { #' @name igraph-es-attributes #' @export `E<-` <- function(x, path = NULL, P = NULL, directed = NULL, value) { - if (!is_igraph(x)) { - stop("Not a graph object") - } + ensure_igraph(x) if (!"name" %in% names(attributes(value)) || !"value" %in% names(attributes(value))) { stop("invalid indexing") diff --git a/R/layout.R b/R/layout.R index ead476e6420..841de934144 100644 --- a/R/layout.R +++ b/R/layout.R @@ -727,11 +727,7 @@ layout.grid.3d <- function(graph, width = 0, height = 0) { #' @family graph layouts layout_on_sphere <- function(graph) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - .Call(C_R_igraph_layout_sphere, graph) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_layout_sphere, graph) } @@ -1928,9 +1924,7 @@ with_sugiyama <- function(...) layout_spec(layout_with_sugiyama, ...) #' g <- disjoint_union(graphs) #' plot(g, layout = lay, vertex.size = 3, labels = NA, edge.color = "black") merge_coords <- function(graphs, layouts, method = "dla") { - if (!all(sapply(graphs, is_igraph))) { - stop("Not a graph object") - } + purrr::walk(graphs, ensure_igraph) if (method == "dla") { on.exit(.Call(R_igraph_finalizer)) res <- .Call( diff --git a/R/operators.R b/R/operators.R index 47a53eff4e8..8dfce182fdd 100644 --- a/R/operators.R +++ b/R/operators.R @@ -116,9 +116,7 @@ disjoint_union <- function(...) { graphs <- unlist(recursive = FALSE, lapply(list(...), function(l) { if (is_igraph(l)) list(l) else l })) - if (!all(sapply(graphs, is_igraph))) { - stop("Not a graph object") - } + purrr::walk(graphs, ensure_igraph) on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_disjoint_union, graphs) @@ -187,9 +185,7 @@ disjoint_union <- function(...) { graphs <- unlist(recursive = FALSE, lapply(list(...), function(l) { if (is_igraph(l)) list(l) else l })) - if (!all(sapply(graphs, is_igraph))) { - stop("Not a graph object") - } + purrr::walk(graphs, ensure_igraph) if (byname != "auto" && !is.logical(byname)) { stop("`bynam' must be \"auto\", or logical") } @@ -524,9 +520,8 @@ difference <- function(...) { #' print_all(G) #' plot(G, layout = layout_nicely(wheel)) difference.igraph <- function(big, small, byname = "auto", ...) { - if (!is_igraph(big) || !is_igraph(small)) { - stop("argument is not a graph") - } + ensure_igraph(big) + ensure_igraph(small) if (byname != "auto" && !is.logical(byname)) { stop("`bynam' must be \"auto\", or logical") } @@ -605,11 +600,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) { #' complementer <- function(graph, loops = FALSE) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - .Call(C_R_igraph_complementer, graph, as.logical(loops)) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_complementer, graph, as.logical(loops)) } @@ -678,9 +669,8 @@ complementer <- function(graph, loops = FALSE) { #' print_all(simplify(gc)) #' compose <- function(g1, g2, byname = "auto") { - if (!is_igraph(g1) || !is_igraph(g2)) { - stop("Not a graph object") - } + ensure_igraph(g1) + ensure_igraph(g2) if (byname != "auto" && !is.logical(byname)) { stop("`byname' must be \"auto\", or logical") diff --git a/R/print.R b/R/print.R index b1f53f18150..133afe47ffb 100644 --- a/R/print.R +++ b/R/print.R @@ -56,9 +56,7 @@ } .print.header <- function(object, id = igraph_opt("print.id")) { - if (!is_igraph(object)) { - stop("Not a graph object") - } + ensure_igraph(object) title <- paste0( "IGRAPH ", @@ -525,9 +523,7 @@ print.igraph <- function(x, full = igraph_opt("print.full"), names = TRUE, max.lines = igraph_opt("auto.print.lines"), id = igraph_opt("print.id"), ...) { - if (!is_igraph(x)) { - stop("Not a graph object") - } + ensure_igraph(x) head_lines <- .print.header(x, id) if (is.logical(full) && full) { diff --git a/R/rewire.R b/R/rewire.R index a3788fbf2f6..cbb465af24b 100644 --- a/R/rewire.R +++ b/R/rewire.R @@ -153,10 +153,7 @@ each_edge <- function(prob, loops = FALSE, multiple = FALSE, mode = c("all", "ou rewire_each_edge <- function(graph, prob, loops, multiple) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_rewire_edges, graph, as.numeric(prob), as.logical(loops), @@ -166,10 +163,7 @@ rewire_each_edge <- function(graph, prob, loops, multiple) { rewire_each_directed_edge <- function(graph, prob, loops, mode) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_rewire_directed_edges, graph, as.numeric(prob), as.logical(loops), diff --git a/R/structural.properties.R b/R/structural.properties.R index ebd32156840..5f7d68acb6e 100644 --- a/R/structural.properties.R +++ b/R/structural.properties.R @@ -1430,11 +1430,7 @@ feedback_arc_set <- feedback_arc_set_impl #' girth <- function(graph, circle = TRUE) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - res <- .Call(C_R_igraph_girth, graph, as.logical(circle)) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_girth, graph, as.logical(circle)) if (igraph_opt("return.vs.es") && circle) { diff --git a/R/structure.info.R b/R/structure.info.R index 01858a9546c..6ca6d4e0d27 100644 --- a/R/structure.info.R +++ b/R/structure.info.R @@ -47,10 +47,7 @@ #' are_adjacent(ug, 2, 1) are_adjacent <- function(graph, v1, v2) { ensure_igraph(graph) - on.exit(.Call(C_R_igraph_finalizer)) - if (!is_igraph(graph)) { - stop("Not a graph object") - } + on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_are_connected, graph, as.igraph.vs(graph, v1) - 1, diff --git a/R/topology.R b/R/topology.R index 3d28ae20de1..3110182dd2a 100644 --- a/R/topology.R +++ b/R/topology.R @@ -25,12 +25,8 @@ graph.get.isomorphisms.vf2 <- function(graph1, graph2, vertex.color1, vertex.color2, edge.color1, edge.color2) { # Argument checks - if (!is_igraph(graph1)) { - stop("Not a graph object") - } - if (!is_igraph(graph2)) { - stop("Not a graph object") - } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(vertex.color1)) { if ("color" %in% vertex_attr_names(graph1)) { vertex.color1 <- V(graph1)$color @@ -87,12 +83,8 @@ graph.get.subisomorphisms.vf2 <- function(graph1, graph2, vertex.color1, vertex.color2, edge.color1, edge.color2) { # Argument checks - if (!is_igraph(graph1)) { - stop("Not a graph object") - } - if (!is_igraph(graph2)) { - stop("Not a graph object") - } + ensure_igraph(graph1) + ensure_igraph(graph2) if (missing(vertex.color1)) { if ("color" %in% vertex_attr_names(graph1)) { vertex.color1 <- V(graph1)$color @@ -161,12 +153,8 @@ graph.subisomorphic.lad <- function(pattern, target, domains = NULL, induced = FALSE, map = TRUE, all.maps = FALSE, time.limit = Inf) { # Argument checks - if (!is_igraph(pattern)) { - stop("Not a graph object") - } - if (!is_igraph(target)) { - stop("Not a graph object") - } + ensure_igraph(pattern) + ensure_igraph(target) induced <- as.logical(induced) if (time.limit == Inf) { time.limit <- 0L @@ -313,12 +301,8 @@ isomorphic <- function(graph1, graph2, method = c( "auto", "direct", "vf2", "bliss" ), ...) { - if (!is_igraph(graph1)) { - stop("Not a graph object") - } - if (!is_igraph(graph2)) { - stop("Not a graph object") - } + ensure_igraph(graph1) + ensure_igraph(graph2) method <- igraph.match.arg(method) if (method == "auto") { From dd2b28b977bd02e7cbd4faea5033ce6bc295ca8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:35:45 +0200 Subject: [PATCH 14/16] add test --- tests/testthat/test-utils-ensure.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-utils-ensure.R b/tests/testthat/test-utils-ensure.R index 2b9b23d51fa..82f00f5648f 100644 --- a/tests/testthat/test-utils-ensure.R +++ b/tests/testthat/test-utils-ensure.R @@ -3,4 +3,5 @@ test_that("ensure_igraph() works", { expect_snapshot_error(ensure_igraph(NA)) expect_snapshot_error(ensure_igraph(NULL)) expect_silent(ensure_igraph(make_empty_graph())) + expect_silent(ensure_igraph(NULL, optional = TRUE)) }) From 985a9550d42fd84f2bf91d7d51e4819465e74335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:43:27 +0200 Subject: [PATCH 15/16] :facepalm: --- R/utils-ensure.R | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/R/utils-ensure.R b/R/utils-ensure.R index d4d607f65a6..6c66fb30487 100644 --- a/R/utils-ensure.R +++ b/R/utils-ensure.R @@ -1,11 +1,15 @@ ensure_igraph <- function(graph, optional = FALSE) { - if (rlang::is_missing(graph)) { - cli::cli_abort("Must provide a graph object (missing argument).") + if (is.null(graph)) { + if (!optional) { + cli::cli_abort("Must provide a graph object (provided {.code NULL}).") + } else { + return() + } } - if (!optional && is.null(graph)) { - cli::cli_abort("Must provide a graph object (provided {.code NULL}).") + if (rlang::is_missing(graph)) { + cli::cli_abort("Must provide a graph object (missing argument).") } if (!is_igraph(graph)) { From 2d079f358d45a3b61058cd9d6f48b82c57f6063a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 3 Apr 2023 15:50:50 +0200 Subject: [PATCH 16/16] oops --- R/layout.R | 2 +- R/operators.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/layout.R b/R/layout.R index 841de934144..8466096d393 100644 --- a/R/layout.R +++ b/R/layout.R @@ -1924,7 +1924,7 @@ with_sugiyama <- function(...) layout_spec(layout_with_sugiyama, ...) #' g <- disjoint_union(graphs) #' plot(g, layout = lay, vertex.size = 3, labels = NA, edge.color = "black") merge_coords <- function(graphs, layouts, method = "dla") { - purrr::walk(graphs, ensure_igraph) + lapply(graphs, ensure_igraph) if (method == "dla") { on.exit(.Call(R_igraph_finalizer)) res <- .Call( diff --git a/R/operators.R b/R/operators.R index 8dfce182fdd..72ba77f8eb7 100644 --- a/R/operators.R +++ b/R/operators.R @@ -116,7 +116,7 @@ disjoint_union <- function(...) { graphs <- unlist(recursive = FALSE, lapply(list(...), function(l) { if (is_igraph(l)) list(l) else l })) - purrr::walk(graphs, ensure_igraph) + lapply(graphs, ensure_igraph) on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_disjoint_union, graphs) @@ -185,7 +185,7 @@ disjoint_union <- function(...) { graphs <- unlist(recursive = FALSE, lapply(list(...), function(l) { if (is_igraph(l)) list(l) else l })) - purrr::walk(graphs, ensure_igraph) + lapply(graphs, ensure_igraph) if (byname != "auto" && !is.logical(byname)) { stop("`bynam' must be \"auto\", or logical") }