Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Visualizing Folder Paths

Tags:

r

I am working with the R programming language.

I am trying to make a network/tree visualization that shows all the folders, subfolders and files that are located on my computer.

I found the following code which can list all files/folders on my computer:

dirs <- list.dirs(path = ".", full.names = TRUE, recursive = TRUE)

From here, I want to make a network diagram visualization (e.g. using igraph) to show all the paths to folders, subfolders and files:

enter image description here https://www.reddit.com/r/dataisbeautiful/comments/ajcb4n/i_made_a_graph_visualization_of_my_projects/

I found this post here that shows how to do this for a limited setting: How to build a dendrogram from a directory tree?

But I am trying to directly do this for this statement

dirs <- list.dirs(path = ".", full.names = TRUE, recursive = TRUE)

Can someone please show me how to do this?

Thanks!

My attempt:

library(igraph)
library(data.tree)

dirs <- list.dirs(path = ".", full.names = TRUE, recursive = TRUE)

edges <- data.frame(from = dirname(dirs), to = basename(dirs), stringsAsFactors = FALSE)

g <- graph_from_data_frame(edges, directed=TRUE)

plot(g, layout=layout_as_tree)
like image 321
stats_noob Avatar asked Jul 13 '26 18:07

stats_noob


2 Answers

You could do this using tidygraph and ggraph:

library(igraph)
library(tidygraph)
library(ggraph)

do.call('rbind', 
  strsplit(dirs, '/') |>
  lapply(\(x) sapply(seq_along(x), \(y) paste(x[1:y], collapse = '/'))) |>
  lapply(\(x) cbind(head(x, -1), tail(x, -1)))
  ) |>
  as.data.frame() |>
  unique() |>
  graph_from_data_frame() |>
  as_tbl_graph() %>%
  mutate(label = gsub('^.*/(.*)$', '\\1', name)) |>
  ggraph(layout = 'tree') + 
  geom_edge_diagonal(color = 'gray') +
  geom_node_point(shape = 21, fill = 'lightblue') +
  geom_node_text(aes(label = label), size = 3, nudge_x = 0.4) +
  coord_flip(clip = 'off') +
  scale_y_reverse() +
  theme_graph()

enter image description here

like image 135
Allan Cameron Avatar answered Jul 15 '26 08:07

Allan Cameron


I have a few pointers: First, your Tree does not generate correctly, because dirnames does not give the name of the directory, but the whole name.

You can fix this by leaving out the basename function in the definition of your edges data.frame. This might also help with duplicate folder names. You can still change the names with

V(g)$name <- basename(V(g)$name)

Second, if you want to have all files and directories your first line should probably be

filesAndDirs <- list.files(path = ".", full.names = TRUE, recursive = TRUE, include.dirs = TRUE)

And finally, when plotting you should probably add the root of your tree:

plot(g, layout=layout_as_tree(g, root = 1))

I can't really help on making a beautiful graph, but I hope this helps anyway.

The whole code is:

library(igraph)

filesAndDirs <- list.files(path = ".",
                           full.names = TRUE, 
                           recursive = TRUE, 
                           include.dirs = TRUE)

edges <- data.frame(from = dirname(filesAndDirs), to = filesAndDirs, stringsAsFactors = FALSE)

g <- graph_from_data_frame(edges, directed=TRUE)
V(g)$name <- basename(V(g)$name)

plot(g, layout=layout_as_tree(g, root = 1))
like image 36
backboned Avatar answered Jul 15 '26 08:07

backboned



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!