Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope in generic functions R

Tags:

scope

r

If you define a variable inside a generic function, it is available to the method. For example:

g <- function(x) {
  y <- 2
  UseMethod("g")
}
g.default <- function() y
g()
[1] 2

But if the variable you define has the same name as the function parameter, this does not happen. It seems that R deletes that variable before calling the method:

g <- function(x) {
  x <- 2
  UseMethod("g")
}
g.default <- function() x
g()
Error in g.default() : object 'x' not found

Could someone explain exactly what is going on here?

like image 457
Carlos Cinelli Avatar asked Jan 09 '15 17:01

Carlos Cinelli


People also ask

What is generic functions in R?

Generic functions (objects from or extending class genericFunction ) are extended function objects, containing information used in creating and dispatching methods for this function. They also identify the package associated with the function and its methods.

Can I define a function inside a function in R?

A nested function or the enclosing function is a function that is defined within another function. In simpler words, a nested function is a function in another function. There are two ways to create a nested function in the R programming language: Calling a function within another function we created.

How do you pass a function as a parameter in R?

Adding Arguments in R We can pass an argument to a function while calling the function by simply giving the value as an argument inside the parenthesis.

How do you call a function from a package in R?

Every time you start a new R session you'll need to load the packages/libraries that contain functions you want to use, using either library() or require() . If you load a package with the same function name as in another package, you can use packageName::functionName() to call the function directly.


1 Answers

The following comments from the C source file that defines do_usemethod at least hint at what's going on. See especially the second sentence of the second enumerated item.

Basically, it looks like (due to dumb application of rule in that second point), the value of x does not get copied over because the C code checks to see if it's among the formals, sees that it is, and so excludes if from the list of variables inserted into the method's evaluation environment.

/* usemethod - calling functions need to evaluate the object
* (== 2nd argument). They also need to ensure that the
* argument list is set up in the correct manner.
*
* 1. find the context for the calling function (i.e. the generic)
* this gives us the unevaluated arguments for the original call
*
* 2. create an environment for evaluating the method and insert
* a handful of variables (.Generic, .Class and .Method) into
* that environment. Also copy any variables in the env of the
* generic that are not formal (or actual) arguments.
*
* 3. fix up the argument list; it should be the arguments to the
* generic matched to the formals of the method to be invoked */
like image 133
Josh O'Brien Avatar answered Oct 14 '22 01:10

Josh O'Brien