[R] R recursion depth and stack size

Martin Maechler maechler at stat.math.ethz.ch
Tue Nov 25 17:16:24 CET 2003


>>>>> "Pascal" == Pascal A Niklaus <Pascal.Niklaus at unibas.ch>
>>>>>     on Tue, 25 Nov 2003 16:10:56 +0100 writes:

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

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

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

a slight one, at least: The recursion depth is limited by
options(expressions = ...), i.e.  getOption("expressions")  which
is 500 by default.

We've had similar problem when drawing a somewhat large dendrogram
(of less than 10000 end nodes still).

I think we should consider increasing the *default* maximal
recursion depth (from 500 to a few thousands) and
even think about increasing the maximally allowed value for
'expressions' (which is 100000).

Martin




More information about the R-help mailing list