[R] Referencing function name from within function
Mark Wardle
mark at wardle.org
Tue Apr 3 15:43:01 CEST 2007
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
More information about the R-help
mailing list