Cormac Long
2011-Aug-17 08:31 UTC
[R] A question about using getSrcDirectory() with R/Rscript
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.
Uwe Ligges
2011-Aug-18 11:13 UTC
[R] A question about using getSrcDirectory() with R/Rscript
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.