Hi all, Forgive me if this question has been addressed, but I was unable to find anything in the r-help list or in cyberspace. My question is this: is there a function, or set of functions, that will enable a script to detect its own path? I have tried file.path() but that was not what I was looking for. It would be nice to be able to put all the related scripts I use in the same folder with a "master" script and then source() them in that "master" script. Problem is, the "master" script must first know where it is (without me having to open it and retype the path every time I move it). Any ideas? Stu ps. I found this on the internet but a) I couldn't understand it, and b) it didn't work: frame_files <- lapply(sys.frames(), function(x) x$ofile) frame_files <- Filter(Negate(is.null), frame_files) PATH <- dirname(frame_files[[length(frame_files)]]) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Stu Field, PhD Postdoctoral Fellow Department of Biology Colorado State University 1878 Campus Delivery Fort Collins, CO 80523-1878 Office: E208 Anatomy/Zoology Phone: (970) 491-5744 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [[alternative HTML version deleted]]
Try this: PATH <- dirname(sys.frame(1)$ofile) On Wed, Sep 29, 2010 at 5:00 PM, Stu Field <sgf@colostate.edu> wrote:> Hi all, > > Forgive me if this question has been addressed, but I was unable to find > anything in the r-help list or in cyberspace. My question is this: is there > a function, or set of functions, that will enable a script to detect its own > path? I have tried file.path() but that was not what I was looking for. It > would be nice to be able to put all the related scripts I use in the same > folder with a "master" script and then source() them in that "master" > script. Problem is, the "master" script must first know where it is (without > me having to open it and retype the path every time I move it). > > Any ideas? > Stu > > ps. I found this on the internet but a) I couldn't understand it, and b) it > didn't work: > > frame_files <- lapply(sys.frames(), function(x) x$ofile) > frame_files <- Filter(Negate(is.null), frame_files) > PATH <- dirname(frame_files[[length(frame_files)]]) > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Stu Field, PhD > Postdoctoral Fellow > Department of Biology > Colorado State University > 1878 Campus Delivery > Fort Collins, CO 80523-1878 > Office: E208 Anatomy/Zoology > Phone: (970) 491-5744 > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
> > Forgive me if this question has been addressed, but I was unable to find anything in the r-help list or in cyberspace. My question is this: is there a function, or set of functions, that will enable a script to detect its own path? I have tried file.path() but that was not what I was looking for. It would be nice to be able to put all the related scripts I use in the same folder with a "master" script and then source() them in that "master" script. Problem is, the "master" script must first know where it is (without me having to open it and retype the path every time I move it).Instead of trying to work out where your script is located, when you source it in, just make sure the working directory is set correctly: source("/path/to/my/script.r", chdir = T) chdir is the very useful, but under advertised, argument to source(). Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/