From ae987e6694ce5d3d7667dbb97a4fd3bcc0cac28b Mon Sep 17 00:00:00 2001 From: schochastics Date: Fri, 27 Jun 2025 10:25:19 +0200 Subject: [PATCH 01/13] added multi attribute assignment --- R/attributes.R | 52 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 7bac81c39fa..82abaaf8be0 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -506,6 +506,7 @@ vertex_attr <- function(graph, name, index = V(graph)) { } } + #' Set vertex attributes #' #' @param graph The graph. @@ -525,14 +526,53 @@ vertex_attr <- function(graph, name, index = V(graph)) { #' set_vertex_attr("label", value = LETTERS[1:10]) #' g #' plot(g) -set_vertex_attr <- function(graph, name, index = V(graph), value) { - check_string(name) +set_vertex_attr <- function(graph, name, index = V(graph), value, ...) { + legacy <- !missing(name) && !missing(value) - if (is_complete_iterator(index)) { - i_set_vertex_attr(graph = graph, name = name, value = value, check = FALSE) - } else { - i_set_vertex_attr(graph = graph, name = name, index = index, value = value) + if (legacy) { + if (!is.character(name) || length(name) != 1) { + cli::cli_abort("`name` must be a single string.") + } + + if (is_complete_iterator(index)) { + return(i_set_vertex_attr( + graph = graph, + name = name, + value = value, + check = FALSE + )) + } else { + return(i_set_vertex_attr( + graph = graph, + name = name, + index = index, + value = value + )) + } + } + + dots <- list(...) + if (length(dots) == 0L) { + cli::cli_abort( + "Must supply either a named attribute and value, or named arguments via `...`." + ) + } + + if (is.null(names(dots)) || any(names(dots) == "")) { + cli::cli_abort("All arguments in `...` must be named.") } + + for (attr_name in names(dots)) { + attr_value <- dots[[attr_name]] + graph <- i_set_vertex_attr( + graph, + name = attr_name, + index = index, + value = attr_value + ) + } + + graph } i_set_vertex_attr <- function( From 1924116ca1398ac77e8de8d3f779f73371a807d4 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 3 Jul 2025 13:35:21 +0000 Subject: [PATCH 02/13] chore: Auto-update from GitHub Actions Run: https://github.com/igraph/rigraph/actions/runs/16051784888 --- man/set_vertex_attr.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/set_vertex_attr.Rd b/man/set_vertex_attr.Rd index cdefdad3915..f323c9500b9 100644 --- a/man/set_vertex_attr.Rd +++ b/man/set_vertex_attr.Rd @@ -4,7 +4,7 @@ \alias{set_vertex_attr} \title{Set vertex attributes} \usage{ -set_vertex_attr(graph, name, index = V(graph), value) +set_vertex_attr(graph, name, index = V(graph), value, ...) } \arguments{ \item{graph}{The graph.} From 5d2a2a5a10b5e6915e22fa983c3e314202fd5723 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 13:26:11 +0200 Subject: [PATCH 03/13] split into vertex_attr and *_attrs --- R/attributes.R | 59 +++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 82abaaf8be0..b8e590772fb 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -527,36 +527,41 @@ vertex_attr <- function(graph, name, index = V(graph)) { #' g #' plot(g) set_vertex_attr <- function(graph, name, index = V(graph), value, ...) { - legacy <- !missing(name) && !missing(value) - - if (legacy) { - if (!is.character(name) || length(name) != 1) { - cli::cli_abort("`name` must be a single string.") - } - - if (is_complete_iterator(index)) { - return(i_set_vertex_attr( - graph = graph, - name = name, - value = value, - check = FALSE - )) - } else { - return(i_set_vertex_attr( - graph = graph, - name = name, - index = index, - value = value - )) - } + check_string(name) + if (is_complete_iterator(index)) { + return(i_set_vertex_attr( + graph = graph, + name = name, + value = value, + check = FALSE + )) + } else { + return(i_set_vertex_attr( + graph = graph, + name = name, + index = index, + value = value + )) } + graph +} +#' Set multiple vertex attributes +#' +#' @param graph The graph. +#' @param ... Named arguments, where the names are the attributes +#' @return The graph, with the vertex attributes added or set. +#' +#' @family attributes +#' +#' @export +#' @examples +#' g <- make_ring(10) %>% +#' set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) +#' g +#' plot(g) +set_vertex_attrs <- function(graph, index = V(graph), ...) { dots <- list(...) - if (length(dots) == 0L) { - cli::cli_abort( - "Must supply either a named attribute and value, or named arguments via `...`." - ) - } if (is.null(names(dots)) || any(names(dots) == "")) { cli::cli_abort("All arguments in `...` must be named.") From 3ce713bdebed21711840b04b15a94b009bb10b9d Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 13:26:39 +0200 Subject: [PATCH 04/13] added tests --- tests/testthat/_snaps/attributes.md | 8 ++++++++ tests/testthat/test-attributes.R | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/tests/testthat/_snaps/attributes.md b/tests/testthat/_snaps/attributes.md index c9b916fc2c5..172c445888a 100644 --- a/tests/testthat/_snaps/attributes.md +++ b/tests/testthat/_snaps/attributes.md @@ -62,3 +62,11 @@ Error in `set_graph_attr()`: ! `name` must be a single string, not the number 1. +# set_vertex_attrs() works + + Code + set_vertex_attrs(g) + Condition + Error in `set_vertex_attrs()`: + ! All arguments in `...` must be named. + diff --git a/tests/testthat/test-attributes.R b/tests/testthat/test-attributes.R index 9736d30b062..a20ec62e698 100644 --- a/tests/testthat/test-attributes.R +++ b/tests/testthat/test-attributes.R @@ -465,3 +465,14 @@ test_that("good error message when not using character", { set_graph_attr(ring, 1, 1) }) }) + +test_that("set_vertex_attrs() works", { + g <- make_ring(10) + g <- set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) + expect_equal(V(g)$color, rep("blue", vcount(g))) + expect_equal(V(g)$size, rep(10, vcount(g))) + expect_equal(V(g)$name, LETTERS[1:10]) + expect_snapshot(error = TRUE, { + set_vertex_attrs(g) + }) +}) From f9710dd0f9bd361f092b9e8cd0d0416f7167ec2c Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 13:26:54 +0200 Subject: [PATCH 05/13] document --- NAMESPACE | 1 + man/delete_edge_attr.Rd | 1 + man/delete_graph_attr.Rd | 1 + man/delete_vertex_attr.Rd | 1 + man/edge_attr-set.Rd | 1 + man/edge_attr.Rd | 1 + man/edge_attr_names.Rd | 1 + man/graph_attr-set.Rd | 1 + man/graph_attr.Rd | 1 + man/graph_attr_names.Rd | 1 + man/igraph-attribute-combination.Rd | 1 + man/igraph-dollar.Rd | 1 + man/igraph-vs-attributes.Rd | 1 + man/set_edge_attr.Rd | 1 + man/set_graph_attr.Rd | 1 + man/set_vertex_attr.Rd | 1 + man/set_vertex_attrs.Rd | 47 +++++++++++++++++++++++++++++ man/vertex_attr-set.Rd | 1 + man/vertex_attr.Rd | 1 + man/vertex_attr_names.Rd | 1 + 20 files changed, 66 insertions(+) create mode 100644 man/set_vertex_attrs.Rd diff --git a/NAMESPACE b/NAMESPACE index 4bd16e4bea4..68324de0e9c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -799,6 +799,7 @@ export(set.vertex.attribute) export(set_edge_attr) export(set_graph_attr) export(set_vertex_attr) +export(set_vertex_attrs) export(shape_noclip) export(shape_noplot) export(shapes) diff --git a/man/delete_edge_attr.Rd b/man/delete_edge_attr.Rd index a1492f9962e..095f8d184ab 100644 --- a/man/delete_edge_attr.Rd +++ b/man/delete_edge_attr.Rd @@ -40,6 +40,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/delete_graph_attr.Rd b/man/delete_graph_attr.Rd index 22d493bd593..c3823b38707 100644 --- a/man/delete_graph_attr.Rd +++ b/man/delete_graph_attr.Rd @@ -39,6 +39,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/delete_vertex_attr.Rd b/man/delete_vertex_attr.Rd index 2e2e2400973..3505211f3a7 100644 --- a/man/delete_vertex_attr.Rd +++ b/man/delete_vertex_attr.Rd @@ -40,6 +40,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/edge_attr-set.Rd b/man/edge_attr-set.Rd index cd6c71cdf64..1f6f39244de 100644 --- a/man/edge_attr-set.Rd +++ b/man/edge_attr-set.Rd @@ -52,6 +52,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/edge_attr.Rd b/man/edge_attr.Rd index bed33fb874d..cb1c373a251 100644 --- a/man/edge_attr.Rd +++ b/man/edge_attr.Rd @@ -46,6 +46,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/edge_attr_names.Rd b/man/edge_attr_names.Rd index 09aa3b66c2b..60e16f4dc98 100644 --- a/man/edge_attr_names.Rd +++ b/man/edge_attr_names.Rd @@ -37,6 +37,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/graph_attr-set.Rd b/man/graph_attr-set.Rd index d3a7fa57cb1..2ce09e03a3b 100644 --- a/man/graph_attr-set.Rd +++ b/man/graph_attr-set.Rd @@ -49,6 +49,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/graph_attr.Rd b/man/graph_attr.Rd index 5bfc783b9e1..5511992ee2a 100644 --- a/man/graph_attr.Rd +++ b/man/graph_attr.Rd @@ -40,6 +40,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/graph_attr_names.Rd b/man/graph_attr_names.Rd index 5f3a3713d94..5e3612e88f5 100644 --- a/man/graph_attr_names.Rd +++ b/man/graph_attr_names.Rd @@ -36,6 +36,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/igraph-attribute-combination.Rd b/man/igraph-attribute-combination.Rd index 877ed2f819a..e7b51f951ff 100644 --- a/man/igraph-attribute-combination.Rd +++ b/man/igraph-attribute-combination.Rd @@ -147,6 +147,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/igraph-dollar.Rd b/man/igraph-dollar.Rd index 55355bcfb22..128e6c43ec3 100644 --- a/man/igraph-dollar.Rd +++ b/man/igraph-dollar.Rd @@ -44,6 +44,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/igraph-vs-attributes.Rd b/man/igraph-vs-attributes.Rd index a442e6edf2b..799d08020d6 100644 --- a/man/igraph-vs-attributes.Rd +++ b/man/igraph-vs-attributes.Rd @@ -103,6 +103,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/set_edge_attr.Rd b/man/set_edge_attr.Rd index d4b467e6d6a..739db4799f8 100644 --- a/man/set_edge_attr.Rd +++ b/man/set_edge_attr.Rd @@ -46,6 +46,7 @@ Vertex, edge and graph attributes \code{\link{igraph-vs-attributes}}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/set_graph_attr.Rd b/man/set_graph_attr.Rd index 26d09a12e8b..70953d8a9aa 100644 --- a/man/set_graph_attr.Rd +++ b/man/set_graph_attr.Rd @@ -41,6 +41,7 @@ Vertex, edge and graph attributes \code{\link{igraph-vs-attributes}}, \code{\link{set_edge_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/set_vertex_attr.Rd b/man/set_vertex_attr.Rd index f323c9500b9..0570e138f27 100644 --- a/man/set_vertex_attr.Rd +++ b/man/set_vertex_attr.Rd @@ -46,6 +46,7 @@ Vertex, edge and graph attributes \code{\link{igraph-vs-attributes}}, \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} diff --git a/man/set_vertex_attrs.Rd b/man/set_vertex_attrs.Rd new file mode 100644 index 00000000000..0e71b748803 --- /dev/null +++ b/man/set_vertex_attrs.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/attributes.R +\name{set_vertex_attrs} +\alias{set_vertex_attrs} +\title{Set multiple vertex attributes} +\usage{ +set_vertex_attrs(graph, index = V(graph), ...) +} +\arguments{ +\item{graph}{The graph.} + +\item{...}{Named arguments, where the names are the attributes} +} +\value{ +The graph, with the vertex attributes added or set. +} +\description{ +Set multiple vertex attributes +} +\examples{ +g <- make_ring(10) \%>\% + set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) +g +plot(g) +} +\seealso{ +Vertex, edge and graph attributes +\code{\link{delete_edge_attr}()}, +\code{\link{delete_graph_attr}()}, +\code{\link{delete_vertex_attr}()}, +\code{\link{edge_attr}()}, +\code{\link{edge_attr<-}()}, +\code{\link{edge_attr_names}()}, +\code{\link{graph_attr}()}, +\code{\link{graph_attr<-}()}, +\code{\link{graph_attr_names}()}, +\code{\link{igraph-attribute-combination}}, +\code{\link{igraph-dollar}}, +\code{\link{igraph-vs-attributes}}, +\code{\link{set_edge_attr}()}, +\code{\link{set_graph_attr}()}, +\code{\link{set_vertex_attr}()}, +\code{\link{vertex_attr}()}, +\code{\link{vertex_attr<-}()}, +\code{\link{vertex_attr_names}()} +} +\concept{attributes} diff --git a/man/vertex_attr-set.Rd b/man/vertex_attr-set.Rd index fa90ad3ff9b..fa7c367496b 100644 --- a/man/vertex_attr-set.Rd +++ b/man/vertex_attr-set.Rd @@ -53,6 +53,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr_names}()} } diff --git a/man/vertex_attr.Rd b/man/vertex_attr.Rd index c28515760dc..bb8c9d76ef2 100644 --- a/man/vertex_attr.Rd +++ b/man/vertex_attr.Rd @@ -48,6 +48,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr<-}()}, \code{\link{vertex_attr_names}()} } diff --git a/man/vertex_attr_names.Rd b/man/vertex_attr_names.Rd index c268a218e74..9f808191324 100644 --- a/man/vertex_attr_names.Rd +++ b/man/vertex_attr_names.Rd @@ -39,6 +39,7 @@ Vertex, edge and graph attributes \code{\link{set_edge_attr}()}, \code{\link{set_graph_attr}()}, \code{\link{set_vertex_attr}()}, +\code{\link{set_vertex_attrs}()}, \code{\link{vertex_attr}()}, \code{\link{vertex_attr<-}()} } From d3de1ddf8c193572811a873d87f3b26afbc2a6c7 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 19:49:45 +0200 Subject: [PATCH 06/13] removed unnecessary ellipsis --- R/attributes.R | 2 +- man/set_vertex_attr.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index b8e590772fb..1aaa4edcb54 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -526,7 +526,7 @@ vertex_attr <- function(graph, name, index = V(graph)) { #' set_vertex_attr("label", value = LETTERS[1:10]) #' g #' plot(g) -set_vertex_attr <- function(graph, name, index = V(graph), value, ...) { +set_vertex_attr <- function(graph, name, index = V(graph), value) { check_string(name) if (is_complete_iterator(index)) { return(i_set_vertex_attr( diff --git a/man/set_vertex_attr.Rd b/man/set_vertex_attr.Rd index 0570e138f27..0c4328cbb94 100644 --- a/man/set_vertex_attr.Rd +++ b/man/set_vertex_attr.Rd @@ -4,7 +4,7 @@ \alias{set_vertex_attr} \title{Set vertex attributes} \usage{ -set_vertex_attr(graph, name, index = V(graph), value, ...) +set_vertex_attr(graph, name, index = V(graph), value) } \arguments{ \item{graph}{The graph.} From c265b772de8697e22102fef0945fa67fd7bc9f24 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 19:53:34 +0200 Subject: [PATCH 07/13] use rlang::is_named --- R/attributes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/attributes.R b/R/attributes.R index 1aaa4edcb54..87d27a57ce8 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -563,7 +563,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { set_vertex_attrs <- function(graph, index = V(graph), ...) { dots <- list(...) - if (is.null(names(dots)) || any(names(dots) == "")) { + if (!rlang::is_named(dots)) { cli::cli_abort("All arguments in `...` must be named.") } From 742f4cd5341b543341d45e26396990575369f5e3 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 7 Jul 2025 20:08:43 +0200 Subject: [PATCH 08/13] added dyndots --- R/attributes.R | 4 ++-- man/set_vertex_attrs.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index 87d27a57ce8..3b594598c20 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -549,7 +549,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { #' Set multiple vertex attributes #' #' @param graph The graph. -#' @param ... Named arguments, where the names are the attributes +#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Named arguments, where the names are the attributes #' @return The graph, with the vertex attributes added or set. #' #' @family attributes @@ -561,7 +561,7 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { #' g #' plot(g) set_vertex_attrs <- function(graph, index = V(graph), ...) { - dots <- list(...) + dots <- list2(...) if (!rlang::is_named(dots)) { cli::cli_abort("All arguments in `...` must be named.") diff --git a/man/set_vertex_attrs.Rd b/man/set_vertex_attrs.Rd index 0e71b748803..7d382f0766f 100644 --- a/man/set_vertex_attrs.Rd +++ b/man/set_vertex_attrs.Rd @@ -9,7 +9,7 @@ set_vertex_attrs(graph, index = V(graph), ...) \arguments{ \item{graph}{The graph.} -\item{...}{Named arguments, where the names are the attributes} +\item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> Named arguments, where the names are the attributes} } \value{ The graph, with the vertex attributes added or set. From dc78740cb71b2f6a648cbc651b8fc447a4a8b93c Mon Sep 17 00:00:00 2001 From: schochastics Date: Tue, 8 Jul 2025 11:02:40 +0200 Subject: [PATCH 09/13] added index to docs --- R/attributes.R | 2 ++ man/set_vertex_attrs.Rd | 3 +++ 2 files changed, 5 insertions(+) diff --git a/R/attributes.R b/R/attributes.R index 3b594598c20..c6d42baa830 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -549,6 +549,8 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { #' Set multiple vertex attributes #' #' @param graph The graph. +#' @param index An optional vertex sequence to set the attributes +#' of a subset of vertices. #' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Named arguments, where the names are the attributes #' @return The graph, with the vertex attributes added or set. #' diff --git a/man/set_vertex_attrs.Rd b/man/set_vertex_attrs.Rd index 7d382f0766f..28076fb4a2f 100644 --- a/man/set_vertex_attrs.Rd +++ b/man/set_vertex_attrs.Rd @@ -9,6 +9,9 @@ set_vertex_attrs(graph, index = V(graph), ...) \arguments{ \item{graph}{The graph.} +\item{index}{An optional vertex sequence to set the attributes +of a subset of vertices.} + \item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> Named arguments, where the names are the attributes} } \value{ From 1a9fe7fea72d95947fb66682f4f9128f36b2138e Mon Sep 17 00:00:00 2001 From: schochastics Date: Tue, 8 Jul 2025 20:45:53 +0200 Subject: [PATCH 10/13] no pipe --- R/attributes.R | 4 ++-- man/set_vertex_attrs.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index c6d42baa830..daae8d73b69 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -558,8 +558,8 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { #' #' @export #' @examples -#' g <- make_ring(10) %>% -#' set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) +#' g <- make_ring(10) +#' g <- set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) #' g #' plot(g) set_vertex_attrs <- function(graph, index = V(graph), ...) { diff --git a/man/set_vertex_attrs.Rd b/man/set_vertex_attrs.Rd index 28076fb4a2f..f3a3a59fd46 100644 --- a/man/set_vertex_attrs.Rd +++ b/man/set_vertex_attrs.Rd @@ -21,8 +21,8 @@ The graph, with the vertex attributes added or set. Set multiple vertex attributes } \examples{ -g <- make_ring(10) \%>\% - set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) +g <- make_ring(10) +g <- set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) g plot(g) } From d894190662346c4e77f520860ae2a7ace790cc83 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 10 Jul 2025 20:01:01 +0200 Subject: [PATCH 11/13] moved dots behind index to enable splicing --- R/attributes.R | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/R/attributes.R b/R/attributes.R index daae8d73b69..9727136f008 100644 --- a/R/attributes.R +++ b/R/attributes.R @@ -549,9 +549,9 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { #' Set multiple vertex attributes #' #' @param graph The graph. +#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Named arguments, where the names are the attributes #' @param index An optional vertex sequence to set the attributes #' of a subset of vertices. -#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Named arguments, where the names are the attributes #' @return The graph, with the vertex attributes added or set. #' #' @family attributes @@ -559,11 +559,14 @@ set_vertex_attr <- function(graph, name, index = V(graph), value) { #' @export #' @examples #' g <- make_ring(10) -#' g <- set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) -#' g -#' plot(g) -set_vertex_attrs <- function(graph, index = V(graph), ...) { - dots <- list2(...) +#' set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) +#' # use splicing if suplying a list +#' x <- list(color = "red", name = LETTERS[1:10]) +#' set_vertex_attrs(g, !!!x) +#' # to set an attribute named "index" use `:=` +#' set_vertex_attrs(g, color = "blue", index := 10, name = LETTERS[1:10]) +set_vertex_attrs <- function(graph, ..., index = V(graph)) { + dots <- rlang::list2(...) if (!rlang::is_named(dots)) { cli::cli_abort("All arguments in `...` must be named.") From a02abb55a23ce890e9df63ecc6274d40d20e5556 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 10 Jul 2025 20:01:06 +0200 Subject: [PATCH 12/13] added tests --- tests/testthat/test-attributes.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-attributes.R b/tests/testthat/test-attributes.R index a20ec62e698..5e750306089 100644 --- a/tests/testthat/test-attributes.R +++ b/tests/testthat/test-attributes.R @@ -475,4 +475,9 @@ test_that("set_vertex_attrs() works", { expect_snapshot(error = TRUE, { set_vertex_attrs(g) }) + + attr_list <- list(age = 42, gender = "F") + g <- set_vertex_attrs(g, !!!attr_list) + expect_equal(V(g)$age, rep(42, vcount(g))) + expect_equal(V(g)$gender, rep("F", vcount(g))) }) From cd2b197a7fee15c5be4aba97d029afd62dce43ae Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 10 Jul 2025 20:01:36 +0200 Subject: [PATCH 13/13] document --- man/set_vertex_attrs.Rd | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/man/set_vertex_attrs.Rd b/man/set_vertex_attrs.Rd index f3a3a59fd46..a0d14aa0cd6 100644 --- a/man/set_vertex_attrs.Rd +++ b/man/set_vertex_attrs.Rd @@ -4,15 +4,15 @@ \alias{set_vertex_attrs} \title{Set multiple vertex attributes} \usage{ -set_vertex_attrs(graph, index = V(graph), ...) +set_vertex_attrs(graph, ..., index = V(graph)) } \arguments{ \item{graph}{The graph.} +\item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> Named arguments, where the names are the attributes} + \item{index}{An optional vertex sequence to set the attributes of a subset of vertices.} - -\item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> Named arguments, where the names are the attributes} } \value{ The graph, with the vertex attributes added or set. @@ -22,9 +22,12 @@ Set multiple vertex attributes } \examples{ g <- make_ring(10) -g <- set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) -g -plot(g) +set_vertex_attrs(g, color = "blue", size = 10, name = LETTERS[1:10]) +# use splicing if suplying a list +x <- list(color = "red", name = LETTERS[1:10]) +set_vertex_attrs(g, !!!x) +# to set an attribute named "index" use `:=` +set_vertex_attrs(g, color = "blue", index := 10, name = LETTERS[1:10]) } \seealso{ Vertex, edge and graph attributes