From 3454e5b24b6d21f43769f55de5f86d9feeb6ad20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 14 May 2024 13:18:26 +0200 Subject: [PATCH 1/2] refactor: use assert_character() instead of as.character() --- R/attributes.R | 21 +++++++++++++-------- R/utils-assert-character.R | 5 +++++ 2 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 R/utils-assert-character.R diff --git a/R/attributes.R b/R/attributes.R index 74f1197de8..8a23af79de 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -282,7 +282,9 @@ graph_attr <- function(graph, name) { return(graph.attributes(graph)) } - .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_graph)[[as.character(name)]] + assert_character(name) + + .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_graph)[[name]] } @@ -384,8 +386,10 @@ vertex_attr <- function(graph, name, index = V(graph)) { } return(vertex.attributes(graph, index = index)) } + + assert_character(name) myattr <- - .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_vertex)[[as.character(name)]] + .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_vertex)[[name]] if (is_complete_iterator(index)) { return(myattr) } @@ -456,6 +460,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { i_set_vertex_attr <- function(graph, name, index = V(graph), value, check = TRUE) { ensure_igraph(graph) + assert_character(name) if (is.null(value)) { return(graph) @@ -470,7 +475,6 @@ i_set_vertex_attr <- function(graph, name, index = V(graph), value, check = TRUE if (!missing(index) && check) { index <- as_igraph_vs(graph, index) } - name <- as.character(name) vattrs <- .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_vertex) @@ -594,7 +598,7 @@ edge_attr <- function(graph, name, index = E(graph)) { edge.attributes(graph, index = index) } } else { - name <- as.character(name) + assert_character(name) myattr <- .Call(R_igraph_mybracket2, graph, igraph_t_idx_attr, igraph_attr_idx_edge)[[name]] if (is_complete_iterator(index)) { myattr @@ -667,6 +671,7 @@ set_edge_attr <- function(graph, name, index = E(graph), value) { i_set_edge_attr <- function(graph, name, index = E(graph), value, check = TRUE) { ensure_igraph(graph) + assert_character(name) if (is.null(value)) { return(graph) @@ -680,7 +685,7 @@ i_set_edge_attr <- function(graph, name, index = E(graph), value, check = TRUE) complete <- is_complete_iterator(index) single <- is_single_index(index) - name <- as.character(name) + if (!missing(index) && check) { index <- as_igraph_es(graph, index) } @@ -853,8 +858,8 @@ edge_attr_names <- function(graph) { #' graph_attr_names(g2) delete_graph_attr <- function(graph, name) { ensure_igraph(graph) + assert_character(name) - name <- as.character(name) if (!name %in% graph_attr_names(graph)) { stop("No such graph attribute: ", name) } @@ -882,8 +887,8 @@ delete_graph_attr <- function(graph, name) { #' vertex_attr_names(g2) delete_vertex_attr <- function(graph, name) { ensure_igraph(graph) + assert_character(name) - name <- as.character(name) if (!name %in% vertex_attr_names(graph)) { stop("No such vertex attribute: ", name) } @@ -911,8 +916,8 @@ delete_vertex_attr <- function(graph, name) { #' edge_attr_names(g2) delete_edge_attr <- function(graph, name) { ensure_igraph(graph) + assert_character(name) - name <- as.character(name) if (!name %in% edge_attr_names(graph)) { stop("No such edge attribute: ", name) } diff --git a/R/utils-assert-character.R b/R/utils-assert-character.R new file mode 100644 index 0000000000..93095cef47 --- /dev/null +++ b/R/utils-assert-character.R @@ -0,0 +1,5 @@ +assert_character <- function(x, name = "name") { + if (!inherits(x, "character")) { + cli::cli_abort("{.arg {name}} must be a character, not {.obj_type_friendly {x}}.") + } +} From 242b69672def364fc9347be8fe88217ba16182be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 14 May 2024 13:20:18 +0200 Subject: [PATCH 2/2] refactor: use assert_character() in make.R --- R/make.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/make.R b/R/make.R index 43954d1061..37c3bb94a2 100644 --- a/R/make.R +++ b/R/make.R @@ -645,10 +645,11 @@ make_graph <- function(edges, ..., n = max(edges), isolates = NULL, } make_famous_graph <- function(name) { + assert_character(name) name <- gsub("\\s", "_", name) on.exit(.Call(R_igraph_finalizer)) - res <- .Call(R_igraph_famous, as.character(name)) + res <- .Call(R_igraph_famous, name) if (igraph_opt("add.params")) { res$name <- capitalize(name) }