Suppose I have a file prog.R stored in a directory under ~/dirname, and ~/dirname is set in a shell script file (e.g. .cshrc) as one of the accessible paths on terminal. On a different directory I could run prog.R interactively by executing source("~/dirname/prog.R") It seems that source() does not search for all the paths set in the shell script. So, is there a better and more elegant way to do this, for example without explicitly typing the directory? TIA, Gang
Gabor Grothendieck
2008-Oct-02 03:49 UTC
[R] Running source() on a file in another directory
Suppose p is a vector of paths, e.g., p <- c("~/dir1", "~/dir2/dir3") Then the following will return the full pathname of the first found location: Find(file.exists, file.path(p, "prog.R")) so you can source that. On Wed, Oct 1, 2008 at 6:09 PM, Gang Chen <gangchen6 at gmail.com> wrote:> Suppose I have a file prog.R stored in a directory under ~/dirname, > and ~/dirname is set in a shell script file (e.g. .cshrc) as one of > the accessible paths on terminal. On a different directory I could run > prog.R interactively by executing > > source("~/dirname/prog.R") > > It seems that source() does not search for all the paths set in the > shell script. So, is there a better and more elegant way to do this, > for example without explicitly typing the directory? > > TIA, > Gang > > ______________________________________________ > 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. >
Thanks a lot! It seems to work fine now with what you suggested. I also tried another approach by running the following on the terminal R -f ~dir1/prog.R and it read in some lines in prog.R, but then the execution halted at one point for some reason I could not figure out. Any way I could get "R -f ..." working? Thanks, Gang On Wed, Oct 1, 2008 at 11:49 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Suppose p is a vector of paths, e.g., p <- c("~/dir1", "~/dir2/dir3") > Then the following will return the full pathname of the first found > location: > > Find(file.exists, file.path(p, "prog.R")) > > so you can source that. > > On Wed, Oct 1, 2008 at 6:09 PM, Gang Chen <gangchen6 at gmail.com> wrote: >> Suppose I have a file prog.R stored in a directory under ~/dirname, >> and ~/dirname is set in a shell script file (e.g. .cshrc) as one of >> the accessible paths on terminal. On a different directory I could run >> prog.R interactively by executing >> >> source("~/dirname/prog.R") >> >> It seems that source() does not search for all the paths set in the >> shell script. So, is there a better and more elegant way to do this, >> for example without explicitly typing the directory? >> >> TIA, >> Gang