diff --git a/.Rbuildignore b/.Rbuildignore index 4cc16b4..a54185a 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -21,3 +21,4 @@ demo/gexfplot\.R ^dependencies$ ^makefile$ ^LICENSE\.md$ +^tests/rundemos\.R$ diff --git a/ChangeLog b/ChangeLog index 365177a..2171079 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,8 @@ 2017-11-27 George G. Vega Yon * R/bugs.r: Function to map colors of a single node. - * R/igraph.r, R/read.gexf.r: Mapping positions, size and colors. Still a - bit buggy. + * R/igraph.r, R/read.gexf.r: Mapping positions, size and colors, ~~Still a + bit buggy.~~ and positions. 2017-11-13 George G. Vega Yon diff --git a/inst/LICENSE b/LICENSE similarity index 100% rename from inst/LICENSE rename to LICENSE diff --git a/NEWS.md b/NEWS.md index 0d0bc7e..763e1c6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# Changes in rgexf version 0.16.0 (2017-11-06) +# Changes in rgexf version 0.16.0 (2017-11-27) ## New features and changes @@ -20,6 +20,10 @@ * `gexf` has a new argument, `rescale.node.size`. When set to `TRUE`, the `size` vector in `nodesVizAtt` is rescaled such that when calling the plot method the largest node spans roughly 5% of the plot. + +* `read.gexf` now parses viz attributes (color, position and size). + +* `igraph.to.gexf` and vice versa now pass viz attributes and layout. ## New functions diff --git a/R/igraph.r b/R/igraph.r index 3632e3b..fcd1b01 100644 --- a/R/igraph.r +++ b/R/igraph.r @@ -27,7 +27,8 @@ #' igraph1 <- gexf.to.igraph(gexf1) #' gexf2 <- igraph.to.gexf(igraph1) #' -#' # Now, let's do it with a layout! +#' # Now, let's do it with a layout! (although we can just use +#' # the one that comes with lesmiserables :)) #' pos <- igraph::layout_nicely(igraph1) #' plot( #' igraph.to.gexf(igraph1, nodesVizAtt = list(position=cbind(pos, 0))), @@ -42,48 +43,24 @@ igraph.to.gexf <- function(igraph.obj, ...) { # Retrive elements from igraph object gdata <- igraph::get.data.frame(g, what="both") - tmpedges <- gdata$edges - tmpnodes <- gdata$vertices - + edges <- gdata$edges + nodes <- cbind(id = 1L:igraph::vcount(g), gdata$vertices) + # If nodes have no name - if (!("name" %in% colnames(tmpnodes))) - tmpnodes <- data.frame(tmpnodes, name=1:nrow(tmpnodes)) - - # Nodes and edges list - # these steps are dangerous as nodes that are not connected are dropped, creating a mismatched number of rows -# nodes <- edge.list(tmpedges[,c(1,2)]) -# edges <- nodes$edges -# nodes <- nodes$nodes - - nodes <- tmpnodes - # change name to label - names(nodes) <- 'label' - # add a column to nodes to hold the IDs - nodes$id <- as.integer(as.factor(nodes$label)) - # put them in the right order - nodes <- nodes[, c('id', 'label')] - - ## replace from and to in edges with their IDs - # first create a named vector of ids - nodesID <- nodes$id - names(nodesID) <- gdata$vertices$name - - edges <- tmpedges - # now in the edges create the id columns - edges$source <- nodesID[edges$from] - edges$target <- nodesID[edges$to] - # keep just source and target - edges <- edges[, c('source', 'target')] + namecol <- which(colnames(nodes) == "name") + if (!length(namecol)) + nodes <- data.frame(nodes, label=nodes$id) + else + nodes$label <- nodes$name - # Building nodes - if (length(tmpnodes)) { - nodes <-merge(tmpnodes,nodes, by.x="name", by.y="label") -# nodes <- nodes[,!(colnames(nodes) %in% c("label"))] - } + + edges$source <- nodes$id[match(edges$from, nodes$label)] + edges$target <- nodes$id[match(edges$to, nodes$label)] + edges <- edges[, c('source', 'target')] # Nodes Attributes x <- igraph::list.vertex.attributes(g) - x <- x[!(x %in% c("label","color","size"))] + x <- x[!(x %in% c("label","color","size", "name"))] if (length(x)) dots$nodesAtt <- subset(nodes, select=x) @@ -91,7 +68,7 @@ igraph.to.gexf <- function(igraph.obj, ...) { x <- igraph::list.edge.attributes(g) x <- x[!(x %in% c("weight","color","edgesLabel","width"))] if (length(x)) - dots$edgesAtt <- subset(tmpedges, select=x) + dots$edgesAtt <- subset(edges, select=x) # Edges Weights if (!length(dots$edgesWeight) && length(igraph::E(g)$weight)) @@ -101,14 +78,14 @@ igraph.to.gexf <- function(igraph.obj, ...) { # Nodes Viz att if (!length(dots$nodesVizAtt$color)) { - if (length(tmpnodes$color)) { - dots$nodesVizAtt$color <- tmpnodes$color + if (length(nodes$color)) { + dots$nodesVizAtt$color <- nodes$color } else dots$nodesVizAtt$color <- NULL } - if (!length(dots$nodesVizAtt$size) && length(tmpnodes$size)) - dots$nodesVizAtt$size <- tmpnodes$size + if (!length(dots$nodesVizAtt$size) && length(nodes$size)) + dots$nodesVizAtt$size <- nodes$size positions <- igraph::graph_attr(g, "layout") @@ -119,8 +96,8 @@ igraph.to.gexf <- function(igraph.obj, ...) { # Edges Viz att if (!length(dots$edgesVizAtt$color)) { - if (length(tmpedges$color)) { - dots$edgesVizAtt$color <- tmpedges$color + if (length(edges$color)) { + dots$edgesVizAtt$color <- edges$color } else dots$edgexVizAtt$color <- NULL } @@ -137,8 +114,8 @@ igraph.to.gexf <- function(igraph.obj, ...) { # Building graph do.call(gexf, c( list( - nodes = nodes[,c("id","name")], - edges = edges + nodes = nodes[, c("id","name")], + edges = edges[, c("source", "target")] ), dots) ) @@ -171,7 +148,10 @@ gexf.to.igraph <- function(gexf.obj) { igraph::V(g2)$size <- x$value if (length(x <- g$nodesVizAtt$position)) - g2 <- igraph::set_graph_attr(g2, "layout", unname(x)) + g2 <- igraph::set_graph_attr( + g2, "layout", + unname(as.matrix(x)) + ) # Nodes atts if (length(x <- g$nodes[, !(colnames(g$nodes) %in% c("id", "label"))])) diff --git a/R/read.gexf.r b/R/read.gexf.r index cca01ff..035a8d0 100644 --- a/R/read.gexf.r +++ b/R/read.gexf.r @@ -161,11 +161,13 @@ read.gexf <- function(x) { class(graph) <- "gexf" + order <- order(as.integer(graph$nodes$id)) + build.and.validate.gexf( - nodes = graph$nodes, + nodes = graph$nodes[order, , drop=FALSE], edges = graph$edges, atts.definitions = graph$atts.definitions, - nodesVizAtt = nodesVizAtt, + nodesVizAtt = lapply(nodesVizAtt, "[", i=order, j=, drop=FALSE), edgesVizAtt = edgesVizAtt, graph = graph$graph ) diff --git a/demo/gexfigraph.R b/demo/gexfigraph.R index 68b66f7..c8aea93 100644 --- a/demo/gexfigraph.R +++ b/demo/gexfigraph.R @@ -1,3 +1,3 @@ gexf1 <- read.gexf(system.file("gexf-graphs/lesmiserables.gexf", package="rgexf")) igraph1 <- gexf.to.igraph(gexf1) -gexf2 <- igraph.to.gexf(igraph1) \ No newline at end of file +gexf2 <- igraph.to.gexf(igraph1) diff --git a/inst/NEWS b/inst/NEWS index 5dcd060..390f633 100644 --- a/inst/NEWS +++ b/inst/NEWS @@ -1,32 +1,34 @@ -CHANGES IN RGEXF VERSION 0.16.0 (2017-11-06) +CHANGES IN RGEXF VERSION 0.16.0 (2017-11-27) New features and changes -- Modernization of the project (roxygen, new CRAN standards, etc.) - -- Updated emails. - -- Remove broken links. - - Function write.gexf has a new argument for specifying the GEXF version for now it only changes the header. - plot.gexf method now uses gexf-js instead of sigma.js. - Now igraph.to.gexf passes arguments to gexf. Before it was only - passing position. This way users have more flexibility specifying - attributes. + passing position. This way users have more flexibility + specifying attributes. - gexf's nodesVizAtt now has defaults for color, size and position. This is a requirement of gexf-js. +- gexf's nodesVizAtt color and size now support passing a default for + all the nodes. Also, color can be specified as a character scalar + (name of the) color, or as an integer (number in colors()). + - gexf has a new argument, rescale.node.size. When set to TRUE, the size vector in nodesVizAtt is rescaled such that when calling the plot method the largest node spans roughly 5% of the plot. +- read.gexf now parses viz attributes (color, position and size). + +- igraph.to.gexf and vice versa now pass viz attributes and layout. + New functions @@ -36,6 +38,15 @@ New functions vers 0.17.0 +Misc changes + +- Modernization of the project (roxygen, new CRAN standards, etc.) + +- Updated emails. + +- Remove broken links. + + CHANGES IN RGEXF VERSION 0.15.3 (2015-03-24) @@ -70,9 +81,9 @@ Bug fixes Development -- Option -keepFactors- default is now in -FALSE- (used to be in - -TRUE-). When set to -TRUE- and there are factors, a warning message - will appear (reported in issue 18, thanks Tim Smith!). +- Option -keepFactors- default is now in -FALSE- (used to be + in -TRUE-). When set to -TRUE- and there are factors, a warning + message will appear (reported in issue 18, thanks Tim Smith!). @@ -81,8 +92,8 @@ CHANGES IN RGEXF VERSION 0.13.11 (2013-11-27) Bug fixes -- Included class checks in -gexf- class functions (thanks to Samuel - Finegold). +- Included class checks in -gexf- class functions (thanks to + Samuel Finegold). - write.gexf does not fails when dynamics different from double are passed (thank you Samuel Finegold!). @@ -120,8 +131,8 @@ CHANGES IN RGEXF VERSION 0.13.05 (2013-05-09) Bug fixes -- sprintf error when using other formats rather than double (issue - 10). +- sprintf error when using other formats rather than double + (issue 10). - in .addNodesEdges, add support to case “!attributes && vizattributes” (issue 9). @@ -163,8 +174,8 @@ Bug fixes Development - .addNodesEdges rewritten now works faster in most of CPUs (some of - them with very high speedups) (Thanks to Duncan Temple Lang, RXML - author) + them with very high speedups) (Thanks to Duncan Temple Lang, + RXML author) - Several code routines have been extracted from “bigger functions” and written as functions themselves. @@ -180,8 +191,8 @@ New features and changes rm.gexf.node and rm.gexf.edge allow to build and manipulate gexf objects from scratch. -- New function read.gexf allows to import gexf files as gexf class - objects. +- New function read.gexf allows to import gexf files as gexf + class objects. - gexf function now it is called write.gexf. @@ -192,8 +203,8 @@ New features and changes - Real-life datasets have been includded. -- New function edge.list builds a dataframe of nodes from an edge - list. +- New function edge.list builds a dataframe of nodes from an + edge list. - New methods for gexf objects: print.gexf and summary.gexf. diff --git a/man/igraph.to.gexf.Rd b/man/igraph.to.gexf.Rd index e300372..e664748 100644 --- a/man/igraph.to.gexf.Rd +++ b/man/igraph.to.gexf.Rd @@ -40,7 +40,8 @@ include the \code{position} viz-attribute. igraph1 <- gexf.to.igraph(gexf1) gexf2 <- igraph.to.gexf(igraph1) - # Now, let's do it with a layout! + # Now, let's do it with a layout! (although we can just use + # the one that comes with lesmiserables :)) pos <- igraph::layout_nicely(igraph1) plot( igraph.to.gexf(igraph1, nodesVizAtt = list(position=cbind(pos, 0))), diff --git a/tests/testthat/test-examples.r b/tests/testthat/test-examples.r index f056189..57370e6 100644 --- a/tests/testthat/test-examples.r +++ b/tests/testthat/test-examples.r @@ -1,3 +1,3 @@ -# context("Examples") +context("Examples") -# test_examples() +test_examples()