Consider the following function:cube <- function(n) {
sq <- function() n * n
n * sq()
}
Under S, sq() does not “know” about the variable n unless it is defined globally:S> cube(2)
Error in sq(): Object "n" not found
Dumped
S> n <- 3
S> cube(2)
[1] 18
In R, the “environment” created when cube() was invoked is also looked in:R> cube(2)
[1] 8
R es capaz de establecer 2 en sq como valor de n, mientras que S no lo puede hacer a menos de que se le indique cuál es el valor de la variable, en este caso se le indicó 3: S> n <- 3