I have a set of .rmd files that I want to order into a list. I am using the file weight (defined in the files YAML header) to indicate desired position of that file, relative to its neighbors.
Example directory structure:
.
├── dir1
│ ├── dir3
│ │ ├── index.rmd (weight: 1)
│ │ └── file5.rmd (weight: 1)
│ ├── index.rmd (weight: 1)
│ └── file4.rmd (weight: 1)
├── dir2
│ ├── index.rmd (weight: 2)
│ ├── file6.rmd (weight: 1)
│ └── file7.rmd (weight: 2)
├── index.rmd (weight: 1)
├── file2.rmd (weight: 2)
└── file3.rmd (weight: 3)
The goal is to generate a list of files by weight order, starting with .
and then iterating through the directories in weight order (as determined by the weight of their index and the weight of their parent). E.g. For the above directory, the order would be:
./index.rmd
./file2.rmd
./file3.rmd
dir1/index.rmd
dir1/file4.rmd
dir1/dir3/index.rmd
dir1/dir3/file5.rmd
dir2/index.rmd
dir2/file6.rmd
dir2/file7.rmd
I can generate a list of files or directories with list.files(filetree, '[.]Rmd$', ignore.case = TRUE, recursive = TRUE)
or unique(dirname(files))
respectively, and can access the weight with yaml_front_matter(filename)$weight
but am struggling with the meat of it; in particular how to avoid doing this with repeated nested for loops.