Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run every file in a folder

Tags:

r

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)
like image 317
Mark Miller Avatar asked Nov 19 '13 22:11

Mark Miller


People also ask

How do I loop all files in a directory?

To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.

How do I run all files in a directory in Linux?

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.

How do I loop all files in a directory in bash?

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).


3 Answers

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))
}
like image 151
Ricardo Saporta Avatar answered Oct 03 '22 23:10

Ricardo Saporta


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.

like image 28
IRTFM Avatar answered Oct 03 '22 22:10

IRTFM


If you don't have subfolders:

library(tidyverse)

list.files("name_folder", full.names = TRUE) %>% walk(source)
like image 25
danilinares Avatar answered Oct 04 '22 00:10

danilinares