1

I am trying to apply the same script to a few folders in the same directory. Separately the script works for each folder, but not when I use the following code:

 parent.folder <- "C:/R_Files/Data/DICOM"
    sub.folders <- list.dirs(parent.folder, recursive=TRUE)[-1]
    r.scripts <- file.path(parent.folder, "general_extract.r")
    # Run scripts in sub-folders 
    for(i in sub.foldes) {
      source(i)
    }

it shows me an error: Error in file(filename, "r", encoding = encoding) : cannot open the connection

The script is here:

DCM <- readDICOM()
mAs <- extractHeader(DCM$hdr, "Exposure", FALSE)
mat = matrix(data=mAs, ncol=1)
write.table(mat, file="curve.txt", row.names=FALSE, col.names=FALSE,quote=FALSE)

Is it possible to rewrite the script which will apply "general_extract.r" to each folder in main directory and create the txt file with the specific name, related to the subfolder's name?

Taegost
  • 1,208
  • 1
  • 17
  • 26
Sanata5
  • 11
  • 4
  • I'm not familiar with R, so my question is: does the method "source(i)" actually call the script? I would think that there would be something obvious that would run it, like "r.Runscript()" or something like that – Taegost May 20 '15 at 14:45
  • Thanks for your comment. Today is my second day of using R ( and any programming language AT all) so I really don't know, if it is enough=) – Sanata5 May 20 '15 at 14:53
  • Shouldn't the code be `for(i in r.scripts)`, not `for(i in sub.folders)`? you want to source the scripts, not the folder names. you will also likely want to set `chdir = FALSE` in `source()`. Read the `?source` help page for details. – MrFlick May 20 '15 at 14:55
  • @MrFlick - I believe his parameters in the for loop are correct (we want to loop through each of the subfolders), but I'm pretty sure the problem is that the script itself isn't being called – Taegost May 20 '15 at 14:56
  • @Taegost `source()` would run the script if it existed. The error message indicates that the file you are attempting to source is not found. Given that you're not familiar with R, it might be better to have some faith in those that are quite familiar with R. At the very least `sub.foldes` is spelled incorrectly/differently than the defined variable `sub.folders` and it should probably be `r.scripts <- file.path(sub.folders, "general_extract.r")` to get a list of all the actual script in the subfolders not just the parent directory. – MrFlick May 20 '15 at 14:59
  • @MrFlick, in this case I got an error> Error in list.files(path) : argument "path" is missing, with no default – Sanata5 May 20 '15 at 15:01
  • @ not his, but her) I am a girl and never programmed before) So the question might be stupid... sorry for that – Sanata5 May 20 '15 at 15:05
  • @MrFlick - Sorry, let me clarify my point - As his code shows, the script is inside the parent folder, not the sub-folders. Is there a correlation between the lines that begin "r.scripts" and "source"? (If there is, disregard the rest of my comment). The parameter being passed to source() is only the sub-folder path, it doesn't include the path to the script. WIth that being said, is source() being passed the proper parameter? – Taegost May 20 '15 at 15:06
  • @Sanata5 Sorry for the incorrect pronoun. It's not a stupid question. It's just difficult to help with problems that rely on certain file system setups where it's difficult to create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick May 20 '15 at 15:11
  • @Taegost I see what you're saying now, but you can only pass an R script file to `source()`, not a folder name hence my confusion. – MrFlick May 20 '15 at 15:13
  • @MrFlick - No worries, I figured that from your response, just wanted to make sure everyone was on the right path, I'm bowing out now and leaving this up to the experts :) – Taegost May 20 '15 at 15:16

1 Answers1

2

The best way to run the same set of commands for files in different folder is to change the working directory before re-sourcing the file each time. For example

parent.folder <- "C:/R_Files/Data/DICOM"
sub.folders <- list.dirs(parent.folder, recursive=TRUE)[-1]
r.scripts <- file.path(parent.folder, "general_extract.r")
# Run scripts in sub-folders 
for(i in sub.folders) {
  setwd(i)
  source(r.scripts)
}

The setwd() will change R's working directory which includes where it looks to read and write files by default.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I tried is as well, got an error: Error in list.files(path) : argument "path" is missing, with no default > That is strange I do not have function "list.files" in my code... How it can be fixed? thanks – Sanata5 May 21 '15 at 06:53
  • Now I changed the code a bit:library("oro.dicom") # Folder containing sub-folders parent.folder <- "C:/R_Files/Data/DICOM" # Sub-folders sub.folders <- list.dirs(path = "parent.folder", full.names = TRUE, recursive = TRUE)[-1] # R script file paths r.script <- file.path(parent.folder, "general_extract.r") # Run scripts in sub-folders for(i in sub.folders) { setwd(i) source(r.script) } It seems that it is running but can not finish the task.. I can see that in comand line: > # Run scripts in sub-folders > for(i in sub.folders) { + setwd(i) + source(r.script) + } – Sanata5 May 21 '15 at 07:49