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

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Aug 18 13:13:58 CEST 2011


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:
> 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.
>
> ______________________________________________
> 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.



More information about the R-help mailing list