Is there a way to know where is the source, so as to make a source call inside another source smarter? As an example: file1.R is in directory /files/dir1/ file2.R is in directory /files/dir1/dir2/ In file1.R, there is this line: source("dir2/file2.R") So, if I setwd to /files/dir1/, and then I call source("file1.R"), it will run correctly. However, if I setwd to /files, then call source("dir1/file1.R"), it will give an error when trying to source file2.R Alberto Monteiro
the smarter thing is to write a package, so you don't need source() at all. but your problem will be fixed if you use the full path instead: source("/home/user/dir1/dir2/file2.R") (and obviously you could pass the path as an argument to whatever function you're using...) b On Jul 16, 2007, at 6:11 PM, Alberto Monteiro wrote:> Is there a way to know where is the source, so as to make a source > call > inside another source smarter? > > As an example: > > file1.R is in directory /files/dir1/ > > file2.R is in directory /files/dir1/dir2/ > > In file1.R, there is this line: > > source("dir2/file2.R") > > So, if I setwd to /files/dir1/, and then I call source("file1.R"), > it will run correctly. However, if I setwd to /files, then > call source("dir1/file1.R"), it will give an error when > trying to source file2.R > > Alberto Monteiro > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
There is a package available at http://www.omegahat.org/Prerelease/RSource_0.1-1.tar.gz that has an extended source() that maintains a list of directories in which to search for files and a stack of the files currently being source()d so that one can determine what is currently going on. It has extra stuff in there for dealing with encrypted files and also some experiments on I/O using C code which you may not need or want installed. D. Alberto Monteiro wrote:> Is there a way to know where is the source, so as to make a source call > inside another source smarter? > > As an example: > > file1.R is in directory /files/dir1/ > > file2.R is in directory /files/dir1/dir2/ > > In file1.R, there is this line: > > source("dir2/file2.R") > > So, if I setwd to /files/dir1/, and then I call source("file1.R"), > it will run correctly. However, if I setwd to /files, then > call source("dir1/file1.R"), it will give an error when > trying to source file2.R > > Alberto Monteiro > > ______________________________________________ > R-help at stat.math.ethz.ch 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.