Re: [R] Referencing function name from within function

From: Mark Wardle <mark_at_wardle.org>
Date: Tue 03 Apr 2007 - 13:43:01 GMT

Alberto Monteiro wrote:
> Jim Holtman wrote:

>>> myfunction <- function(){
>> +     # some calculations
>> +     # now get my name
>> +     .caller <- sys.call()
>> +     cat(paste(as.character(.caller[[length(.caller)]]),"needs 
>> 'xyz'\n")) + }
>>> myfunction()
>> myfunction needs 'xyz'

>
> I like this! It's even possible to know _which_ function
> called myfunction and caused the 'disaster' (for bug tracking):
>
> myfunction <- function() {
> # some calculations
> # oops, something wrong here
> call.stack <- sys.calls()
> n <- length(call.stack)
> cat(as.character(call.stack[[n]]),
> " crashed, called from ",
> as.character(call.stack[[n-1]]), "\n")
> }
>
> evil.function <- function() myfunction()
>
> evil.function()
>
> How can I get the _arguments_ to the calls? as.character strips
> them :-/
>

?traceback

Or aren't you better off creating a custom error handler if you are going to go this far?

Best wishes,

Mark

-- 
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Tue Apr 03 23:47:19 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 03 Apr 2007 - 14:30:58 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.