Convert data.tree to and from list, json, networkD3, and more

[This article was first published on ipub » R, 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.

The data.tree package provides a facility to create and manage tree structures in R, and to decorate nodes with methods. This comes in handy for many algorithms. For an example, see here. Or read up the vignettes in the package, e.g. from CRAN.

In the last post, I showed how we can write a custom converter to convert a data.tree Node to a JSON string. For standard conversions, this has just become extremely simple. As of today, data.tree contains generic methods to convert a data.tree from and to a list.

Convert a Node to a list

Converting a data.tree Node to a list is really simple:

library(data.tree)
 
data(acme)
l <- acme$ToList()

Alternatively, you might prefer the “traditional” generic method:

as.list(acme)

Convert a Node to JSON

Conversion to JSON becomes trivial:

library(rjson)
cat(toJSON(l))

The result is (doing some pretty-printing):

{
  "name": "Acme Inc.",
  "children": {
    "Accounting": {
      "name": "Accounting",
      "children": {
        "New Software": {
          "name": "New Software",
          "cost": 1000000,
          "p": 0.5
        },
        "New Accounting Standards": {
          "name": "New Accounting Standards",
          "cost": 500000,
          "p": 0.75
        }
      }
    },
    "Research": {
      "name": "Research",
      "children": {
        "New Product Line": {
          "name": "New Product Line",
          "cost": 2000000,
          "p": 0.25
        },
        "New Labs": {
          "name": "New Labs",
          "cost": 750000,
          "p": 0.9
        }
      }
    },
    "IT": {
      "name": "IT",
      "children": {
        "Outsource": {
          "name": "Outsource",
          "cost": 400000,
          "p": 0.2
        },
        "Go agile": {
          "name": "Go agile",
          "cost": 250000,
          "p": 0.05
        },
        "Switch to R": {
          "name": "Switch to R",
          "cost": 50000,
          "p": 1
        }
      }
    }
  }
}

If we prefer our children to be in an array, we can use the unname flag. This will cause the nested children elements to be unnamed in the list:

cat(toJSON(acme$ToList(unname = TRUE)))

This yields:

{
  "name": "Acme Inc.",
  "children": [
    {
      "name": "Accounting",
      "children": [
        {
          "name": "New Software",
          "cost": 1000000,
          "p": 0.5
        },
        {
          "name": "New Accounting Standards",
          "cost": 500000,
          "p": 0.75
        }
      ]
    },
    {
      "name": "Research",
      "children": [
        {
          "name": "New Product Line",
          "cost": 2000000,
          "p": 0.25
        },
        {
          "name": "New Labs",
          "cost": 750000,
          "p": 0.9
        }
      ]
    },
    {
      "name": "IT",
      "children": [
        {
          "name": "Outsource",
          "cost": 400000,
          "p": 0.2
        },
        {
          "name": "Go agile",
          "cost": 250000,
          "p": 0.05
        },
        {
          "name": "Switch to R",
          "cost": 50000,
          "p": 1
        }
      ]
    }
  ]
}

There are multiple options in the ToList method. Type the following to learn more:

?ToList
?as.list.Node

Namely, you can call the reserved-word “name” and “children” differently when importing and exporting, if you want.

Convert a list to a Node

As a side note: you can also convert a list to a Node:

as.Node(l)

##                           levelName
## 1  Acme Inc.                       
## 2   ¦--Accounting                  
## 3   ¦   ¦--New Software            
## 4   ¦   °--New Accounting Standards
## 5   ¦--Research                    
## 6   ¦   ¦--New Product Line        
## 7   ¦   °--New Labs                
## 8   °--IT                          
## 9       ¦--Outsource               
## 10      ¦--Go agile                
## 11      °--Switch to R

 

networkD3

This opens endless possibilities for integration with other packages. For example, to draw a tree with the networkD3 package:

library(networkD3)
treeNetwork(acme$ToList(unname = TRUE))

The result is here. Not very meaningful, I agree, but you get the idea.

Note that the ToList feature is not yet rolled out to CRAN, but you can easily try it out by downloading it from github:

devtools::install_github("gluc/data.tree")

Next, I’ll be working on converting a data.tree to a dendrogram for plotting, and to a party object (from the partykit package) for access to partinioning algorithms.

The post Convert data.tree to and from list, json, networkD3, and more appeared first on ipub.

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

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)