[R] Help with functions as arguments

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 12 12:05:34 CET 2013


On 13-02-12 5:34 AM, Jan T Kim wrote:
> On Tue, Feb 12, 2013 at 09:38:19AM +0100, Rainer M Krug wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> On 12/02/13 08:30, Ian Renner wrote:
>>> Hi,
>>>
>>> I am trying to write a function which defines some arguments, then uses those arguments as
>>> arguments of other function calls. It's a bit tricky to explain, so a simple example will have
>>> to suffice. I imagine this has a simple solution, but perusing through environments and other
>>> help lists has not helped. Suppose I have two functions:
>>>
>>>
>>> f1 = function(a) { b = a + 1 b } f2 = function(x, z) { y = x*z -2 y }
>>>
>>> Where I am running into trouble is when I want to call function f1 within function f2:
>>>
>>> f2(x = 3, z = f1(x))
>>>
>>> This returns the error:
>>>
>>> "Error in f1(x) : object 'x' not found"
>>
>> Obviously easiest:
>>
>> X <- 3
>> f2(X=x, Z=f1(X))
>>
>> Your solution does not work, as the f1(x) is evaluated and the value is passed on to f1, and your
>> x is an argument and *only in the function f2* available (= in this context *no* assignment).
>>
>> I remember something similar, and the solution had to do with eval() and quote() and friends -
>> i.e. you have to only evaluate f(x) *inside* the function f2 - but unfortunately I do not remember
>> details.
>
> If you really want to force it, you could use
>
>      f2(x = X <- 3, z = f1(X))

It's very difficult to know what result this will give, because without 
looking into f2, you don't know whether x or z will be computed first.

Duncan Murdoch

>
> I'd always recommend Rainer's two-line version (first assign X, then
> invoke f2), for reasons of legibility / code maintainability and others.
>
> If you want to evaluate f1 inside f2, then you can rewrite f2 to take
> f1 as an argument:
>
>      f2 <- function(x, f) { z <- f(x); return(x * z - 2); }
>
> and invoke that as follows:
>
>      f2(x = 3, f = f1)
>
> If the value of z always depends in some way on x, passing in the
> function to compute z this way is much better than to write f2 as
> though x and z are independent variables.
>
> Best regards, Jan
>
>> Cheers,
>>
>> Rainer
>>
>>
>>
>>>
>>> I'm not sure how to define environments within the functions so that the just-defined 'x' may
>>> be passed as an argument to f1 within f2.
>>>
>>> Any help would be greatly appreciated!
>>>
>>> Ian Renner [[alternative HTML version deleted]]
>>>
>>>
>>>
>>
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.11 (GNU/Linux)
>> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>>
>> iQEcBAEBAgAGBQJRGf97AAoJENvXNx4PUvmCn0EIALjKZK4XsBkELCKJtWU7AXPd
>> 7Qs5Sk2gQF64Jc1uHYm17hwgonZIFIj9IHE05lRP4dl2F6onMACfIEpzz+GF9cO5
>> qS2Kq3Oe/+bglBZIJ1oBGSxs2YEh5gGIykP+PZcWr4xP8QPQ23wnnTbafxTa7PaU
>> BkkNrmNBbre5f+wYBbFaxSlSdlslDFg9c6b5OgLzwBLB0o9tr7KA6POCa8HrX7H4
>> UBsPfwSEkOfyIEwq5drKjXF853nUNRVtd0cPA+mpo+5y/qIkGTiehMRlEGwcBBg7
>> 6uNA8wpTuJI49tdY7rkVEIEGH34atwvBA1kwFYh1UfBzzIg+oRikHm1ZJ4UeJCM=
>> =h9ju
>> -----END PGP SIGNATURE-----
>>
>> ______________________________________________
>> R-help at 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.
>



More information about the R-help mailing list