Skip to content

Instantly share code, notes, and snippets.

@halpo
Created December 20, 2011 22:46
Show Gist options
  • Save halpo/1503662 to your computer and use it in GitHub Desktop.
Save halpo/1503662 to your computer and use it in GitHub Desktop.
makes a graphviz plot of parsed R code from parser package
library(Rgraphviz)
library(plyr)
parserGraph <- function(x){
d0 <- attr(x, 'data')
d <- mutate(d0, id =as.character(id), parent=as.character(parent))
stopifnot(inherits(x, "parser"))
g <- new("graphNEL", nodes=as.character(d$id), edgemode='directed')
d_ply(d, .(id), with, {
if(parent %in% d$id)
g <<- addEdge(id, parent, g, 1)
})
nAttrs <- list()
nAttrs$label <- structure(ifelse(d$text!="", d$text,d$token.desc), names = d$id)
nAttrs$shape <- structure(mapToShapes(d$token.desc), names=d$id)
attrs <- list(node=list(shape="ellipse", fixedsize=FALSE), graph=list(rankdir="RL"))
plot(g, "dot", nodeAttrs = nAttrs, attrs = attrs)
}
unique(d$token.desc)
default.shape.mapping <- c(
ROXYGEN_COMMENT = "box")
mapToShapes <- function(y, mapping=default.shape.mapping, default.shape='ellipse'){
sh <- mapping[y]
structure(ifelse(is.na(sh), default.shape, sh), names=y)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment