Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call the `function` function?

Tags:

r

I am trying to call the function `function` to define a function in R code.

As we all know™️, `function`is a .Primitive that’s used internally by R to define functions when the user uses the conventional syntax, i.e.

mean1 = function (x, ...) base::mean(x, ...) 

But there’s nothing preventing me from calling that primitive directly. Or so I thought. I can call other primitives directly (and even redefine them; for instance, in a moment of madness I overrode R’s builtin `for`). So this is in principle possible.

Yet I cannot get it to work for `function`. Here’s what I tried:

# Works mean2 = as.function(c(formals(mean), quote(mean(x, ...))))  # Works mean3 = eval(call('function', formals(mean), quote(mean(x, ...))))  # Error: invalid formal argument list for "function" mean4 = `function`(formals(mean), quote(mean(x, ...))) 

The fact that mean3 in particular works indicates to me that mean4 should work. But it doesn’t. Why?

I checked the definition of the `function` primitive in the R source. do_function is defined in eval.c. And I see that it calls CheckFormals, which ensures that each argument is a symbol, and this fails. But why does it check this, and what does that mean?

And most importantly: Is there a way of calling the `function` primitive directly?


Just to clarify: There are trivial workarounds (this question lists two, and there’s at least a third). But I’d like to understand how this (does not) works.

like image 929
Konrad Rudolph Avatar asked Feb 08 '19 16:02

Konrad Rudolph


People also ask

What is calling a function called?

A function call is an expression containing the function name followed by the function call operator, () . If the function has been defined to receive parameters, the values that are to be sent into the function are listed inside the parentheses of the function call operator.

How do you call a function in a function in Python?

How to call a function in python? Once we have defined a function, we can call it from another function, program, or even the Python prompt. To call a function we simply type the function name with appropriate parameters.

How do you call a function from a function in C++?

Use the return Statement to Call a Function Within a Function in C++ Another useful method to invoke a function within a function is to utilize the return statement. Mind though, the called function should have a return value to fit this notation or not compile.


2 Answers

This is because function is a special primitive:

typeof(`function`) #> [1] "special" 

The arguments are not evaluated, so you have actually passed quote(formals(mean)) instead of the value of formals(mean). I don't think there's a way of calling function directly without evaluation tricks, except with an empty formals list which is just NULL.

like image 90
Lionel Henry Avatar answered Sep 28 '22 01:09

Lionel Henry


For completeness’ sake, Lionel’s answer hints at a way of calling `function` after all. Unfortunately it’s rather restricted, since we cannot pass any argument definition except for NULL:

mean5 = `function`(NULL, mean(x, ...)) formals(mean5) = formals(mean) 

(Note the lack of quoting around the body!)

This is of course utterly unpractical (and formals<- internally calls as.function anyway.)

like image 39
Konrad Rudolph Avatar answered Sep 28 '22 02:09

Konrad Rudolph