An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110712/5484597f/attachment.pl>
Call and Eval function inside R
3 messages · Narayani Barve, Luca Morandini, Michael Sumner
On 07/12/2011 10:29 PM, Narayani Barve wrote:
I am writing a function where I want to supply a variable which contains a
condition to be evaluated inside the function. For example, I have a hourval
variable containing values like 0, 3, 6, 9, 18, 3, 6, 9, 18 0, 3, 18 ... I
want to select the indices where hourval variable matches to 0, 6.. This 0,
6 could change depending upon some other parameters. Basically they are not
fixed always. So I pass a variable g1 = call("which", (hourval==0 | hourval
== 6)). I want this statement to be evaluated in the program. Hence I use
the statement x1 = eval(g1). Obviously, when I pass the variable g1, that
time hourval variable is not generated, but it is generated just before the
eval(g1) statement. I get error, object 'hourval' not found. Is there any
other way to solve this problem.
The call function's arguments are evaluated at definition, hence hourval should be
defined beforehand.
An exmaple:
remove(c(a, b, c))
fmean<-call("mean", c(a, b, c)) # Error, a,b,c are not defined
a<-1
b<-2
c<-3
fmean<-call("mean", c(a, b, c)) # OK, a,b,c are defined
g<-function(f) {
eval(f)
}
g(fmean) # Returns 2
a<--1
b<-0
c<-1
g(fmean) # Returns 2 as well, since a,b,c are evaluated at definition time
Regards,
Luca Morandini
http://www.lucamorandini.it
Please don't cross post: http://stackoverflow.com/questions/6671029/call-and-eval-function-inside-r This is not a question for R-Sig-Geo at any rate, R-help is a better candidate and you should consult that mailing list's posting guide. Cheers, Mike.
On Wed, Jul 13, 2011 at 6:29 AM, Narayani Barve <narayani at ku.edu> wrote:
Hi All,
I am writing a function where I want to supply a variable which contains a
condition to be evaluated inside the function. For example, I have a hourval
variable containing values like 0, 3, 6, 9, 18, 3, 6, 9, 18 0, 3, 18 ... I
want to select the indices where hourval variable matches to 0, 6.. This 0,
6 ?could change depending upon some other parameters. Basically they are not
fixed always. So I pass a variable g1 = ?call("which", (hourval==0 | hourval
== 6)). I want this statement to be evaluated in the program. Hence I use
the statement x1 = eval(g1). Obviously, when I pass the variable g1, that
time hourval variable is not generated, but it is generated just before the
eval(g1) statement. I get error, ?object 'hourval' not found. Is there any
other way to solve this problem.
Thanks in advance, any help is appreciated.
Narayani Barve
? ? ? ?[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Michael Sumner Institute for Marine and Antarctic Studies, University of Tasmania Hobart, Australia e-mail: mdsumner at gmail.com