[R] A question about using getSrcDirectory() with R/Rscript

Cormac Long clong150 at gmail.com
Wed Aug 17 10:31:25 CEST 2011


Good morning R-help,

I have an idiot question: I would like to use getSrcDirectory()
and friends to allow me to identify where an R file has been
called from when invoked using Rscript. If I understand the
documentation correctly, the following example should work:

In file test.R:
   options(keep.source=T)
   fn<-function(x){x<-x+1}
   srcDir<-getSrcDirectory(fn)
   print(srcDir)

I attempted the following invocations of Rscript:
   + Rscript test.R
   + Rscript <full_path>/test.R

I attempted the following invocations using R:
   + source("test.R")
   + Manually entering the function

In both attempts, the variable srcDir is a zero-length character
vector. Digging into the documentation, I notice that getSrcDirectory()
looks for a "srcref" attribute in the function body. In neither R
nor Rscript is this attribute set when declaring the function.

So: what am I missing?

Comments:
   + I have 'keep.source' option set to TRUE in both R and Rscript
     (irritatingly, it's default is TRUE in R and FALSE in Rscript
      - why is this?)
   + I have tested this with:
        o R 2.13.1 on Ubuntu 10.10 (server)
        o R 2.13.0 on Windows 7

Best wishes,
Cormac.



More information about the R-help mailing list