Re: [R] Fwd: Evaluating a function within a pre-defined environment?

2009-12-10 Thread hadley wickham
On Wed, Dec 9, 2009 at 4:48 PM, David Reiss dre...@systemsbiology.org wrote:
 Ideally I would like to be able to use the function f (in my example)
 as-is, without having to designate the environment as an argument, or
 to otherwise have to use e$x in the function body.

e - new.env()
e$x - 3
f - function(xx) x - x + xx
environment(f) - e

f(1)
f(1)
f(2)
e$x

But I think you'd be better off doing it within the usual scoping rules:

new_adder - function(init = 0) {
  sum - init
  function(x) {
sum - sum + x
sum
  }
}

f - new_adder(3)
f(1)
f(2)

Hadley

-- 
http://had.co.nz/

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


Re: [R] Fwd: Evaluating a function within a pre-defined environment?

2009-12-09 Thread Gabor Grothendieck
e - new.env()
e$x - 2
f - function(a, e) { e$x - e$x + a; e$x }
f(3, e)
e$x # 5

Another way to accomplish this is to use the proto package which puts
the whole thing into an object oriented framework.  See
http://r-proto.googlecode.com

library(proto)
p - proto(x = 2, f = function(this, a) { this$x - this$x + a; this$x })
p$f(3) # 5


On Wed, Dec 9, 2009 at 4:54 PM, David Reiss dre...@systemsbiology.org wrote:
 Hi all,

 I have a somewhat confusing question that I was wondering if someone
 could help with. I have a pre-defined environment with some variables,
 and I would like to define a function, such that when it is called, it
 actually manipulates the variables in that environment, leaving them
 to be examined later. I see from the R language definition that

 When a function is called, a new environment (called the evaluation
 environment) is created, whose enclosure (see Environment objects) is
 the environment from the function closure. This new environment is
 initially populated with the unevaluated arguments to the function; as
 evaluation proceeds, local variables are created within it.

 So basically, I think I am asking if it is possible to pre-create my
 own evaluation environment and have it retain the state that it was
 in at the end of the function call?

 Example:

 e - new.env()
 e$x - 3
 f - function(xx) x - x + xx

 can I then call f(2) and have it leave e$x at 5 after the function
 returns? I know that

 environment(f) - e

 goes part of the way, but I would like to let the function also write
 to the environment.

 Thanks for any advice.

 --David

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


Re: [R] Fwd: Evaluating a function within a pre-defined environment?

2009-12-09 Thread David Reiss
Ideally I would like to be able to use the function f (in my example)
as-is, without having to designate the environment as an argument, or
to otherwise have to use e$x in the function body.

thanks for any further advice...

On Wed, Dec 9, 2009 at 2:36 PM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 e - new.env()
 e$x - 2
 f - function(a, e) { e$x - e$x + a; e$x }
 f(3, e)
 e$x # 5

 Another way to accomplish this is to use the proto package which puts
 the whole thing into an object oriented framework.  See
 http://r-proto.googlecode.com

 library(proto)
 p - proto(x = 2, f = function(this, a) { this$x - this$x + a; this$x })
 p$f(3) # 5


 On Wed, Dec 9, 2009 at 4:54 PM, David Reiss dre...@systemsbiology.org wrote:
 Hi all,

 I have a somewhat confusing question that I was wondering if someone
 could help with. I have a pre-defined environment with some variables,
 and I would like to define a function, such that when it is called, it
 actually manipulates the variables in that environment, leaving them
 to be examined later. I see from the R language definition that

 When a function is called, a new environment (called the evaluation
 environment) is created, whose enclosure (see Environment objects) is
 the environment from the function closure. This new environment is
 initially populated with the unevaluated arguments to the function; as
 evaluation proceeds, local variables are created within it.

 So basically, I think I am asking if it is possible to pre-create my
 own evaluation environment and have it retain the state that it was
 in at the end of the function call?

 Example:

 e - new.env()
 e$x - 3
 f - function(xx) x - x + xx

 can I then call f(2) and have it leave e$x at 5 after the function
 returns? I know that

 environment(f) - e

 goes part of the way, but I would like to let the function also write
 to the environment.

 Thanks for any advice.

 --David

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


Re: [R] Fwd: Evaluating a function within a pre-defined environment?

