Ivan Krylov [krylov.r...@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. So X<-1:5 h <- function(p=2) p*X h() # works, but X <- pi h() # 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 rm(X) h() # 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. BUT X <- pi/2 fh <- function(p=2) { X <- 7 h(p) } fh() # 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}} ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.