Skip to content

A question about using getSrcDirectory() with R/Rscript

3 messages · Cormac Long, Uwe Ligges

#
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.
1 day later
#
Works for me in R-patched:

I guess your problem is that you have to set the options() before 
source()ing.

Best,
Uwe Ligges
On 17.08.2011 10:31, Cormac Long wrote:
#
Dear Uwe,

Thanks for you suggestion. I should have spotted that (not thinking
about the order of commands properly). I still don't know why it failed
to work in RGui for me. It works now. I suspect it must be another
case of PEBCAK (Problem Exists Between Chair And Keyboard).

Best wishes,
Cormac.


2011/8/18 Uwe Ligges <ligges at statistik.tu-dortmund.de>: