0

I have a "Master R script" that runs multiple R scripts using "source()". It looks something like this:

source("script1.R")
source("script2.R")
source("script3.R")
source("script4.R")
source("script5.R")
source("script6.R")
source("script7.R")
source("script8.R")

Scripts 1 to 8 need to run sequentially. This "Master R script" is scheduled using cron and "Rscript" command.

Rscript --verbose --vanilla "master_r_script.R" 

My "Master R script" throws an error='Cannot allocate memory' after running a few R scripts. I've tried calling rm(list = ls()) and gc(verbose = TRUE, reset = TRUE) after every source() command, but that still doesn't solve the problem.

I need a way that somehow "cleans" the R environment and frees up the memory. I could of course break up the "Master R script" into multiple "RScript calls" but I want to avoid doing that.

How can I "restart" a session created by an "Rscript". The solutions here are not applicable because they're restarting an R REPL session.

ah bon
  • 9,293
  • 12
  • 65
  • 148
Gufran Pathan
  • 301
  • 2
  • 12
  • If you really don't need them run in the same session (just sequentially) then I'm not sure why this need to be an .R find. A bash file seems like the more natural way to do this to me. – Dason Dec 23 '17 at 15:43
  • There are some variables I define in the "Master R Script" that I use in each of the R scripts. I could ofcourse define them inside each of the scripts but I want to avoid doing that. – Gufran Pathan Dec 23 '17 at 15:50
  • Can you use `rm(...)` and then `gc(reset=TRUE)`? I realize `gc()` doesn't fix all problems, but without a working example, I don't know how to test this. – r2evans Dec 23 '17 at 18:17
  • This question is not answerable as it stands, because we cannot reproduce the problem and it is not immediately obvious that what you think is the problem is actually the problem. For example, can you source the 8 scripts in sequence interactively from an R shell? Do you know where exactly the out-of-memory error occurs? – Claus Wilke Dec 23 '17 at 18:55
  • @ClausWilke, the problem happens after "script6.R". "script6.R" loads a lot of data. "script7.R" tries to load some more but runs out of memory. – Gufran Pathan Dec 24 '17 at 07:57

1 Answers1

0

Short answer:

The main reason you do not succeed with cleaning your environment is because you are not (in your R files) saving the result of rm command.

Expanded answer:

If using following commands in an Rscript rm(list = ls()) and gc(verbose = TRUE,reset = TRUE), you need in every script to add load('.RData') and save.image('.RData').

Since Rscript and R terminal does not act dynamically as opposite to Rstudio, each time Rscript is not in a running state, it does not remember the state of the environment, but running Rscript it will of course load .RData which will act as the global environment.

In the [Master script] you should at start clean the environment with above commands, alternative have a separate script that is source and contains the "clean".

ah bon
  • 9,293
  • 12
  • 65
  • 148
Toolbox
  • 2,333
  • 12
  • 26