[R-sig-Geo] Recursive user-defined methods in R scripts - upper bound on nested calls?

Baz B.Rowlingson at lancaster.ac.uk
Mon May 9 23:04:55 CEST 2005


Rick Reeves wrote:

> 
> The question: Has anyone explored the limits of 
> recursive R function calls? I coded the recursive 
> method to have a minimal run-time memory footprint. 

  There's a limit on how deep expressions can go:

  > recurse
  function(n){if(n==1){return(0)}else{return(recurse(n-1))}}

  > recurse(99)
  [1] 0

  but this usually gives an error when it goes too far:

  > recurse(100)
  Error in recurse(n - 1) : evaluation nested too deeply: infinite 
recursion / options(expression=)?

> Is there a way to ask for more resources (stack space)
> in the R run-time environment? 

  > options(expressions=1000)
  > recurse(100)
  [1] 0

Might not explain why your code dies silently, unless the error is being 
trapped inside a 'try'...

Baz




More information about the R-sig-Geo mailing list