[R] as.function parameters

Rui Barradas ruipbarradas at sapo.pt
Wed May 9 23:51:45 CEST 2012


Hello,

>
> Is there a way to force a call-by-value instead of a call-by-address? 
>
Yes, there is one way, just do nothing. R always uses call-by-value.
I'm not completely sure about this, but when you create f() the parameter x
default value
is a promise, not an actual, evaluated value. Courtesy lazy evaluation.
It's only when you need it that it's evaluated.

For instance, using your code, you don't need to create tmp before creating
f().
Since R is not evaluating tmp, there's no error to throw.
You'll only get an error  if you try to *use* f() without creating tmp or
without a default value override.

rm(f, tmp) # just to be sure
f <- as.function(alist(y=, x=tmp, y + x))
f(1)	# error
f(1, 3) # no error
tmp <- 2
f(1) # 3

Hope this helps,
Rui Barradas



jackl wrote
> 
> Hi,
> 
> i have a question regarding the as.function functionality.
> I need to save multiple functions with adjusted parameters in a list.
> The adjusted parameters are changed via variables in a loop.
> The problem I'm facing right now is that the functions saved in
> the list don't have the values of the parameters but the variable
> parameter
> itself. Is there a way to force a call-by-value instead of a
> call-by-address?
> 
> Example(not the real function):
> tmp <- 4
> f <- as.function(alist(y=,x=tmp,y+x)
> f(1) > 5
> tmp <- 2
> f(1) > 3
> 
> thanks ahead and sorry if this question has already been asked before.
> I could not really grasp the problem to one Subject.
> 


--
View this message in context: http://r.789695.n4.nabble.com/as-function-parameters-tp4620390p4621903.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list