Reading all scripts and data files from multiple folders
for (f in list.files(pattern="*.R")) {
source(f)
}
for (f in list.files(pattern="*.rda")) {
load(f)
}
Since .rda
requires load
and .R
requires source
, I would do something like this:
file.sources = list.files(pattern="*.R")
data.sources = list.files(pattern="*.rda")
sapply(data.sources,load,.GlobalEnv)
sapply(file.sources,source,.GlobalEnv)
Update for reading from multiple folders at once
file.sources = list.files(c("C:/folder1", "C:/folder2"),
pattern="*.R$", full.names=TRUE,
ignore.case=TRUE)
data.sources = list.files(c("C:/folder1", "C:/folder2"),
pattern="*.rda$", full.names=TRUE,
ignore.case=TRUE)
sapply(data.sources,load,.GlobalEnv)
sapply(file.sources,source,.GlobalEnv)
Notice also the use of $
at the end of the search pattern, to make sure it matches only, say, a .R
at the end of a line, and the use of ignore.case
in case some of the files are named, say, script.r
.