I typically do this with a loop:
FileVector <- c("data1.Rdata", "data1.Rdata", "data1.Rdata")
Res <- vector(mode = "list",
length = length(FileVector))
for (m1 in seq_along(FileVector)) {
FilesLoaded <- load(file = FileVector[m1],
verbose = FALSE)
if ("filename" %in% FilesLoaded) {
Res[[m1]] <- get("filename")
}
rm(list = FilesLoaded)
}
This gives us a list, and we can add other checks to the loop to say, not add data that has some value in whatever column, or we can also check each new piece of data to make sure that certain column names are present. You can also wrap your load()
call in a try()
call if you have real world concerns like some data files not having generated properly. Then we just slam it all together with do.call()
# Null positions will be dropped
Res <- do.call(rbind,
Res)
It's usually advantageous to build your vector of file names with something like list.files()
and specifying the pattern =
argument.
Generally that might look something like:
FileVector <- list.files(path = "~/my/directory",
full.names = TRUE,
pattern = "mypattern")