Michael
2012-Jun-05 20:58 UTC
[R] How do I obtain the current active path of a function that's being called?
Hi all, How do I obtain the current active path of a function that's being called? That's to say, I have several source files and they all contain definition of function A. I would like to figure out which function A and from which file is the one that's being called and is currently active? Thanks a lot! [[alternative HTML version deleted]]
Greg Snow
2012-Jun-05 21:35 UTC
[R] How do I obtain the current active path of a function that's being called?
There are several ways that a function can come into being within R, it can be sourced from a file like in your case, but it could also be typed in by hand at the command prompt, or created by another function, etc. So R does not in general keep links to the files from which the file was generated. Some of the development programs for R (such as rstudio) may keep track of this for you. Or you could put some unique assignment at the start of each version of the function then just look at the code for the function to see what that assignment is. Unfortunately an R only general solution would need ESP of some sort and my version of an esp package is still very pre-alpha, it suggests "amply dromedary support effect damper photogenic nonce" and unless that means something to you I don't think it will be much help for a while. You will need to find a way to keep track for yourself. On Tue, Jun 5, 2012 at 2:58 PM, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > How do I obtain the current active path of a function that's being called? > > That's to say, I have several source files and they all contain definition > of function A. > > I would like to figure out which function A and from which file is the one > that's being called and is currently active? > > Thanks a lot! > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
Andrew Miles
2012-Jun-05 21:35 UTC
[R] How do I obtain the current active path of a function that's being called?
Can you provide an example of the code file that you use to call the different functions? Without that it might be hard for people to answer your question. Offhand, I'd say that it is whatever version of function A was called last. If you are loading functions into the workspace, they are treated as any other object, which is to say that you can only have one function of the same name at a time. Hence whenever you call a source file to load in function A, the old function A gets overwritten. Andrew Miles On Jun 5, 2012, at 4:58 PM, Michael wrote:> Hi all, > > How do I obtain the current active path of a function that's being called? > > That's to say, I have several source files and they all contain definition > of function A. > > I would like to figure out which function A and from which file is the one > that's being called and is currently active? > > Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.
Gabor Grothendieck
2012-Jun-06 10:52 UTC
[R] How do I obtain the current active path of a function that's being called?
On Tue, Jun 5, 2012 at 4:58 PM, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > How do I obtain the current active path of a function that's being called? > > That's to say, I have several source files and they all contain definition > of function A. > > I would like to figure out which function A and from which file is the one > that's being called and is currently active? >Here are a few possibilities: 1. In each source file add this line: this.file <- sys.frame(1)$ofile Then if you do this: source("a.R") the variable this.file will be left in your global environment so you can find out what your last file read in was. A slightly more sophisticated version creates a stack of file names: if (!exists("this.file")) this.file <- NULL this.file <- unique(c(sys.frame(1)$ofile, this.file)) This is a bit of a hack. If the internals of source change then you will have to change this code in a corresponding manner. 2. source each file into a separate environment. source("a.R", local = a <- new.env(parent = .GlobalEnv)) # if f is a function in a.R then call it like this: a$f(...whatever...) Be sure you source each file into a different environment. 3. In the source file a.R write each function like this: f <- function() # a.R { ...whatever... } Then as.character(attr(f, "srcfile"))[2] will show the comment. Be sure that getOption("keep.source") is TRUE (normally it is) or if its not feasible to arrange that then set it in each source call: source("a.R", keep.source = TRUE) 4. put your functions in packages. search() will show the packages you have loaded in order. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Duncan Murdoch
2012-Jun-07 09:04 UTC
[R] How do I obtain the current active path of a function that's being called?
On 12-06-05 4:58 PM, Michael wrote:> Hi all, > > How do I obtain the current active path of a function that's being called? > > That's to say, I have several source files and they all contain definition > of function A. > > I would like to figure out which function A and from which file is the one > that's being called and is currently active?You've had lots of good suggestions so far. One more possibility: getSrcFilename and the related functions in the same help topic will usually tell you the filename and other location information for functions that you source(). Duncan Murdoch