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