I have written a recursive function of the form
foo=function(vars,i=2){
**do something with vars**
if(i==length(vars)){
return(**something**)
}else{
foo(vars,i+1)
}
}
length(vars)
is around 1500. When I execute it, I got the error
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
Fair enough, so I increased
options(expressions=10000)
Then it works.
But when I read the help doc of options
regarding expressions=
, i just don't understand what it is saying. Furthermore, it suggests
...If you increase it, you may also want to start R with a larger protection stack;...
So can someone tell me what's is going on, if I should have increased the expressions
parameters as I have, and if I should modify something else together with it.
Cutting some corners here... The expressions -option sets the maximum number of nested expressions that will be evaluated. With deep recursion the default is sometimes exceeded, and increasing the value often solves the problem. But if it doesn't (a new error message is given), you might need to additionally increase the size of the protection stack. Computers store information about the active routines in stacks. Sometimes when the information doesn't quite fit to the stack, the information is written beyond the stacks boundary, which is bad, since it typically creates, e.g., memory access problems. This can potentially be rectified by by setting the option --max-ppsize when starting R. It's like giving a child a larger paper when he or she overdraws the current paper, and colors the table too.
For more background, see Wikipedia, and links thereof. For details of R's command line options, see An Introduction to R, section B.1.