I don't think you said how you packaged this stuff up and that is the critical part of your question. Here is an example that does what I think you want to do. It uses save() and load() for the serialization and I serialize an environent full of data and functions.
In one session of R make some data and functions that use the data in a fresh environment: env <- new.env() with(env, { r <- 0.5 setRadius <- function(radius) r <<- radius area <- function() pi * r^2 }) Serialize the environment env with save("env", file = tf <- tempfile(fileext=".rda")) # print(tf) # [1] "C:\\DOCUME~1\\wdunlap\\LOCALS~1\\Temp\\RtmpJNnRiu\\filef9f6711.rda" In another session deserial that rda file with load and use the environment env as you did in the first session: > load("C:\\DOCUME~1\\wdunlap\\LOCALS~1\\Temp\\RtmpJNnRiu\\filef9f6711.rda") > objects() [1] "env" > env$area() [1] 0.7853982 > env$setRadius(10) > env$area() [1] 314.1593 If you serialize a function whose environment is not on the search list then save serializes the environment of the function along with the function. E.g., in the master session: > circleMaker <- function(r) { + radius <- r + function() pi * r^2 + } > c1 <- circleMaker(10) > c2 <- circleMaker(200) # same function, different environment as c1 > save("c1", "c2", file = tf <- tempfile(fileext=".rda")) > tf [1] "C:\\DOCUME~1\\wdunlap\\LOCALS~1\\Temp\\RtmpJNnRiu\\file2cc3696f.rda" and in the slave session: > load("C:\\DOCUME~1\\wdunlap\\LOCALS~1\\Temp\\RtmpJNnRiu\\file2cc3696f.rda") > c1() [1] 314.1593 > c2() [1] 125663.7 > objects(environment(c2)) [1] "r" "radius" If the function is in .GlobalEnv and then you need to add by hand the names of the data in .GlobalEnv that it requires to run (or save all of .GlobalEnv, probably under an alternate name). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Tyler Pirtle > Sent: Monday, October 17, 2011 5:28 PM > To: r-help@r-project.org > Subject: [R] Remote environments, calling functions > > Hi there, > > I'm trying to do something like a migration of an R program. I've got a > function and some variables > in an interactive-session: > > r <- .5 > ## estimatePi references r > estimatePi <- function(seed) { > set.seed(seed) > numDraws <- 1e+05 > x <- runif(numDraws, min = -r, max = r) > y <- runif(numDraws, min = -r, max = r) > inCircle <- ifelse((x^2 + y^2)^0.5 < r, 1, 0) > sum(inCircle)/length(inCircle) * 4 > } > > At some point later (in C) I package all this up and send it somewhere else, > where deserialize it (with care): > > > ls(env) > [1] "echoBack" "estimatePi" "r" > > env$estimatePi > function (seed) > { > set.seed(seed) > numDraws <- 1e+05 > x <- runif(numDraws, min = -r, max = r) > y <- runif(numDraws, min = -r, max = r) > inCircle <- ifelse((x^2 + y^2)^0.5 < r, 1, 0) > sum(inCircle)/length(inCircle) * 4 > } > > env$r > [1] 0.5 > > Ok? So now i want to call estimatePi(10): > > > env$estimatePi(10) > Error in runif(numDraws, min = -r, max = r) : object 'r' not found > > I've tried several different things and I'm stumped. > My understanding of R function calls and environments is limited - but I'm > not convinced that what I'm trying to do is > completely out of reach. Any thoughts? ;) > > > Thanks, > > > Tyler > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.