Question

Say I have 5 different data set of the same format (same variable names but different value). I initially analyzed them separately, so there will be 'data1.R' which contains the analysis

read(data1)
*analysis*
out<-f(data1)

And similarly 'data2.R',

read(data2)
*analysis*
out<-f(data2)

so on and so forth...

Now, I am writing up the result and would like to use one single R script to draw the result from the individual scripts, such as,

# this is the result from dataset 1
*some command to run 'data1.R' and extract object 'out'
# Then some discussion then
*some command to run 'data2.R' and extract object 'out'

I could of course just source the different scripts, but I don't want to do that because the scripts create/manipulate variables with the same name, and I just feel a lot more comfortable if they don't cross-contaminate each other. So what should I do? Mess around with environments? (I am not skill enough to do that)

Was it helpful?

Solution

No reason to be afraid of environments. Here's a little example.

script1.R:

x <- rnorm(10)

out <- mean(x)

script2.R:

x <- rpois(20, 100)

out <- mean(x)

main script:

e1 <- new.env()
source("script1.R", local = e1)

e2 <- new.env()
source("script2.R", local = e2)


as.list(e1)
## $x
##  [1] -1.0941748872 -1.1482668349 -0.0008547298  0.8843361504  1.5612913538
##  [6]  0.6729380904  0.6081967121  0.0943251617  1.0372299042  2.3717537411

## $out
## [1] 0.4986775

as.list(e2)
## $x
##  [1] 107 114  92 105 100 100 130  86 104  98 103  92 116  97  99 100 105  89  97
## [20]  95

## $out
## [1] 101.45

environments are pretty simple and convenient to use in this case and can just be turned into lists so you can continue to work with the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top