I am trying to evaluate an R function in a specific environment and getting different results than expected.
Example below, I would like to eval my function in testEnv, but when I set envir to that it still uses the global variable. I am not sure why it does that, ChatGPT also suggests this method.
testEnv<-env(x = 1000)
testEnv$x
func1 <- function(){
2 + x
}
x <- 1
func1()
eval(func1(), envir = testEnv)
This returns 3 instead of 1003.