[R] R recursion depth and stack size

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 25 17:09:39 CET 2003


Perhaps you could take the trouble to read the error message, which is

   Error in inherits(x, "factor") : evaluation is nested too deeply: 
   infinite recursion?

The evaluation depth is controlled by options(expressions=).  Increasing
it allows your code to run, albeit very slowly.

On Tue, 25 Nov 2003, Pascal A. Niklaus wrote:

> Hi all,
> 
> I am playing around with latin squares, and wrote a recursive function 
> that searches for valid combinations.
> Apart from the fact that there are very many, I run into troubles 
> beginning with size 10x10 because the recursion depth becomes too large 
> (max of 10x9-1=89 in this case).
> 
> Why is this a problem? Isn't there enough space allocated to the stack? 

It is space for expressions.  This is a safety check to avoid infinite
recursion overrunning the C-level stack and crashing R (and thereby losing
all the current work).  There is no portable way to check the C stack size
and usage that we know of.

> Can this be increased? The memory demand shouldn't be terrible, with 
> only minimal local variables (only set and the function params r,c,t - s 
> is local to a block called only once when a solution is found). Even if 
> variables aren't stored efficiently, a recursion depth of 100 shouldn't 
> consume more than a couple of kilobytes.

Why `shouldn't' it?  Have you any idea of the storage requirements of R 
objects?

> Is this a fundamental misunderstanding of the way R works?

It is a misreading of a simple message, for sure.

[...]

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list