我正在R环境中包含多个子文件夹的文件夹中工作。我想在多个子文件夹上循环,然后在每个子文件夹中调用R脚本执行。我想出了下面的代码。但我的代码似乎在添加“。”到子文件夹列表,我得到错误(“文件错误(文件名,”r“,编码=编码): 无效的'description'参数“)。虽然调用R脚本没有问题。
代码:
for (x in list.dirs()){
folder <- list.dirs(x)
script <- paste(folder,"/","process.R", sep="")
source(script)
}
这样做的正确方法是什么?我认为我所做的可能不对。
答案 0 :(得分:2)
类似以下工作:
# Folder containing sub-folders
parent.folder <- "/home/tony/Desktop/test"
# Sub-folders
sub.folders <- list.dirs(parent.folder, recursive=TRUE)[-1]
# R script file paths
r.scripts <- file.path(sub.folders, "process.R")
# Run scripts in sub-folders
for(script in r.scripts) {
source(script)
}
答案 1 :(得分:1)
如果我理解正确,您在多个子文件夹中有一个名为process.R
的脚本。您想要运行所有这些脚本。如果是这种情况,我想以下可行:
folder <- list.dirs() # this assumes being in the main folder, otherwise specify the path
folder <- folder[-1] # comment this out if the main folder also contains the script
script.paths <- paste(folder,"/","process.R", sep="")
for (i in script.paths) {
source(i)
}