[R] argument "x" is missing in minpack.lm

Stephen Ellison S@E|||@on @end|ng |rom LGCGroup@com
Tue Jun 30 21:59:03 CEST 2020

Ivan Krylov [krylov.r00t using gmail.com] said:
>  Instead you can either close over X:
> X <- c(...)
> holly <- function(p) (p$a * X^2) / (p:b^2 + X^2)
> # function holly now "contains" the vector X

That would not be an accurate statement as written.
The function only contains an unevaluated call referencing X; not the vector X. 
If X is not defined inside the function or its arguments, scoping rules take over and R goes looking in the function's environment, using the first thing called "X" that it finds.

h <- function(p=2) p*X
# works, but
X <- pi
# Not the same answer. If the function 'contained' the vector, the result would be 2*(1:5) as above.
# This is why it's not wise to rely on scoping rules in functions, unless you _completely_ control the environment.

# and
# returns an error because X is no longer defined, in the function or out of it, even though X was defined at the time h() was defined.


X <- pi/2
fh <- function(p=2) {
        X <- 7
# returns pi and not 14 because h() was bound to the global environment on creation and still is when R finds it on evaluating h() in the fh() function body. 

Moral: if you want to be safe, pass it as an argument.

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list