I have:
- directories (let's say two: A and B) that contain files;
- two character objects storing the directories (
dir_A
,dir_B
); - a function that takes the directory as argument and returns the list of the names of the files found there (in a convenient way for me that is different from
list.files()
).
directories <- c(dir_A, dir_B)
read_names <- function(x) {foo}
Using a for-loop, I want to create objects that each contain the list of files of a different directory as given by read_names()
. Essentially, I want to use a for-loop to do the equivalent as:
files_A <- read_names(dir_A)
files_B <- read_names(dir_B)
I wrote the loop as follows:
for (i in directories) {
assign(paste("files_", sub('.*\\_', '', deparse(substitute(i))), sep = ""), read_names(i))
}
However, although outside of the for-loop deparse(substitute(dir_A))
returns "dir_A" (and, consequently, the sub()
function written as above would return "A"), it seems to me that in the for-loop substitute(i)
makes i
stop being one of the directories, and just being i
.
It follows that deparse(substitute(i))
returns "i"
and that the output of the for-loop above is only one object called files_i
, which contains the list of the files in the last directory of the iteration because that is the last one that has been overwritten on files_i
.
How can I make the for-loop read the name (or part of the name in my case, but it is the same) of the object that i
is representing in that moment?