How can I ensure that when a function is called it is not allowed to grab variables from the global environment?
I would like the following code to give me an error. The reason is because I might have mistyped z (I wanted to type y).
z <- 10
temp <- function(x,y) {
y <- y + 2
return(x+z)
}
> temp(2,1)
[1] 12
I’m guessing the answer has to do with environments, but I haven’t understood those yet.
Is there a way to make my desired behavior default (e.g. by setting an option)?
The function doesn’t change, so no need to check it each time it’s used.
findGlobalsis more general, and a little more cryptic. Something likecould visit all functions in an environment, but if there are several functions then maybe it’s time to think about writing a package (it’s not that hard).