Error: evaluation nested too deeply: infinite recursion / options(expressions=)? in R3.3.2

scriptgirl_3000 picture scriptgirl_3000 · Mar 19, 2018 · Viewed 7k times · Source

I'm trying to use a function in order to read in different models. Using the code below without a function works. When I use the function and call it, I will get the error

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

Can anyone tell me why?

x=rnorm(1000)+sin(c(1:1000)/100)#random data+ sinus superimposed

plot <- function(model){
    par(mfrow=c(2,2))# plot window settings
    plot(model)
    lines(filter(model,rep(1/30,30)),col='red')
    plot(filter(model,rep(1/30,30)))
    plot(model-filter(model,rep(1/30,30)))

    # variances of variable, long term variability and short term variability
    var(model)
    var(filter(model, rep(1/30,30)),na.rm=T)
    var(model-filter(model, rep(1/30,30)),na.rm=T)

}

plot(x)

Answer

Thomas Mailund picture Thomas Mailund · Mar 19, 2018

The problem is that the plot function you are defining is also the one that gets called inside its body, so there is an infinite recursion here.

Rename your plot function to something else, like myplot, and you should be fine.