@@ -349,10 +349,7 @@ graph.attributes <- function(graph) {
349
349
# ' @export
350
350
" graph.attributes<-" <- function (graph , value ) {
351
351
ensure_igraph(graph )
352
- if (! is.list(value ) || (length(value ) > 0 && is.null(names(value ))) ||
353
- any(names(value ) == " " ) || any(duplicated(names(value )))) {
354
- stop(" Value must be a named list with unique names" )
355
- }
352
+ assert_named_list(value )
356
353
357
354
.Call(R_igraph_mybracket2_set , graph , igraph_t_idx_attr , igraph_attr_idx_graph , value )
358
355
}
@@ -535,10 +532,8 @@ vertex.attributes <- function(graph, index = V(graph)) {
535
532
" vertex.attributes<-" <- function (graph , index = V(graph ), value ) {
536
533
ensure_igraph(graph )
537
534
538
- if (! is.list(value ) || (length(value ) > 0 && is.null(names(value ))) ||
539
- any(names(value ) == " " ) || any(duplicated(names(value )))) {
540
- stop(" Value must be a named list with unique names" )
541
- }
535
+ assert_named_list(value )
536
+
542
537
if (any(sapply(value , length ) != length(index ))) {
543
538
stop(" Invalid attribute value length, must match number of vertices" )
544
539
}
@@ -744,10 +739,8 @@ edge.attributes <- function(graph, index = E(graph)) {
744
739
" edge.attributes<-" <- function (graph , index = E(graph ), value ) {
745
740
ensure_igraph(graph )
746
741
747
- if (! is.list(value ) || (length(value ) > 0 && is.null(names(value ))) ||
748
- any(names(value ) == " " ) || any(duplicated(names(value )))) {
749
- stop(" Value must be a named list with unique names" )
750
- }
742
+ assert_named_list(value )
743
+
751
744
if (any(sapply(value , length ) != length(index ))) {
752
745
stop(" Invalid attribute value length, must match number of edges" )
753
746
}
@@ -1189,3 +1182,20 @@ NULL
1189
1182
`$<-.igraph` <- function (x , name , value ) {
1190
1183
set_graph_attr(x , name , value )
1191
1184
}
1185
+
1186
+ assert_named_list <- function (value ) {
1187
+ error_msg <- " {.arg value} must be a named list with unique names"
1188
+
1189
+ if (! is.list(value )) {
1190
+ rlang :: abort(error_msg )
1191
+ }
1192
+
1193
+ if (length(value ) == 0 ) {
1194
+ return ()
1195
+ }
1196
+
1197
+ if (! rlang :: is_named(value ) || anyDuplicated(names(value )) > 0 ) {
1198
+
1199
+ rlang :: abort(error_msg )
1200
+ }
1201
+ }
0 commit comments