Site icon R-bloggers

Little useless-useful R functions – Folder Treemap

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

Welcome to new year 2021! Wish you all a great start and a healthy, prosperous and wonderful year.

Brand new simple useless function will generate a Treemap with the value of subfolder size and the given names of the subfolder on a given client machine.

The idea is simple, check the distribution of the subfolders in size for a given folder.

This can be achieved easy and fast with useless R function:

FolderTreemap <- function(directory){
  df <- NULL
  aa <- list.files(directory,full.names=TRUE)
  dirs <- aa[file.info(aa)$isdir]
  for (i in 1:length(dirs)){
    name_f <- basename(dirs[i])
    size_f <- sum(file.info(list.files(path=dirs[i], recursive = T, full.names = T))$size)
    df <- rbind(df, data.frame(size=size_f/(1024*1024), folder=name_f))
  }
  p <- treemap(df,
               index=c("folder"),
               vSize="size",
               type="index",
               palette = "Set2",
               bg.labels=c("white"),
               title = paste0("Total size of folder: ",directory, " is: ", as.integer(sum(df$size)), " MiB.", collapse=NULL),
               align.labels=list(c("center", "center"), c("right", "bottom"))  
  )            
}

A lot of improvements is available here, but the idea is to keep it simple and with many steps as possible.

The only downside is, that treemap as a visual is not available in base R engine, so additional library was loaded:

library(treemap)

Once you load the function and the library, just add the path and call the function:

input_directory <- "/users/tomazkastrun/Documents/Github"
FolderTreemap(input_directory)

Plenty of useless and pointless, but again useful visualization.

As always, code is available at Github.

Happy R-coding! And stay healthy!

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

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.