Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 715
user1827356 Avatar asked Sep 23 '15 18:09

user1827356


People also ask

How do I see the source code of a function in R?

If you want to view the code built-in to the R interpreter, you will need to download/unpack the R sources; or you can view the sources online via the R Subversion repository or Winston Chang's github mirror.

What does source () mean in R?

You can use the source function in R to reuse functions that you create in another R script. This function uses the following basic syntax: source("path/to/some/file.R") Simply add this line to the top of your R script and you'll be able to use any functions defined in file.


2 Answers

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"
like image 61
Ben Bolker Avatar answered Oct 25 '22 01:10

Ben Bolker


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

like image 38
Michael Barrowman Avatar answered Oct 24 '22 23:10

Michael Barrowman