[R] Lazy evaluation in function call

Bert Gunter gunter.berton at gene.com
Wed May 5 00:35:33 CEST 2010


Inline below.

-- Bert 


Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Joris Meys
Sent: Tuesday, May 04, 2010 2:52 PM
To: Thorn
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Lazy evaluation in function call

I think you'll have to code it a bit different. I'd do :
f <- function(x,y){
if(missing(y)) y <-x
x+y
}
> f(2)
[1] 4
> f(2,3)
[1] 5
>

On Tue, May 4, 2010 at 4:26 PM, Thorn <thorn.thaler at rdls.nestle.com> wrote:

> Hi everybody,
>
> how is it possible to refer to an argument passed to a function in the
> function call? What I like to do, is something like
>
> f <- function(x,y) x+y
> f(2, x) # should give 4

-- No.

f <- function(x, y = x)x+y  ## lazy evaluation enables this

> f(2)
[1] 4
> f(2,3)
[1] 5

-- Bert



More information about the R-help mailing list