On Mon, Oct 17, 2011 at 5:44 PM, Peter Langfelder <
peter.langfel...@gmail.com> wrote:

> On Mon, Oct 17, 2011 at 5:28 PM, Tyler Pirtle <r...@google.com> wrote:
> > 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? ;)
>
> Try do.call. Look at the help page and perhaps try
>
> do.call(env$estimatePi, list(seed=10), envir = env)
>
> My understanding is that when you call a function, it is executed in
> the current environment (i.e., your "session" environment), not in
> env. do.call allows you to specify the environment.
>
> HTH
>
> Peter
>


Thanks Peter, I tried that too:
> do.call(msg$fun, msg$args, envir=msg$env)
Error in runif(numDraws, min = -r, max = r) : object 'r' not found

Yet still:
> msg$env$r
[1] 0.5

I'm gonna start pulling hair out. ;)

        [[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.

Reply via email to