Can I write one R script to open and run every R file in a folder?
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.
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.
Here are some example R scripts all stored in a folder named run_all_these
:
The file run.one.r
contains:
a <- 10
b <- 20
c <- a+b
c
The file run.two.r
contains:
a <- 10
b <- 20
c <- a-b
c
The file run.three.r
contains:
a <- 10
b <- 20
c <- a*b
c
The file run.four.r
contains:
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
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.
Thank you for any suggestions.
EDIT Nov 20, 2013:
After discussion with Ricardo Saporta below I changed the four input files to:
File run.one.r
:
a <- 10
b <- 20
c <- a+b
print(c)
File run.two.r
:
a <- 10
b <- 20
c <- a-b
print(c)
File run.three.r
:
a <- 10
b <- 20
c <- a*b
print(c)
File run.four.r
:
a <- 10
b <- 20
c <- a/b
print(c)
To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.
We can run all scripts in a directory or path using "run-parts" command. The run-parts command is used to run scripts or programs in a directory or path. One disadvantage with run-parts command is it won't execute all scripts. It will work only if your scripts have the correct names.
The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).
I have the following function in my utils file:
## 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))
}
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.
If you don't have subfolders:
library(tidyverse)
list.files("name_folder", full.names = TRUE) %>% walk(source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With