Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Create a separate environment where to source() an R script, such that the latter does not affect the "caller" environment

Scenario: Let's say I have a master pipeline.R script as follows:

WORKINGDIR="my/master/dir" 
setwd(WORKINDIR)

# Step 1
tA = Sys.time()
source("step1.R")
difftime(Sys.time(), tA)

# Add as many steps as desired, ... 

And suppose that, within step1.R happens a:

rm(list=ls())

Question: How can I separate the pipeline.R (caller) environment from the step1.R environment? More specifically, I would like to run step1.R in separate environment such that any code within it, like the rm, does not affect the caller environment.

like image 535
grd Avatar asked Oct 24 '25 23:10

grd


1 Answers

There are a few ways to call a R script and run it. One of them would be source().

Source evaluates the r script and does so in a certain environment if called so. Say we have a Test.R script:

#Test.R
a <- 1    
rm(list = ls())
b <- 2
c <- 3

and global variables:

a <- 'a'
b <- 'b'
c <- 'c'

Now you would like to run this script, but in a certain environment not involving the global environment you are calling the script from. You can do this by doing creating a new environment and then calling source:

step1 <- new.env(parent = baseenv())

#Working directory set correctly.
source("Test.R", local = step1)

These are the results after the run, as you can see, the symbols in the global environment are not deleted.

a
#"a"
b
#"b"
step1$a
#NULL
#rm(list = ls()) actually ran in Test.R
step1$b
#2

Note: You can also run a R script by using system. This will, however, be ran in a different R process and you will not be able to retrieve anything from where you called the script.

system("Rscript Test.R")
like image 176
Shique Avatar answered Oct 27 '25 16:10

Shique