R data.tree 0.3 “Apple Tree” on CRAN: Now with tree plot!

[This article was first published on R – ipub, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In Celtic mythology, the apple tree is a symbol for fruitfulness. May data.tree blossom and fructify … and generate that perfect tree plot for us!

The hottest feature of the new release are tree plots: Thanks to the DiagrammeR package, we can now generate beautiful plots in html format. You can include them in RMarkdown documents, web pages, shiny apps, and more. And they may contain interactive tooltips and more.

In case you don’t know what data.tree is: It’s a generic data.structure in R, letting you model and work with hierarchic data with ease. There are two vignettes, walking you through the possibilities offered by the package:

data.tree introduction vignette

sample applications using data.tree

data.tree is useful when converting hierarchic data structures from and to R (e.g. JSON, YAML), for recursive programming algorithms, and much more. For example, the AHP package has been developed with data.tree. Check out the AHP sample application to learn more.

To install the package, simply type the following in R:

install.packages(data.tree)

To get started with tree data structures, you may want to look at acme, the package sample data.tree structure.

library(data.tree)
data(acme)
print(acme, "p", "cost", expectedCost = function(node) node$p * node$cost)

This will print the tree to the console:

levelName    p    cost expectedCost
Acme Inc.                          NA      NA           NA
 ¦--Accounting                     NA      NA           NA
 ¦   ¦--New Software             0.50 1000000       500000
 ¦   °--New Accounting Standards 0.75  500000       375000
 ¦--Research                       NA      NA           NA
 ¦   ¦--New Product Line         0.25 2000000       500000
 ¦   °--New Labs                 0.90  750000       675000
 °--IT                             NA      NA           NA
     ¦--Outsource                0.20  400000        80000
     ¦--Go agile                 0.05  250000        12500
     °--Switch to R              1.00   50000        50000

To generate a (non-styled) tree plot of the acme sample data, do the following:

plot(acme)

This will display a basic tree plot of our sample tree. It looks something like this:

acme tree plot

 

 

With a few directives, we can style this to our needs:

SetEdgeStyle(acme, arrowhead = "vee", color = "grey35", penwidth = 2)
SetNodeStyle(acme, style = "filled,rounded", shape = "box", fillcolor = "GreenYellow", fontname = "helvetica")
SetNodeStyle(acme$IT, fillcolor = "LightBlue", penwidth = "5px")
SetNodeStyle(acme$Accounting, inherit = FALSE, fillcolor = "Thistle", fontcolor = "Firebrick")
plot(acme)

acme tree plot with style

 

Styling is very flexible, though it takes advantage of the fact that data.tree structure are strictly hierarchical. For example, styling attributes are inherited from parents to children (except inheritance is set to FALSE).

The release contains also a few fixes and other improvements. See here for the release notes. And if you have questions or want to report a bug, then please do so directly on github. And if you want to get informed about solved issues, new releases, etc., please star the project on github.

So, to frame this post appropriately, let’s close with the following: In the Ulster Cycle, the soul of Cú Roí was confined in an apple that lay in the stomach of a salmon which appeared once every seven years. Not that I know what the Ulster Cycle is, or who in the world Cú Roí was. But it makes a nice story, as we all do hope hope that the next data.tree release will appear earlier than that!

The post R data.tree 0.3 “Apple Tree” on CRAN: Now with tree plot! appeared first on ipub.

To leave a comment for the author, please follow the link and comment on their blog: R – ipub.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)