[R] Determining name of calling function.

Rolf Turner r.turner at auckland.ac.nz
Mon Sep 28 01:24:10 CEST 2009


I have vague recollections of seeing this question discussed on r-help
previously, but I can't find the relevant postings.

I want to determine (from within a given function) the name of the  
function
calling that given function.

E.g. if I have a function foo() which calls a function bar(), and also
a function clyde() which calls bar(), I want to have, in the code of  
bar(),
an instruction which will return the character string "foo" if bar() was
called from foo() and the string "clyde" if bar() was called from  
clyde().

Without really understanding what I'm doing I cobbled together the  
following:

fname <- as.character(sys.call(-1))[1]

This ***seems*** to work, at least in simple test cases.

But is it reliably robust?  Are there traps for young players that I am
not seeing?

My ``solution'' returns NA as the value of fname if bar() is called  
from the
command line, rather than being called by foo() or clyde().  This is  
acceptable.
I think ....

Any avuncular advice from those younger and wiser than myself? :-)

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list