2009-12-09 Thread Gabor Grothendieck
You could write a wrapper function that accepts the output of the
function you don't want to change and then sets the values.

# f is function we don't want to change
f - function(a)  x + a

wrapper - function(x, e) {
   environment(f) - e
   e$x - f(x)
}

e - new.env()
e$x - 2
wrapper(3, e)
e$x # 5

or with proto:

library(proto)
p - proto(x = 2, f = f, wrapper = function(this, x) this$x -
with(this, f)(x) )
p$wrapper(3)
p$x # 5




On Wed, Dec 9, 2009 at 5:48 PM, David Reiss dre...@systemsbiology.org wrote:
 Ideally I would like to be able to use the function f (in my example)
 as-is, without having to designate the environment as an argument, or
 to otherwise have to use e$x in the function body.

 thanks for any further advice...

 On Wed, Dec 9, 2009 at 2:36 PM, Gabor Grothendieck
 ggrothendi...@gmail.com wrote:
 e - new.env()
 e$x - 2
 f - function(a, e) { e$x - e$x + a; e$x }
 f(3, e)
 e$x # 5

 Another way to accomplish this is to use the proto package which puts
 the whole thing into an object oriented framework.  See
 http://r-proto.googlecode.com

 library(proto)
 p - proto(x = 2, f = function(this, a) { this$x - this$x + a; this$x })
 p$f(3) # 5


 On Wed, Dec 9, 2009 at 4:54 PM, David Reiss dre...@systemsbiology.org 
 wrote:
 Hi all,

 I have a somewhat confusing question that I was wondering if someone
 could help with. I have a pre-defined environment with some variables,
 and I would like to define a function, such that when it is called, it
 actually manipulates the variables in that environment, leaving them
 to be examined later. I see from the R language definition that

 When a function is called, a new environment (called the evaluation
 environment) is created, whose enclosure (see Environment objects) is
 the environment from the function closure. This new environment is
 initially populated with the unevaluated arguments to the function; as
 evaluation proceeds, local variables are created within it.

 So basically, I think I am asking if it is possible to pre-create my
 own evaluation environment and have it retain the state that it was
 in at the end of the function call?

 Example:

 e - new.env()
 e$x - 3
 f - function(xx) x - x + xx

 can I then call f(2) and have it leave e$x at 5 after the function
 returns? I know that

 environment(f) - e

 goes part of the way, but I would like to let the function also write
 to the environment.

 Thanks for any advice.

 --David

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


Re: [R] Fwd: Evaluating a function within a pre-defined environment?

2009-12-09 Thread Charles C. Berry

On Wed, 9 Dec 2009, David Reiss wrote:


Ideally I would like to be able to use the function f (in my example)
as-is, without having to designate the environment as an argument, or
to otherwise have to use e$x in the function body.

thanks for any further advice...


Perhaps you want something along the lines of the open.account example of

R-intro 10.7 Scope

??

Chuck



On Wed, Dec 9, 2009 at 2:36 PM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:

e - new.env()
e$x - 2
f - function(a, e) { e$x - e$x + a; e$x }
f(3, e)
e$x # 5

Another way to accomplish this is to use the proto package which puts
the whole thing into an object oriented framework.  See
http://r-proto.googlecode.com

library(proto)
p - proto(x = 2, f = function(this, a) { this$x - this$x + a; this$x })
p$f(3) # 5


On Wed, Dec 9, 2009 at 4:54 PM, David Reiss dre...@systemsbiology.org wrote:

Hi all,

I have a somewhat confusing question that I was wondering if someone
could help with. I have a pre-defined environment with some variables,
and I would like to define a function, such that when it is called, it
actually manipulates the variables in that environment, leaving them
to be examined later. I see from the R language definition that

When a function is called, a new environment (called the evaluation
environment) is created, whose enclosure (see Environment objects) is
the environment from the function closure. This new environment is
initially populated with the unevaluated arguments to the function; as
evaluation proceeds, local variables are created within it.

So basically, I think I am asking if it is possible to pre-create my
own evaluation environment and have it retain the state that it was
in at the end of the function call?

Example:

e - new.env()
e$x - 3
f - function(xx) x - x + xx

can I then call f(2) and have it leave e$x at 5 after the function
returns? I know that

environment(f) - e

goes part of the way, but I would like to let the function also write
to the environment.

Thanks for any advice.

--David

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



Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

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