Can I write one R script to open and run every R file in a folder?
我可以写一个R脚本来打开并运行文件夹中的每个R文件吗?
I know how to check for the presence of a file in a folder, how to read every file in a folder as a text connection and how to read every data file in a folder.
我知道如何检查文件夹中文件的存在,如何读取文件夹中的每个文件作为文本连接,以及如何读取文件夹中的每个数据文件。
However, I want to execute every R script in a folder one at a time, ideally using a single R script and the default R gui installed on a Windows desktop during installation.
但是,我希望每次都在一个文件夹中执行每个R脚本,理想情况下,在安装过程中使用一个R脚本和默认的R gui安装在Windows桌面上。
I suspect I might need to run R from the command line instead and write some sort of batch file to do this. I have rarely run R from the command line and never written a batch file for R.
我怀疑我可能需要从命令行运行R,并编写某种批处理文件来实现这一点。我很少从命令行运行R,也从不为R编写批处理文件。
Here are some example R scripts all stored in a folder named run_all_these
:
下面是一些示例R脚本,它们都存储在名为run_all_these的文件夹中:
The file run.one.r
contains:
文件run.one。r包含:
a <- 10
b <- 20
c <- a+b
c
The file run.two.r
contains:
文件run.two。r包含:
a <- 10
b <- 20
c <- a-b
c
The file run.three.r
contains:
文件run.three。r包含:
a <- 10
b <- 20
c <- a*b
c
The file run.four.r
contains:
文件run.four。r包含:
a <- 10
b <- 20
c <- a/b
c
I found virtually nothing on this topic using Google. Although, I did find a little on batch files here:
使用谷歌,我几乎找不到这个主题。虽然,我在这里找到了一些批次文件:
http://cran.r-project.org/bin/windows/base/rw-FAQ.html
http://cran.r-project.org/bin/windows/base/rw-FAQ.html
My actually R scripts will each create their own output files when run. So, I am mostly concerned with running the R scripts right now. Although the next step would be to open each R script, change a
from 10
to 100
and run them again. Perhaps that should be a follow-up post.
实际上,我的R脚本在运行时将各自创建自己的输出文件。所以,我现在最关心的是运行R脚本。尽管下一步是打开每个R脚本,但是将a从10改为100,然后再次运行它们。也许这应该是一个后续的帖子。
Thank you for any suggestions.
谢谢你的建议。
EDIT Nov 20, 2013:
编辑2013年11月20日:
After discussion with Ricardo Saporta below I changed the four input files to:
在与下面的Ricardo Saporta讨论后,我将四个输入文件改为:
File run.one.r
:
文件run.one.r:
a <- 10
b <- 20
c <- a+b
print(c)
File run.two.r
:
文件run.two.r:
a <- 10
b <- 20
c <- a-b
print(c)
File run.three.r
:
文件run.three.r:
a <- 10
b <- 20
c <- a*b
print(c)
File run.four.r
:
文件run.four.r:
a <- 10
b <- 20
c <- a/b
print(c)
3 个解决方案
#1
7
I have the following function in my utils file:
我的utils文件中有以下功能:
## finds all .R files within a folder and soruces them
sourceEntireFolder <- function(folderName, verbose=FALSE, showWarnings=TRUE) {
files <- list.files(folderName, full.names=TRUE)
# Grab only R files
files <- files[ grepl("\\.[rR]$", files) ]
if (!length(files) && showWarnings)
warning("No R files in ", folderName)
for (f in files) {
if (verbose)
cat("sourcing: ", f, "\n")
## TODO: add caught whether error or not and return that
try(source(f, local=FALSE, echo=FALSE), silent=!verbose)
}
return(invisible(NULL))
}
#2
2
sapply( list.files(run_all_these, full.names=TRUE), source )
You would need to make sure that run_all_these
was a valid Windows directory specification.
您需要确保run_all_these是有效的Windows目录规范。
#3
0
If you don't have subfolders:
如果你没有子文件夹:
library(tidyverse)
list.files("name_folder", full.names = TRUE) %>% walk(source)
#1
7
I have the following function in my utils file:
我的utils文件中有以下功能:
## finds all .R files within a folder and soruces them
sourceEntireFolder <- function(folderName, verbose=FALSE, showWarnings=TRUE) {
files <- list.files(folderName, full.names=TRUE)
# Grab only R files
files <- files[ grepl("\\.[rR]$", files) ]
if (!length(files) && showWarnings)
warning("No R files in ", folderName)
for (f in files) {
if (verbose)
cat("sourcing: ", f, "\n")
## TODO: add caught whether error or not and return that
try(source(f, local=FALSE, echo=FALSE), silent=!verbose)
}
return(invisible(NULL))
}
#2
2
sapply( list.files(run_all_these, full.names=TRUE), source )
You would need to make sure that run_all_these
was a valid Windows directory specification.
您需要确保run_all_these是有效的Windows目录规范。
#3
0
If you don't have subfolders:
如果你没有子文件夹:
library(tidyverse)
list.files("name_folder", full.names = TRUE) %>% walk(source)