[R] How many nested function calls?

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Feb 1 13:28:24 CET 2011



On 31.01.2011 23:23, muchado22 at gmail.com wrote:
> Is there a limit to how many nested function calls R will tolerate? I mean,
> if I have a function that calls another function that calls another
> function, etc., is there a limit to how deep I can go?
>
> I am getting an error that says a variable is not available. When I do a
> traceback(), it shows that the supposedly missing variable is 7 functions
> back. I'll call that "fcn1".
>
> As far as I can tell, the variable that the error says is not available is
> in fact globally available, except when I try to access it via nested
> function calls. Also, when I call fcn1 at the command prompt, there is no
> problem. But if I put browser() inside fcn1 and call fcn7, the variable is
> not available at the Browse[1]>  prompt. And I get the same problem if I call
> fcn2.
>
> Does it sound like the problem is the number of nested function calls or
> something else?


No, the number is controlled.

See ?options and its argument "expressions" that tells you that 5000 
nested expressions are a default limit that can be changed easily (and 
if it is hit you will get an informative error message).

What you see is how are searches objects in environments (so called 
scoping rules).
The general first rule is to pass all required objects as arguments to 
the functions that need them. You can make (but should not make too 
many) exceptions if you have understood the scoping rules that you can 
read about in the documentation.

Best,
Uwe Ligges



> Thanks,
> Anna
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list