All examples By author By category About

timelyportfolio

R | Interactive d3.js treemap of file directory with d3treeR and data.tree

Using d3treeR and data.tree, we can rather quickly get a treemap view of our directory and files.

Getting Ready

Let’s install the necessary packages if you do not already have them.

#devtools::install_github("timelyportfolio/d3treeR")
#devtools::install_github("gluc/data.tree")

# I'll use pipeR for piping, but feel free to use your favorite
#devtools::install_github("renkun-ken/pipeR") 

library("d3treeR")
library("data.tree")
library("pipeR")

Analyzing Your Directory

#  get all the files recursively in our directory
list.files(
  recursive = TRUE
  ,include.dirs = FALSE
) %>>%
  # supplement filename with file.info
  (
    data.frame(
      filename = sapply(
        .
        ,function(fl){
          paste0(tail(strsplit(getwd(),"/")[[1]],1),"/",fl)
        }
      )
      , file.info(.)
      , stringsAsFactors = FALSE
    )
  ) %>>%
  # convert to a data.tree
  as.Node(
    pathName = "filename"
  ) %>>%
  # convert to a list nested in the form of a d3.js hierarchy
  as.list( mode = "explicit", unname = TRUE ) %>>%
  d3tree2(  )