I have a couple of tslm
functions (from the forecast
R package) inside a bigger function. I cannot seem to get them both to find their relevant objects. Including an explicit environment fixed one instance and broke the other. A minimal example follows:
library(forecast)
data(gas)
testlm <- function(x)
{
e<-new.env()
e$x<-x
tslm(e$x~trend)->e$z
return(e$z)
}
testlm(gas)
This throws the following error:
Error in eval(expr, envir, enclos) : object 'e' not found
I also tried to explicitly give the formula the correct environment by setting
e$f<-as.formula("e$x~trend+season",env=e)
but got the exact same error.
P.S. The other error seems harder to reproduce, but getting this to work with the explicit environment should be enough.
Finally figured it out! To avoid messing with tslm
or lm
's environments you can just put the data as a separate parameter. Changing the tslm
line to
tslm(data ~ trend,data=e$x)
works normally.