Re: [R] Referencing function name from within function

From: Alberto Monteiro <albmont_at_centroin.com.br>
Date: Tue 03 Apr 2007 - 13:26:14 GMT

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



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:33:48 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.