Dear R-users, When a function f1() is called, is there a way to know, from inside the function f1(), if f1() is directly called from the R console, or indirectly from another function f2() ? Of course, I may add an argument to f1(..., callbyf2=FALSE) only used by f2() giving explicitely this info, but I would like to know if there may be a more generic way ? Thanks for any info or pointer
vincent at 7d4.com writes:> Dear R-users, > > When a function f1() is called, is there a way to know, > from inside the function f1(), if f1() is directly called from the > R console, or indirectly from another function f2() ? > > Of course, I may add an argument to f1(..., callbyf2=FALSE) only used > by f2() giving explicitely this info, but I would like to know > if there may be a more generic way ? > > Thanks for any info or pointerYou can always query via sys.status/sys.call/sys.function etc. Or, if the issue is whether the call is from the command line: identical(parent.frame(), globalenv()) However, it does look a bit like unsound programming practice. Why do you perceive a need to do this? -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Perhaps you what you want to do is to return an object
that has a print method like this:
f1 <- function(x) structure(c(x, x^2), class = "f1")
print.f1 <- function(x) cat("x is", x[1], "x squared is",
x[2], "\n")
Now you could do this:
f1(3) # gives fancy output
y <- f1(3) # returns structure
unclass(y) # y, not as an "f1" class object
The point is that print.f1 could return something which is different
than the output of f1.
On 6/19/06, vincent at 7d4.com <vincent at 7d4.com>
wrote:> Dear R-users,
>
> When a function f1() is called, is there a way to know,
> from inside the function f1(), if f1() is directly called from the
> R console, or indirectly from another function f2() ?
>
> Of course, I may add an argument to f1(..., callbyf2=FALSE) only used
> by f2() giving explicitely this info, but I would like to know
> if there may be a more generic way ?
>
> Thanks for any info or pointer
>
> ______________________________________________
> R-help at 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
>