7

Is there a way in R to pause execution of a script until a file is created? I would like to pre-load R in advance to decrease the execution time when the input file is generated.

josliber
  • 43,891
  • 12
  • 98
  • 133
John Richardson
  • 676
  • 8
  • 24
  • if R is writing a file it would normally proceed after that command finishes, do you mean a file generated by a system call? – Stephen Henderson Dec 11 '13 at 23:10
  • 1
    Yes, an OS command in the background would create the file. I would like R wait to read that file until is created. Thanks – John Richardson Dec 11 '13 at 23:12
  • Have you tested this with the interactive console? If you have `system(doSomething, wait=TRUE)` doesn't it wait till the OS finishes that command then return control? – Stephen Henderson Dec 11 '13 at 23:22
  • I have an R script (non interactive) that needs to load libraries and a large histoircal dataset before creating the model that depends on a new smaller dataset. I would like to load the libraries and the his dataset and wait until the new smaller dataset is created to model it. – John Richardson Dec 11 '13 at 23:26
  • 3
    OK different tangent.. rather than have an Rscript with some bash inside, why not have a bash script with Rscripts inside. Because with bash it will be a lot easier to control waits/sleeps/asynchronous/parallel of the diff processes? – Stephen Henderson Dec 11 '13 at 23:32

1 Answers1

12

If you want to keep this in R, you might try the following code:

while (!file.exists(your.file.name)) {
  Sys.sleep(1)
}

A shorter sleep duration would poll more frequently but might be slightly more CPU intensive.

josliber
  • 43,891
  • 12
  • 98
  • 133