diff --git a/R/attributes.R b/R/attributes.R index 1a34d29fc8e..cad34d9f213 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -968,7 +968,7 @@ edge.attributes <- function(graph, index = E(graph)) { if (!missing(index)) { index <- as_igraph_es(graph, index) - if (any(duplicated(index)) || any(is.na(index))) { + if (anyDuplicated(index) > 0 || anyNA(index)) { cli::cli_abort("{.arg index} contains duplicated edges or NAs.") } } @@ -1302,7 +1302,7 @@ igraph.i.attribute.combination <- function(comb) { if (is.null(names(comb))) { names(comb) <- rep("", length(comb)) } - if (any(duplicated(names(comb)))) { + if (anyDuplicated(names(comb)) > 0) { cli::cli_warn("Some attributes are duplicated") } comb <- lapply(comb, function(x) { diff --git a/R/community.R b/R/community.R index 0d9b8cf3781..bd70b84fad0 100644 --- a/R/community.R +++ b/R/community.R @@ -1654,21 +1654,21 @@ cluster_leiden <- function( if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } - if (!is.null(weights) && !any(is.na(weights))) { + if (!is.null(weights) && !anyNA(weights)) { weights <- as.numeric(weights) } else { weights <- NULL } # Parse initial_membership argument - if (!is.null(initial_membership) && !any(is.na(initial_membership))) { + if (!is.null(initial_membership) && !anyNA(initial_membership)) { initial_membership <- as.numeric(initial_membership) } else { initial_membership <- NULL } # Parse node weights argument - if (!is.null(vertex_weights) && !any(is.na(vertex_weights))) { + if (!is.null(vertex_weights) && !anyNA(vertex_weights)) { vertex_weights <- as.numeric(vertex_weights) if (objective_function == 1) { # Using modularity @@ -1851,7 +1851,7 @@ cluster_walktrap <- function( if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } - if (!is.null(weights) && !any(is.na(weights))) { + if (!is.null(weights) && !anyNA(weights)) { weights <- as.numeric(weights) } else { weights <- NULL diff --git a/R/conversion.R b/R/conversion.R index 0634fb46949..02a2cbf02bc 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -1567,7 +1567,7 @@ graph_from_data_frame <- function(d, directed = TRUE, vertices = NULL) { ## Handle if some elements are 'NA' (first two columns are interpreted as from/to) ensure_no_na(d[, 1:2], "edge data frame") - if (!is.null(vertices) && any(is.na(vertices[, 1]))) { + if (!is.null(vertices) && anyNA(vertices[, 1])) { cli::cli_warn( "In {.code vertices[,1]}, {.code NA} elements were replaced with string {.str NA}." ) @@ -1582,7 +1582,7 @@ graph_from_data_frame <- function(d, directed = TRUE, vertices = NULL) { cli::cli_abort("{.arg vertices} contains no rows") } names <- as.character(vertices[, 1]) - if (any(duplicated(names))) { + if (anyDuplicated(names) > 0) { cli::cli_abort("{.arg vertices} contains duplicated vertex names") } if (any(!names2 %in% names)) { diff --git a/R/indexing.R b/R/indexing.R index ae76333e547..73dc78c0269 100644 --- a/R/indexing.R +++ b/R/indexing.R @@ -234,12 +234,12 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) { cli::cli_abort("Cannot use {.arg from}/{.arg to} without the other") } if (!missing(from)) { - if ((!is.numeric(from) && !is.character(from)) || any(is.na(from))) { + if ((!is.numeric(from) && !is.character(from)) || anyNA(from)) { cli::cli_abort( "{.arg from} must be a numeric or character vector without NAs" ) } - if ((!is.numeric(to) && !is.character(to)) || any(is.na(to))) { + if ((!is.numeric(to) && !is.character(to)) || anyNA(to)) { cli::cli_abort( "{.arg to} must be a numeric or character vector without NAs" ) @@ -487,12 +487,12 @@ expand.grid.unordered <- function(i, j, loops = FALSE, directed = FALSE) { cli::cli_abort("Logical or numeric value must be of length 1") } if (!missing(from)) { - if ((!is.numeric(from) && !is.character(from)) || any(is.na(from))) { + if ((!is.numeric(from) && !is.character(from)) || anyNA(from)) { cli::cli_abort( "{.arg from} must be a numeric or character vector without NAs" ) } - if ((!is.numeric(to) && !is.character(to)) || any(is.na(to))) { + if ((!is.numeric(to) && !is.character(to)) || anyNA(to)) { cli::cli_abort( "{.arg to} must be a numeric or character vector without NAs" ) diff --git a/R/interface.R b/R/interface.R index e589ea2fc3f..2cbe8ab4fae 100644 --- a/R/interface.R +++ b/R/interface.R @@ -145,7 +145,7 @@ add_edges <- function(graph, edges, ..., attr = list()) { attrs <- list(...) attrs <- append(attrs, attr) nam <- names(attrs) - if (length(attrs) != 0 && (is.null(nam) || any(nam == ""))) { + if (length(attrs) != 0 && (is.null(nam) || any(!nzchar(nam)))) { cli::cli_abort("All attributes must be named.") } @@ -212,7 +212,7 @@ add_vertices <- function(graph, nv, ..., attr = list()) { attrs <- list(...) attrs <- append(attrs, attr) nam <- names(attrs) - if (length(attrs) != 0 && (is.null(nam) || any(nam == ""))) { + if (length(attrs) != 0 && (is.null(nam) || any(!nzchar(nam)))) { cli::cli_abort("All attributes must be named.") } diff --git a/R/iterators.R b/R/iterators.R index 2cfd3101348..d02e9d364d6 100644 --- a/R/iterators.R +++ b/R/iterators.R @@ -401,7 +401,7 @@ create_es <- function(graph, idx, na_ok = FALSE) { simple_vs_index <- function(x, i, na_ok = FALSE) { res <- unclass(x)[i] - if (!na_ok && any(is.na(res))) { + if (!na_ok && anyNA(res)) { cli::cli_abort("Unknown vertex selected.") } class(res) <- "igraph.vs" @@ -842,7 +842,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { } else { res <- unclass(x)[i] } - if (!na_ok && any(is.na(res))) { + if (!na_ok && anyNA(res)) { cli::cli_abort("Unknown edge selected") } attr(res, "env") <- attr(x, "env") @@ -1562,7 +1562,7 @@ as_igraph_vs <- function(graph, v, na.ok = FALSE) { } if (is.character(v) && "name" %in% vertex_attr_names(graph)) { v <- as.numeric(match(v, V(graph)$name)) - if (!na.ok && any(is.na(v))) { + if (!na.ok && anyNA(v)) { cli::cli_abort("Invalid vertex names") } v @@ -1574,7 +1574,7 @@ as_igraph_vs <- function(graph, v, na.ok = FALSE) { } else { res <- as.numeric(v) } - if (!na.ok && any(is.na(res))) { + if (!na.ok && anyNA(res)) { cli::cli_abort("Invalid vertex name(s)") } res @@ -1617,7 +1617,7 @@ as_igraph_es <- function(graph, e) { } else { res <- as.numeric(e) } - if (any(is.na(res))) { + if (anyNA(res)) { cli::cli_abort("Invalid edge names") } res diff --git a/R/layout.R b/R/layout.R index aa881913a5d..79d324c478c 100644 --- a/R/layout.R +++ b/R/layout.R @@ -2867,7 +2867,7 @@ layout_with_drl <- function( if (is.null(weights) && "weight" %in% edge_attr_names(graph)) { weights <- E(graph)$weight } - if (!is.null(weights) && !any(is.na(weights))) { + if (!is.null(weights) && !anyNA(weights)) { weights <- as.numeric(weights) } else { weights <- NULL diff --git a/R/make.R b/R/make.R index 9301e1552f1..fee809577ed 100644 --- a/R/make.R +++ b/R/make.R @@ -695,7 +695,7 @@ graph.bipartite <- function(types, edges, directed = FALSE) { ) } edges <- match(edges, vertex.names) - if (any(is.na(edges))) { + if (anyNA(edges)) { cli::cli_abort( "edge vector contains a vertex name that is not found in {.arg types}" ) @@ -2672,7 +2672,7 @@ make_bipartite_graph <- function(types, edges, directed = FALSE) { ) } edges <- match(edges, vertex.names) - if (any(is.na(edges))) { + if (anyNA(edges)) { cli::cli_abort( "edge vector contains a vertex name that is not found in {.arg types}" ) diff --git a/R/operators.R b/R/operators.R index 27cf7d455ae..9737a945071 100644 --- a/R/operators.R +++ b/R/operators.R @@ -261,7 +261,7 @@ disjoint_union <- function(...) { } vertex.attributes(res) <- attr - if ("name" %in% names(attr) && any(duplicated(attr$name))) { + if ("name" %in% names(attr) && anyDuplicated(attr$name) > 0) { cli::cli_warn("Duplicate vertex names in disjoint union.") } @@ -690,7 +690,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) { snames <- V(small)$name } perm <- match(bnames, snames) - if (any(is.na(perm))) { + if (anyNA(perm)) { perm[is.na(perm)] <- seq(from = vcount(small) + 1, to = vcount(big)) } big <- permute(big, perm) @@ -969,7 +969,7 @@ vertex <- function(...) { # Check for duplicate named arguments if (!is.null(arg_names)) { - named_args <- arg_names[arg_names != ""] + named_args <- arg_names[nzchar(arg_names)] if (anyDuplicated(named_args)) { duplicates <- unique(named_args[duplicated(named_args)]) cli::cli_abort( @@ -1142,8 +1142,8 @@ path <- function(...) { toadd <- unlist(e2, recursive = FALSE) attr <- list() } else { - toadd <- unlist(e2[names(e2) == ""]) - attr <- e2[names(e2) != ""] + toadd <- unlist(e2[!nzchar(names(e2))]) + attr <- e2[nzchar(names(e2))] } res <- add_edges(e1, as_igraph_vs(e1, toadd), attr = attr) } else if ("igraph.vertex" %in% class(e2)) { @@ -1167,8 +1167,8 @@ path <- function(...) { to_add <- unlist(e2, recursive = FALSE) attr <- list() } else { - to_add <- unlist(e2[names(e2) == ""]) - attr <- e2[names(e2) != ""] + to_add <- unlist(e2[!nzchar(names(e2))]) + attr <- e2[nzchar(names(e2))] } to_add <- as_igraph_vs(e1, to_add) lt <- length(to_add) diff --git a/R/other.R b/R/other.R index 1936d98d7ac..36df82a9085 100644 --- a/R/other.R +++ b/R/other.R @@ -162,7 +162,7 @@ handle_vertex_type_arg <- function(types, graph, required = T) { cli::cli_warn("vertex types converted to logical.") } types <- as.logical(types) - if (any(is.na(types))) { + if (anyNA(types)) { cli::cli_abort("`NA' is not allowed in vertex types") } } diff --git a/R/plot.R b/R/plot.R index 149d44dd780..f6f4d933a57 100644 --- a/R/plot.R +++ b/R/plot.R @@ -1700,7 +1700,7 @@ rglplot.igraph <- function(x, ...) { rgl::text3d(x, y, z, labels, col = label.color, adj = 0) edge.labels[is.na(edge.labels)] <- "" - if (any(edge.labels != "")) { + if (any(nzchar(edge.labels))) { x0 <- layout[, 1][el[, 1]] x1 <- layout[, 1][el[, 2]] y0 <- layout[, 2][el[, 1]] diff --git a/R/plot.common.R b/R/plot.common.R index 1232d323c71..7bb7f62f0da 100644 --- a/R/plot.common.R +++ b/R/plot.common.R @@ -589,7 +589,7 @@ i.parse.plot.params <- function(graph, params) { } } if (!is.function(p[[type]][[name]])) { - if (any(is.na(p[[type]][[name]]))) { + if (anyNA(p[[type]][[name]])) { if (name != "label") { cli::cli_warn( "{type} attribute {name} contains NAs. Replacing with default value {i.default.values[[type]][[name]] diff --git a/R/print.R b/R/print.R index 243a7d2055d..5ff30590bee 100644 --- a/R/print.R +++ b/R/print.R @@ -87,8 +87,8 @@ cat(title, "\n", sep = "") atxt <- .get.attr.codes(object) - atxt <- paste(atxt[atxt != ""], collapse = ", ") - if (atxt != "") { + atxt <- paste(atxt[nzchar(atxt)], collapse = ", ") + if (nzchar(atxt)) { atxt <- strwrap( paste(sep = "", "+ attr: ", atxt), prefix = "| ", @@ -96,7 +96,7 @@ ) cat(atxt, sep = "\n") } - 1 + if (length(atxt) == 1 && atxt == "") 0 else length(atxt) + 1 + if (length(atxt) == 1 && !nzchar(atxt)) 0 else length(atxt) } #' @importFrom utils capture.output diff --git a/R/sparsedf.R b/R/sparsedf.R index b52aba0a6b1..289ee91dd43 100644 --- a/R/sparsedf.R +++ b/R/sparsedf.R @@ -28,8 +28,8 @@ sdf <- function(..., row.names = NULL, NROW = NULL) { if ( is.null(names(cols)) || - any(names(cols) == "") || - any(duplicated(names(cols))) + any(!nzchar(names(cols))) || + anyDuplicated(names(cols)) > 0 ) { cli::cli_abort("Columns must be have (unique) names.") } diff --git a/tests/testthat/test-cliques.R b/tests/testthat/test-cliques.R index ef3fc43ca03..077ae7dbd4a 100644 --- a/tests/testthat/test-cliques.R +++ b/tests/testthat/test-cliques.R @@ -158,7 +158,7 @@ test_that("max_cliques() work", { } else { numeric() } - if (any(duplicated(PX$PX))) { + if (anyDuplicated(PX$PX) > 0) { cli::cli_abort("foo2") } }