user1827356
user1827356

Reputation: 7022

Find the source file containing R function definition

I come from a python background and am trying to get up to speed with R, so please bear with me

I have an R file - util.R with the following lines:

util.add <- function(a,b) a + b
util.sub <- function(a,b) { a - b }

I source it as follows:

source('path/util.R')

I now have two function objects and want to write a function as follows:

getFilePath(util.add)

that would give me this result

[1] "path/util.R"

Upvotes: 15

Views: 2665

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226192

Digging into the srcref attribute of one of the loaded functions appears to work, if you go deep enough ...

source("tmp/tmpsrc.R")
str(util.add)
## function (a, b)  
##  - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 13 1 31 13 31 1 1
##   .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x8fffb18> 
srcfile <- attr(attr(util.add,"srcref"),"srcfile")
ls(srcfile)
## [1] "Enc"           "filename"      "fixedNewlines" "isFile"       
## [5] "lines"         "parseData"     "timestamp"     "wd"    
srcfile$filename
## [1] "tmp/tmpsrc.R"

Comments note that this only works out-of-the-box for interactive code, because ...

Rscript does not load srcrefs by default. A work-around is this code snippet: Rscript -e "options(keep.source = TRUE); source('your_main_file.R')" or set the option keep.source permanently in the .Rprofile file (see ?Startup for details)

Upvotes: 13

Michael Barrowman
Michael Barrowman

Reputation: 1191

I know this was solved years ago, but I've just come across it and realised that there is a bit more to this if you use the body() function.

The raw function has only the one attribute, "srcref" which contains the code of the function, along with it's own attributes and class of "srcref" (which dictates how it'll get printed).

The body() of a function, such as body(util.add) has three attributes.

  • "srcref" which contains the body of the function stored as a list of expressions.
  • "srcfile" which contains the source file of the function (which is what you are looking for in this question)
  • "wholeSrcref" which points to the entire source file.

This gives you an alternative (although slightly slower) method to extract the source file name attr(body(util.add),"srcfile"), along with being able to see (although not interact with) the sibling functions (i.e. the other functions loaded in the same source file).

Not sure if it's useful, but it could be interesting.

Let's also not forget about the %@% infix operator for accessing attributes using the {purrr} package, with this we could use the more succinct (although again, slower) piece of code as: util.add%@%srcref%@%srcfile

Upvotes: 2

Related Questions