[R] srt --- slope text with function?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Feb 6 08:49:03 CET 2006


On Sun, 5 Feb 2006, Duncan Murdoch wrote:

> On 2/5/2006 4:28 PM, ivo welch wrote:
>> Dear R Wizards:  To bore everyone to death, below is an improved and
>> hopefully final version of my native.slope() function.  (Thanks, Jim.)
>>  In case you are not asleep yet reading yet another post of mine, may
>> I ask the experts some programming questions?
>>
>>   [I just saw yesterday's threat---I think it would be very, very, very nice if
>>   R would remember from what files with what linenumbers code came from.
>>   thus ignore below questions that mention this feature.  ALAS, I understand
>>   that this cannot be done generally.  But could not function definitions at
>>   least have a component that remembers this?]
>>
>> * Is there a way that I can print an R functional backtrace?  Instead
>> of passing my subroutine name to my function "assert()" below, it
>> would be nice if I could just print the whole call tree [preferably
>> with file names+line numbers] when I want my program to gracefully
>> bonk out.  (PS: I think that this is also what R should print when any
>> user program bonks out, not just the stop message.)
>> Actually, I just learned about traceback(), which functions nicely 
>> AFTER the program stops, not before.  But it gives no traceback 
>> earlier---and it does not give filename+lineno.

'Actually', R does what you ask it to: see options(error).  But before the 
program stops, there is no error to report.  In interactive use I see no 
problem in typing traceback() or using recover() (see below), but for 
batch use we are looking at alternatives.  For example, in R 2.3.0 
(modern) Unix users will get a traceback after a segfault.

> There's going to be a new section on debugging in the R 2.3.0 "Writing R
> Extensions" manual (written by Brian Ripley).  You can see it now if you
> build R-devel (or download a binary build from CRAN; I put Windows
> builds there approximately daily).  I also put together a
> Windows-oriented debugging page at
> http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/.

filename+lineno makes no sense: R functions are not (in the main) from 
files.  They can be autogenerated (and often are).  And I can 'fix' them.

As for the call stack, try options(error=recover) and 'where': see the 
chapter Duncan pointed you to.

>> * is there a way to print all my user defined functions?  I have an
>> init file, in which I am defining all sorts of useful utility
>> functions, and I would like to print what I have defined (for memory)
>> upon a read of this init file?  that is, something that has
>> functionality like
>>    note.all.local.definitions.now.in.vector( all.local.functions )
>>    a <- function() { }
>>    b <- function() { }
>>    cat( all.local.functions ); # should print 'a' and 'b'.
>
> ls.str() includes an option to select only functions.  You could also
> write your own filter for ls() output.
>>
>> * is there a string variable that gives me the name of the current
>> function I am in?
>
> I don't think so, but sys.call() gets close.  Watch out though:  the
> name you see will be the name used by the caller, which may not be the
> name used when the function was written.  For example, in a function
> called from apply(), you'll see FUN, not the original name.  Objects
> don't know their own names in general, because they keep getting passed
> around.

In the debugging context the command 'where' tells you the sequence of 
calls (which can be more helpful).

More generally, functions need not even have names (Bill Venables calls 
them 'anonymous functions'), as in

     r <- sapply(nms,
                 function(n) if (exists(n, envir = envir, mode = mode))n
                             else as.character(NA))

from ls.str.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list