Here is a solution using recursion:
tree.list <- function(file.or.dir) {
isdir <- file.info(file.or.dir)$isdir
if (!isdir) {
out <- file.or.dir
} else {
files <- list.files(file.or.dir, full.names = TRUE,
include.dirs = TRUE)
out <- lapply(files, tree.list)
names(out) <- basename(files)
}
out
}
I have tested it here on a small directory
test.dir <- tree.list("./test")
test.dir
# $a
# $a$`1.txt`
# [1] "./test/a/1.txt"
#
# $a$aa
# $a$aa$`2.txt`
# [1] "./test/a/aa/2.txt"
#
# $b
# $b$`3.txt`
# [1] "./test/b/3.txt"
If this is too slow for your needs, I would consider reading all the files into a single call to list.files
with recursive = TRUE
then do a bit of parsing.