This sets an attribute list for all vertices of the graph.
set_vertex_attributes(g, name, attributes)
g | A nctx graph |
---|---|
name | Name of the attribute |
attributes | The list of attribute values for the nctx graph. |
Setting attributes for vertices is mainly used to export them to files, e.g. using save_graph
.
Association between attributes and vertices is via the position in the attribute list, i.e. attributes for the 4th vertex are taken from the 4th position of the attribute list.
Note that setting attribute lists with an existing name will override previous attribute lists.
library(RUnit) library(nctx) file <- system.file("extdata", "bcde.ctx.dir.graphml", package="nctx") g <- load_graph(file, directed = TRUE) prop_names <- get_vertex_attribute_names(g) checkTrue(all(prop_names %in% c("context","distance_to_A","distance_to_B","distance_to_C","distance_to_D","distance_to_E","distance_to_F","distance_to_G","distance_to_H","distance_to_I","distance_to_J","name")))#> [1] TRUEchr_test <- tail(letters, n = num_vertices(g)) set_vertex_attributes(g, "chr_test", chr_test) bool_test <- sample(c(TRUE, FALSE), num_vertices(g), TRUE) set_vertex_attributes(g, "bool_test", bool_test) int_test <- as.integer(sample(c(1,2,3,4,5), num_vertices(g), replace = TRUE)) set_vertex_attributes(g, "int_test", int_test) save_graph(g, file = "bla.graphml") g2 <- load_graph("bla.graphml", directed = TRUE) checkTrue(all(get_vertex_attribute_names(g2) %in% c("bool_test", "chr_test", "int_test", "context","distance_to_A","distance_to_B","distance_to_C","distance_to_D","distance_to_E","distance_to_F","distance_to_G","distance_to_H","distance_to_I","distance_to_J", "name")))#> [1] TRUEbool_loaded <- as.logical(get_vertex_attributes(g2, "bool_test")) checkEquals(bool_loaded,bool_test)#> [1] TRUE#> [1] TRUE#> [1] TRUE#> [1] TRUE#> [1] TRUE