diff --git a/DESCRIPTION b/DESCRIPTION index 8d2562e1c08..381fb1ecae1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,6 +30,7 @@ Depends: methods, R (>= 3.0.2) Imports: + cli, graphics, grDevices, magrittr, diff --git a/R/aaa-auto.R b/R/aaa-auto.R index 9636328d32f..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 @@ -241,7 +241,7 @@ hsbm_list_game_impl <- function(n, mlist, rholist, Clist, p) { correlated_game_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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -1636,8 +1636,8 @@ isoclass_impl <- function(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(R_igraph_finalizer) ) # Function call @@ -1661,8 +1661,8 @@ isoclass_create_impl <- function(size, number, directed=TRUE) { 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 @@ isomorphic_vf2_impl <- function(graph1, graph2, vertex.color1, vertex.color2, ed 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 @@ count_isomorphisms_vf2_impl <- function(graph1, graph2, vertex.color1, vertex.co 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 @@ subisomorphic_vf2_impl <- function(graph1, graph2, vertex.color1, vertex.color2, 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 @@ count_subisomorphisms_vf2_impl <- function(graph1, graph2, vertex.color1, vertex 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(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) ) @@ -1915,8 +1915,8 @@ permute_vertices_impl <- function(graph, permutation) { 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 @@ -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) ) @@ -2249,4 +2249,3 @@ vertex_coloring_greedy_impl <- function(graph, heuristic=c("colored_neighbors")) } res } - diff --git a/R/attributes.R b/R/attributes.R index 8943ecd5092..71274306aeb 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(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(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,7 @@ 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(R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_graph) if (is.null(res)) { res <- character() @@ -600,9 +576,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(R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) if (is.null(res)) { @@ -626,9 +601,7 @@ 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(R_igraph_mybracket2_names, graph, igraph_t_idx_attr, igraph_attr_idx_edge) if (is.null(res)) { res <- character() @@ -652,9 +625,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 +655,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 +685,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 +734,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 +771,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 +780,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 3d207c2333d..2bea28c03dc 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 e7b91b61cfd..5afcda66f9d 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 7c7594a3ede..bec35ff9f14 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, @@ -1062,9 +1059,7 @@ harmonic_centrality <- harmonic_centrality_cutoff_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) { @@ -1240,9 +1235,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) @@ -1280,9 +1273,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 0166f85c470..4e07a41d908 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, optional = TRUE) + 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 b3f77a85c79..b06fb013cbc 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_number_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(R_igraph_finalizer)) res <- .Call(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(R_igraph_finalizer)) res <- .Call(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(R_igraph_finalizer)) .Call(R_igraph_independence_number, graph) diff --git a/R/cocitation.R b/R/cocitation.R index eb500f85520..d4fc440487f 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(R_igraph_finalizer)) res <- .Call(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(R_igraph_finalizer)) res <- .Call(R_igraph_bibcoupling, graph, v - 1) diff --git a/R/cohesive.blocks.R b/R/cohesive.blocks.R index f8249b6fceb..fe58b9be021 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(R_igraph_finalizer)) # Function call diff --git a/R/community.R b/R/community.R index 0bfff1a684a..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") } @@ -475,9 +473,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 +967,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 +1123,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 +1240,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(R_igraph_finalizer)) @@ -1322,9 +1314,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 +1441,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 +1524,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 +1659,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 +1755,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 +1848,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 +1940,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 +2018,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 99a50eb4948..2ee19725a36 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 b3d8af59d10..f3366820fee 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(R_igraph_finalizer)) res <- matrix(.Call(R_igraph_get_edgelist, graph, TRUE), ncol = 2 @@ -366,9 +358,7 @@ as.directed <- to_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) @@ -884,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")) { @@ -998,9 +978,7 @@ graph_from_adj_list <- adjlist_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(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 0b4eb4cf253..4f93bbc857b 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 9d4af216ed6..c30d71c3249 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(R_igraph_finalizer)) .Call(R_igraph_adhesion, graph, as.logical(checks)) @@ -398,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)) @@ -555,9 +541,7 @@ st_min_cuts <- all_st_mincuts_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 7bbf8c16391..73466162546 100644 --- a/R/foreign.R +++ b/R/foreign.R @@ -178,9 +178,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 18f1bd87417..0f1fd85a3ee 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 e1ce4430c40..9dfc3690516 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 } @@ -109,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/hrg.R b/R/hrg.R index f27bc403734..b5a8e7108c8 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 <- hrg_game_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 77df4ef034d..e52261c6746 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,8 @@ 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(R_igraph_finalizer)) .Call(R_igraph_delete_edges, graph, as.igraph.es(graph, edges) - 1) } @@ -214,9 +209,8 @@ 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(R_igraph_finalizer)) .Call(R_igraph_delete_vertices, graph, as.igraph.vs(graph, v) - 1) } @@ -246,9 +240,8 @@ 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(R_igraph_finalizer)) .Call(R_igraph_ecount, graph) } @@ -277,9 +270,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 +310,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 +350,8 @@ 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(R_igraph_finalizer)) .Call(R_igraph_is_directed, graph) } @@ -385,9 +373,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 +456,8 @@ 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(R_igraph_finalizer)) .Call( R_igraph_get_eids, graph, as.igraph.vs(graph, vp) - 1, @@ -520,7 +505,7 @@ vcount <- vcount_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 +548,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 f4ed399e6b1..a1793e178b7 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) @@ -1182,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") @@ -1203,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 cea5e848eed..8466096d393 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(R_igraph_finalizer)) .Call(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,8 @@ 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(R_igraph_finalizer)) .Call(R_igraph_layout_sphere, graph) } @@ -778,9 +765,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(R_igraph_finalizer)) .Call(R_igraph_layout_random, graph) @@ -924,9 +909,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 +1032,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 +1155,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 +1235,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 +1335,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 +1441,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 +1487,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 +1519,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 +1746,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) @@ -1955,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") - } + lapply(graphs, ensure_igraph) if (method == "dla") { on.exit(.Call(R_igraph_finalizer)) res <- .Call( @@ -2033,9 +2000,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 5c4d19dd718..f3ab659caf5 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 7e450fdf11d..eb0f0f21fab 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(R_igraph_finalizer)) res <- .Call(R_igraph_linegraph, graph) diff --git a/R/minimum.spanning.tree.R b/R/minimum.spanning.tree.R index 1fb9b0c0c2b..d98e51d57cd 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 99f286f9398..bcb550d1e40 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 55c66b71c7d..72ba77f8eb7 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") - } + lapply(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") - } + lapply(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") } @@ -604,9 +599,8 @@ 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(R_igraph_finalizer)) .Call(R_igraph_complementer, graph, as.logical(loops)) } @@ -675,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/paths.R b/R/paths.R index eda1d2e2c76..f5136e23809 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/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 4b1e63394ca..cbb465af24b 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,8 @@ 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(R_igraph_finalizer)) .Call( R_igraph_rewire_edges, graph, as.numeric(prob), as.logical(loops), @@ -165,9 +162,8 @@ 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(R_igraph_finalizer)) .Call( R_igraph_rewire_directed_edges, graph, as.numeric(prob), as.logical(loops), diff --git a/R/scg.R b/R/scg.R index 76bd4baa821..949fa186a1b 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 7a9e4455239..6fea8448c8b 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(R_igraph_finalizer)) # Function call diff --git a/R/structural.properties.R b/R/structural.properties.R index f3cc0ba1e08..5f7d68acb6e 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 <- average_path_length_dijkstra_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(R_igraph_finalizer)) .Call(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,8 @@ 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(R_igraph_finalizer)) res <- .Call(R_igraph_girth, graph, as.logical(circle)) if (igraph_opt("return.vs.es") && circle) { @@ -1657,9 +1616,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 +1779,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 +1883,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 +1944,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 +2004,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 +2120,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 +2137,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 +2155,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 6a77fe3f64e..6ca6d4e0d27 100644 --- a/R/structure.info.R +++ b/R/structure.info.R @@ -46,9 +46,8 @@ #' 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(R_igraph_finalizer)) .Call( 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 922c0e7194f..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 @@ -147,9 +139,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(R_igraph_finalizer)) @@ -163,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 @@ -315,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") { diff --git a/R/utils-ensure.R b/R/utils-ensure.R new file mode 100644 index 00000000000..6c66fb30487 --- /dev/null +++ b/R/utils-ensure.R @@ -0,0 +1,18 @@ +ensure_igraph <- function(graph, optional = FALSE) { + + if (is.null(graph)) { + if (!optional) { + cli::cli_abort("Must provide a graph object (provided {.code NULL}).") + } else { + return() + } + } + + if (rlang::is_missing(graph)) { + cli::cli_abort("Must provide a graph object (missing argument).") + } + + if (!is_igraph(graph)) { + cli::cli_abort("Must provide a graph object (provided wrong object type).") + } +} diff --git a/tests/testthat/_snaps/utils-ensure.md b/tests/testthat/_snaps/utils-ensure.md new file mode 100644 index 00000000000..69b6ac09d8c --- /dev/null +++ b/tests/testthat/_snaps/utils-ensure.md @@ -0,0 +1,12 @@ +# ensure_igraph() works + + 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-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] diff --git a/tests/testthat/test-utils-ensure.R b/tests/testthat/test-utils-ensure.R new file mode 100644 index 00000000000..82f00f5648f --- /dev/null +++ b/tests/testthat/test-utils-ensure.R @@ -0,0 +1,7 @@ +test_that("ensure_igraph() works", { + expect_snapshot_error(ensure_igraph(1)) + 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)) +}) diff --git a/tools/stimulus/types-RR.yaml b/tools/stimulus/types-RR.yaml index f56dc0047c6..13d8ecf4a9c 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%, optional = TRUE) INTEGER: DEFAULT: