lambda-like functions in R?

Just for completeness. You can use "lambda-like" (anonymous) functions in R but if you want to put them to immediate use, you need to enclose the function definition in parentheses or curly braces:

(function (x) x+1) (1)
{function (x,y) x^y} (2,3)

In the case of curve the first argument is either expression or a function name - but if it is a function name then it is first converted to an expression. (See first few lines in the source code of curve). So if its' not a function name, you'll need an expression – which may contain a "lambda" function:

curve((function (x) x^2)(x))

If you want to use a function (as opposed to its name) as the argument, you can use plot.function:

plot(function(x) x^2)

You have to look at the source of curve to appreciate what is happening (just type curve at the prompt and press enter).

There you can find how the expression passed is parsed.

The only way a function is discovered as being just that, is when only its name is passed along (see the is.namepart). If that is not the case, the expression is called for every x. In your case: for every x, the result is a function, which is not a happy thought for plotting...

So in short: no you cannot do what you tried, but as @ROLO indicated, you can immediately pass the function body, which will be parsed as an expression (and should contain x). If this holds multiple statements, just enclose them in curly braces.


From R 4.1 on, you can use \(x) lambda-like shorthand:

R now provides a shorthand notation for creating anonymous functions,
e.g. \(x) x + 1 is parsed as function(x) x + 1.

With function(x) x^2:

(\(x) x^2)(2)
#[1] 4

This can be used with curve :

curve((\(x) x^2)(x))

enter image description here

But as stated in comments, in this case an expression is more straightforward :

curve(x^2)