[R] Getting the list of lines evaluated in a function call

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 7 05:07:28 CET 2012


On 12-02-06 3:11 PM, Francois Rousseu wrote:
>
> Hello useRs
> I am looking for a function that can give the list of lines that were evaluated or used to generatethe last output from any function, whether an error message was generated or not. For example, let's say I have a function like this:
> foo<- function( x ){                           if( x>10 ){                                                ans<- x+100                                   }else{                  ans<- x-100                  }             ans             }
> and I type:
>> foo(4)[1] -96
> I would like a function that returns something like this
> list.lines(foo)[1] 1 3 4 5 6
> or:
>> foo("hello")Error in x + 100 : non-numeric argument to binary operator
> list.lines(foo)[1] 1 2
> Is there already a function in R doing something similar or anybody has any hints for functions that I could use to create such a function?
> Sincerely,Francois Rousseu

There isn't a function like that.  I think it would be hard to write one 
without internal changes to R, but those changes might not have to be 
too large if the function had srcref information included, because the 
evaluator does have access to that.

(One way to do it without changing R is to pre-process the function to 
add in lots of extra code that records where it is as it is being 
evaluated.   This is a little tricky to get right, but it might be 
quicker than waiting for the internal changes.)

Duncan Murdoch



More information about the R-help mailing list