[R] Help with functions as arguments

Frans Marcelissen fransiepansiekevertje at gmail.com
Tue Feb 12 10:36:40 CET 2013


Hi Ian,
The remark of Rainer is correct, but I think the solution is very simpe:
f1 = function(a)
{
  b = a + 1
  b
}
f2 = function(x, z)
{
  y = x*z(x) -2
  y
}

> f2(x = 3, z = f1)
[1] 10
Or are you intending something else?
Best wishes,
Frans

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Ian Renner
Verzonden: dinsdag 12 februari 2013 8:30
Aan: r-help at r-project.org
Onderwerp: [R] Help with functions as arguments

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"

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]]



More information about the R-help mailing list