Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

recursion - Explanation of R: options(expressions=) to non-computer scientists

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...