0

These are the folders in my directory

128    128-1-32  16384    16384-1-36  4096-1     512    512-1-65  65536-1
128-1  128tbw1   16384-1  4096        4096-1-36  512-1  65536

Each of them has a7.R code that loads files from each folder and creates images.I want my script to enter each of the folders then

source('a7.R') 

then exit that folder and repeat the process for all the folders.I am doing this now manually and it is really boring.Is this possible with R?

I have tried solution like this

#!/usr/bin/Rscript

 lapply(list.files(full.names=TRUE, recursive = TRUE, pattern = "^a7\\.R$"), source)

milenko@milenko-desktop:~/jbirp/mt07$ Rscript s.R
list()

The coffeinejunky's solution is not working

#!/usr/bin/Rscript

foo <- function(directory) { setwd(directory); source(a7.R) }
do.call("foo", list(directory= 128 128-1-32  16384 16384-1-36  4096-1 512 512-1-65 65536-1  128-1 128tbw1 16384-1  4096 4096-1-36  512-1  65536))

    source('n.R')
Error in source("n.R") : n.R:2:33: unexpected numeric constant
1: foo <- function(directory) { setwd(directory); source(a7.R) }
2: do.call("foo", c(directory= 128 128

If i change list like this

   do.call("foo", list(directory= "./128" "./128-1" "./128-1-32" "./128tbw1" "./16384" "./16384-1" "./16384-1-36" "./4096" "./4096-1" "./4096-1-36"  "./512" "./512-1" "./512-1-65" "./65536" "./65536-1"))

I got

Error in source("n.R") : n.R:2:40: unexpected string constant
1: foo <- function(directory) { setwd(directory); source(a7.R) }
2: do.call("foo", list(directory= "./128" "./128-1"

                        ^

This is what I got when I list path

> list.dirs(path = ".", full.names = TRUE)
 [1] "."            "./128"        "./128-1"      "./128-1-32"   "./128tbw1"   
 [6] "./16384"      "./16384-1"    "./16384-1-36" "./4096"       "./4096-1"    
[11] "./4096-1-36"  "./512"        "./512-1"      "./512-1-65"   "./65536"     
[16] "./65536-1"

I need to change directory multiple times and perform the same operation in each of them.Is lapply good for this or not?

MotaF
  • 605
  • 2
  • 9
  • 22
  • 1
    Something like `for(myDir in dirVec) source(paste(myDir, "a7.R", sep="/"))` should do the trick. – lmo Nov 01 '16 at 13:00
  • 3
    Try `lapply(list.files(recursive = TRUE, pattern = "^a7\\.R$"), source)` – talat Nov 01 '16 at 13:00
  • Every folder has a7.R file, is this script doing something specific only to that folder, or is it the same script but with different data, i.e.: input file formats are the same in all those folders? – zx8754 Nov 01 '16 at 13:02
  • @zx8754 Input formats are the same in all those folders! – MotaF Nov 01 '16 at 13:04
  • Then this is a possible duplicate of - [How can I read command line parameters from an R script?](http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script) , meaning you need to pass folder name to one main `a7.R` script, e.g.: `Rscript a7.R 128` – zx8754 Nov 01 '16 at 13:05
  • @zx8754 I forgot to tell that I am on Ubuntu! – MotaF Nov 01 '16 at 13:13
  • @zx8754 I do not know how to solve my problem.I need to cd 128,than source('a7.R'). – MotaF Nov 01 '16 at 13:20
  • @plannapus The same,I will edit my post. – MotaF Nov 01 '16 at 14:16
  • @coffeinjunky Please take a look at my edit,not working! – MotaF Nov 01 '16 at 19:25
  • Why are you running Rscript? – Hong Ooi Nov 01 '16 at 19:31
  • @coffeinjunky It is the same unfortunately! – MotaF Nov 01 '16 at 20:03
  • @coffeinjunky No,it can not recognize string content!! – MotaF Nov 01 '16 at 20:12
  • @coffeinjunky With coma I got,Error in foo("128", "128-1", "128-1-32", "128tbw1", "16384", "16384-1", : unused arguments ("128-1", "128-1-32", "128tbw1", "16384", "16384-1", "16384-1-36", "4096", "4096-1", "4096-1-36", "512", "512-1", "512-1-65", "65536", "65536-1") – MotaF Nov 01 '16 at 20:21

2 Answers2

4

The following should work:

directories <- list.dirs(path=".", full.names = T)
# you need to make sure this contains the relevant directories
# otherwise you need to remove irrelevant directories

foo <- function(x) {
  old <- setwd(x) # this stores the old directory and changes into the new one
  source("a7.R")
  setwd(old) 
}

lapply(directories, foo)

Alternatively,

for(folder in directories) foo(folder)
coffeinjunky
  • 11,254
  • 39
  • 57
0

This will source every a7.R file with the working directory temporarily set to the sourced file's folder.

a7files <- list.files(full.names=TRUE, recursive = TRUE, pattern = "^a7\\.R$")
sapply(a7files, source, chdir = TRUE)

From ?source

chdir logical; if TRUE and file is a pathname, the R working directory is temporarily changed to the directory containing file for evaluating.

Therkel
  • 1,379
  • 1
  • 16
  • 31