Skip to content
Prev 30982 / 398506 Next

lexical scope

Dear Robin,
A couple of people have pointed out that the source of the difficulty here 
is lexical scoping. One can sometimes take advantage of lexical scoping by 
defining a local function. For your illustration, for example,

 > x <- 19
 > g <- function(a){
+     f <- function(t){t + x}
+     x <- 1e99
+     f(a)
+     }
 > g(4)
[1] 1e+99
 >

Whether or not this solves your problem depends upon whether it's 
reasonable to make f local to g.

John


-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox