[R] detecting entry into a recursive function
Duncan Murdoch
murdoch.duncan at gmail.com
Sun Feb 17 20:03:26 CET 2013
On 13-02-17 10:03 AM, Benjamin Tyner wrote:
> Thanks Jim -- I had considered this approach; is there any way to "hide"
> such arguments from users?
Don't export the recursive function, just a non-recursive function that
calls it.
Duncan Murdoch
>
> Jim Lemon wrote:
>> On 02/17/2013 12:55 PM, Benjamin Tyner wrote:
>>> Given a function that calls itself, what's the best way to detect the
>>> entry point? The best I came up with is:
>>>
>>> IsEntryPoint <- function(){
>>>
>>> par <- sys.call(-1L)[[1]]
>>> grandpar <- sys.call(-2L)[[1]]
>>>
>>> !identical(par, grandpar)
>>> }
>>>
>>> but this won't work for functions that don't directly call themselves;
>>> for example, in this one the paste gets inserted in the call stack, so i
>>> is always TRUE.
>>>
>>> f <- function(d){
>>>
>>> i <- IsEntryPoint()
>>>
>>> if(d > 1L) paste(d, f(d-1L))
>>> }
>>>
>>> Any ideas?
>>>
>> Hi Ben,
>> There are a number of ways to do this, from simply having a flag that
>> has one value at the initial call to the function and another value
>> when the function calls itself. for an example of this, see the code
>> for the "sizetree" function in the plotrix package, in particular the
>> "firstcall" argument.
>>
>> Jim
>>
>
>
>
> ______________________________________________
> 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