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/R/attributes.R b/R/attributes.R index 7bac81c39fa..9727136f008 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. @@ -527,12 +528,61 @@ vertex_attr <- function(graph, name, index = V(graph)) { #' plot(g) set_vertex_attr <- function(graph, name, index = V(graph), value) { check_string(name) - if (is_complete_iterator(index)) { - i_set_vertex_attr(graph = graph, name = name, value = value, check = FALSE) + return(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) + return(i_set_vertex_attr( + graph = graph, + name = name, + index = index, + value = value + )) + } + graph +} + +#' 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. +#' @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]) +#' # 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.") } + + 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( 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 cdefdad3915..0c4328cbb94 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..a0d14aa0cd6 --- /dev/null +++ b/man/set_vertex_attrs.Rd @@ -0,0 +1,53 @@ +% 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{...}{<\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.} +} +\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]) +# 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 +\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<-}()} } 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..5e750306089 100644 --- a/tests/testthat/test-attributes.R +++ b/tests/testthat/test-attributes.R @@ -465,3 +465,19 @@ 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) + }) + + 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))) +})