I often teach R to my peers and getting to explain the structure of nested data such as nested lists can be somewhat an arduous task and I find that creating a visual aid can go a long way.
However the output of a function such as str()
has a lot of information and is not the most human readable format so I have attempted to format this output to then use RegEx to a more readable output. I have experienced some caveats as well as not being very proficient in string manipulation I was hoping that I could get some help.
Given the following object:
object <- list(
a = 1:5,
b = matrix(c(1, 3, "a", "i"), byrow = TRUE),
l1 = list(
data = data.frame(
x = letters,
y = LETTERS
),
vec = "The river",
l2 = list(
abc = seq(1, 9, by = 2),
col = "#445f43"
)
),
data2 = data.frame(
x = c("a","h"),
y = runif(2, 9, 90)
),
rand = runif(12, 99, 120),
form = y~x^4
)
And the expected output would be a tree renderization:
object
├── a 'int'
├── b 'chr'
├── l1 'list'
│ ├── data 'data.frame'
│ │ ├── x 'factor'
│ │ └── y 'factor'
│ ├── vec 'chr'
│ └── l2 'list'
│ ├── abc 'chr'
│ └── col 'chr'
├── data2 'data.frame'
│ ├── x 'factor'
│ └── y 'num'
├── rand 'num'
└── form 'formula'
I would like to write a function that gives this output as well as adding some arguments to also return the length of the elements of the list and other information and perhaps color-coded classes.
EDIT: Just found other questions similar to mine here: and here: