[R] Referencing function name from within function

Alberto Monteiro albmont at centroin.com.br
Tue Apr 3 15:26:14 CEST 2007


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 :-/

Alberto Monteiro



More information about the R-help mailing list