diff --git a/R/operators.R b/R/operators.R index 8722413f9a..4276666b97 100644 --- a/R/operators.R +++ b/R/operators.R @@ -119,26 +119,26 @@ graph.complementer <- function(graph, loops = FALSE) { ################################################################### rename.attr.if.needed <- function( - type, + type = c("g", "v", "e"), graphs, newsize = NULL, maps = NULL, maps2 = NULL, ignore = character() ) { + type <- igraph.match.arg(type) + listfun <- switch( type, "g" = graph_attr_names, "v" = vertex_attr_names, - "e" = edge_attr_names, - stop("Internal igraph error") + "e" = edge_attr_names ) getfun <- switch( type, "g" = graph_attr, "v" = vertex_attr, - "e" = edge_attr, - stop("Internal igraph error") + "e" = edge_attr ) alist <- lapply(graphs, listfun) an <- unique(unlist(alist)) @@ -1176,7 +1176,7 @@ path <- function(...) { ## Adding named vertices res <- add_vertices(e1, length(e2), name = e2) } else { - cli::cli_abort("Cannot add {.obj_type_friendly type} to igraph graph.") + cli::cli_abort("Cannot add {.obj_type_friendly {type}} to igraph graph.") } res } @@ -1257,7 +1257,7 @@ path <- function(...) { res <- delete_vertices(e1, e2) } else { cli::cli_abort( - "Cannot substract {.obj_type_friendly type} from igraph graph." + "Cannot substract {.obj_type_friendly {type}} from igraph graph." ) } res @@ -1310,7 +1310,7 @@ rep.igraph <- function(x, n, mark = TRUE, ...) { rep.igraph(x, n) } else { cli::cli_abort( - "Cannot multiply igraph graph with {.obj_type_friendly type}." + "Cannot multiply igraph graph with {.obj_type_friendly {type}}." ) } } diff --git a/R/plot.common.R b/R/plot.common.R index 72b07c6d1e..00b7f45616 100644 --- a/R/plot.common.R +++ b/R/plot.common.R @@ -542,10 +542,13 @@ i.parse.plot.params <- function(graph, params) { } } - func <- function(type, name, range = NULL, dontcall = FALSE) { - if (!type %in% names(p)) { - stop("Invalid plot option type") - } + func <- function( + type = c("vertex", "edge", "plot"), + name, + range = NULL, + dontcall = FALSE + ) { + type <- igraph.match.arg(type) ret <- function() { v <- p[[type]][[name]] if (is.function(v) && !dontcall) { @@ -676,7 +679,7 @@ igraph.check.shapes <- function(x) { bad.shapes <- !xx %in% ls(.igraph.shapes) if (any(bad.shapes)) { bs <- paste(xx[bad.shapes], collapse = ", ") - stop("Bad vertex shape(s): ", bs, ".") + cli::cli_abort("Bad vertex {cli::qty(length(bad.shapes))} shape{?s}: {bs}.") } x } diff --git a/R/printr.R b/R/printr.R index 15ba158d0b..dcc32ac622 100644 --- a/R/printr.R +++ b/R/printr.R @@ -195,8 +195,11 @@ head_print_callback <- function( #' @export indent_print <- function(..., .indent = " ", .printer = print) { - if (length(.indent) != 1) { - stop(".indent must be a scalar") + if (length(.indent) != 1 || !is.character(.indent)) { + indent <- .indent # cli literal cannot start with a dot + cli::cli_abort( + "{.arg .indent} must be a character scalar, not {.obj_type_friendly {indent}}." + ) } opt <- options(width = getOption("width") - nchar(.indent)) diff --git a/R/rewire.R b/R/rewire.R index 54f5725049..44354e904b 100644 --- a/R/rewire.R +++ b/R/rewire.R @@ -40,7 +40,10 @@ #' print_all(rewire(g, with = keeping_degseq(niter = vcount(g) * 10))) rewire <- function(graph, with) { if (!is(with, "igraph_rewiring_method")) { - stop("'with' is not an igraph rewiring method") + cli::cli_abort( + "{.arg with} must be an igraph rewiring method, + not {.obj_type_friendly {with}}." + ) } do_call(with$fun, list(graph), .args = with$args) } @@ -140,7 +143,10 @@ each_edge <- function( multiple <- as.logical(multiple) if (mode != 3) { if (!multiple) { - stop("multiple = FALSE not supported when mode != \"all\"") + cli::cli_abort( + '{.code multiple = FALSE} is not supported + when {.code mode != "all"}' + ) } method <- list( fun = rewire_each_directed_edge, diff --git a/R/sgm.R b/R/sgm.R index 2f8b8a3058..ad7baa465a 100644 --- a/R/sgm.R +++ b/R/sgm.R @@ -1,11 +1,22 @@ solve_LSAP <- function(x, maximum = FALSE) { - if (!is.matrix(x) || any(x < 0)) { - stop("x must be a matrix with nonnegative entries.") + if (!is.matrix(x)) { + cli::cli_abort("{.arg x} must be a matrix, not {.obj_type_friendly {x}}.") + } + if (any(x < 0)) { + cli::cli_abort(c( + "{.arg x} must not have negative entries.", + i = "It has {sum(x<0)} negative entr{?y/ies}." + )) } nr <- nrow(x) nc <- ncol(x) if (nr > nc) { - stop("x must not have more rows than columns.") + cli::cli_abort( + c( + "{.arg x} must not have more rows than columns.", + i = "It has {nrow(x)} rows and {ncol(x)} columns." + ) + ) } if (nc > nr) { x <- rbind(x, matrix(2 * sum(x), nc - nr, nc)) diff --git a/R/stochastic_matrix.R b/R/stochastic_matrix.R index f6113d8a86..eb5afe3a6c 100644 --- a/R/stochastic_matrix.R +++ b/R/stochastic_matrix.R @@ -83,12 +83,16 @@ stochastic_matrix <- function( column.wise <- as.logical(column.wise) if (length(column.wise) != 1) { - stop("`column.wise' must be a logical scalar") + cli::cli_abort( + "{.arg column.wise} must be a logical scalar, not {.obj_type_friendly {column.wise}}." + ) } sparse <- as.logical(sparse) if (length(sparse) != 1) { - stop("`sparse' must be a logical scalar") + cli::cli_abort( + "{.arg sparse} must be a logical scalar, not {.obj_type_friendly {sparse}}." + ) } on.exit(.Call(R_igraph_finalizer)) diff --git a/R/versions.R b/R/versions.R index 27c384b9d9..08520a5d84 100644 --- a/R/versions.R +++ b/R/versions.R @@ -92,11 +92,8 @@ upgrade_graph <- function(graph) { } if (g_ver > p_ver) { - stop( - "Don't know how to downgrade graph from version ", - g_ver, - " to ", - p_ver + cli::cli_abort( + "Don't know how to downgrade graph from version {g_ver} to {p_ver}." ) } @@ -119,7 +116,9 @@ upgrade_graph <- function(graph) { graph } else { - stop("Don't know how to upgrade graph from version ", g_ver, " to ", p_ver) + cli::cli_abort( + "Don't know how to upgrade graph from version {g_ver} to {p_ver}." + ) } } @@ -157,8 +156,9 @@ warn_version <- function(graph) { return(TRUE) } - stop( - "This graph was created by a new(er) igraph version. Please install the latest version of igraph and try again." + cli::cli_abort( + "This graph was created by a newer igraph version. + Please install the latest version of igraph and try again." ) } diff --git a/tests/testthat/_snaps/versions.md b/tests/testthat/_snaps/versions.md index d4f2c60662..27d877e05d 100644 --- a/tests/testthat/_snaps/versions.md +++ b/tests/testthat/_snaps/versions.md @@ -13,7 +13,7 @@ upgrade_graph(g) Condition Error in `upgrade_graph()`: - ! Don't know how to upgrade graph from version 0 to 4 + ! Don't know how to upgrade graph from version 0 to 4. # we can't upgrade from 0.2 to 1.5.0, on the fly diff --git a/tests/testthat/test-cliques.R b/tests/testthat/test-cliques.R index 79734f3540..2525e7c253 100644 --- a/tests/testthat/test-cliques.R +++ b/tests/testthat/test-cliques.R @@ -159,7 +159,7 @@ test_that("max_cliques() work", { numeric() } if (any(duplicated(PX$PX))) { - stop("foo2") + cli::cli_abort("foo2") } } pres diff --git a/tests/testthat/test-glet.R b/tests/testthat/test-glet.R index af7cd8b615..b6f845f4ff 100644 --- a/tests/testthat/test-glet.R +++ b/tests/testthat/test-glet.R @@ -52,10 +52,10 @@ threshold.net <- function(graph, level) { graphlets.old <- function(graph) { if (!is_weighted(graph)) { - stop("Graph not weighted") + cli::cli_abort("Graph not weighted") } if (min(E(graph)$weight) <= 0 || any(!is.finite(E(graph)$weight))) { - stop("Edge weights must be non-negative and finite") + cli::cli_abort("Edge weights must be non-negative and finite") } ## Do all thresholds @@ -94,10 +94,10 @@ test_that("Graphlets work for a bigger graph", { graphlets.project.old <- function(graph, cliques, iter, Mu = NULL) { if (!is_weighted(graph)) { - stop("Graph not weighted") + cli::cli_abort("Graph not weighted") } if (min(E(graph)$weight) <= 0 || any(!is.finite(E(graph)$weight))) { - stop("Edge weights must be non-negative and finite") + cli::cli_abort("Edge weights must be non-negative and finite") } if ( length(iter) != 1 || @@ -105,7 +105,7 @@ graphlets.project.old <- function(graph, cliques, iter, Mu = NULL) { !is.finite(iter) || iter != as.integer(iter) ) { - stop("`iter' must be a non-negative finite integer scalar") + cli::cli_abort("`iter' must be a non-negative finite integer scalar") } clf <- cliques diff --git a/tests/testthat/test-structural-properties.R b/tests/testthat/test-structural-properties.R index 218fd52e3e..f938d495dd 100644 --- a/tests/testthat/test-structural-properties.R +++ b/tests/testthat/test-structural-properties.R @@ -160,7 +160,7 @@ test_that("BFS callback does not blow up when an invalid value is returned", { test_that("BFS callback does not blow up when an error is raised within the callback", { callback <- function(graph, data, extra) { - stop("test") + cli::cli_abort("test") FALSE }