I am trying to get this not to look like garbage. Any suggestions? Stole most of the code from the internet (partially from some raptor sympathizer named @trinker )

Code:
parseTree <- function(string, ignore=c('(',')','{','}'), ...) {
dat <- utils::getParseData(parse(text=string))
g <- parser2graph(dat[!(dat$text %in% ignore), ])
leaves <- V(g)[!degree(g, mode='out')] # tree leaves
preds <- sapply(leaves, neighbors, g=g, mode="in") # their predecessors
vertex_attr(g, 'label', preds) <- vertex_attr(g, 'label', leaves) # bump labels up a level
g <- g - leaves # remove the leaves
gaps <- V(g)[!nchar(vertex_attr(g, 'label'))] # gaps where ()/{} were
nebs <- c(sapply(gaps, neighbors, graph=g, mode='all')) # neighbors of gaps
g <- add_edges(g, nebs, color='blue') # edges around the gaps
g <- g - V(g)[!nchar(vertex_attr(g, 'label'))] # remove leaves/gaps
plot(g, layout=layout.reingold.tilford, ...)
title(string, cex.main=2.5)
}
Code:
string = "(2*Na) + (Glucose / 18) + (BUN / 2.8)"
library(igraph)
string <- "(a/{5})+(2*b+c)"
parseTree(string, # plus some graphing stuff
vertex.color="#FCFDBFFF", vertex.frame.color=NA,
vertex.label.font=2, vertex.label.cex=2,
vertex.label.color="darkred", vertex.size=25,
asp=.8, edge.width=2, margin= -0.9)

Last edited: