Exactly where does R search for foo.R if I type source("foo.R")? Only from current working directory (same as getwd()), from all directories specified by e.g. $PATH? Thanks. Daehyok Shin
In case no one who knows has time to reply to this, I will report on my empirical investigation of this question using R 1.9.1 under Windows 2000. First, I saved a simple script file "tst-source.R" in the working directory, e.g., "d:/sg/proj1". When I said, "source('tst-source.R')", it sourced the file appropriately. Then I moved this file to the immediate parent, e.g., "d:/sg" and tried the same source command. It replied, "Error ... unable to open connection ... ." Then I got a command prompt, said, "path", and moved the file into one of the directories in the search path. When I repeated the "source" command, it was still "unable to open connection ... ." Conclusion: From this and other experiences, I have found three ways to specify file names: (1) If the complete path and file name are supplied for an existing file, 'source' will find it. (2) If a file is in the working directory, specifying that name will get it. (3) If a file is in a subdirectory of the working directory, e.g., "d:/sg/proj1/sub1/tst-source.R", then specifying "source('sub1/tst-source.R')" will get it. hope this helps. spencer graves Shin, Daehyok wrote:>Exactly where does R search for foo.R if I type source("foo.R")? >Only from current working directory (same as getwd()), from all directories >specified by e.g. $PATH? Thanks. > >Daehyok Shin > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
> From: Shin, Daehyok > > Exactly where does R search for foo.R if I type source("foo.R")? > Only from current working directory (same as getwd()), from > all directories > specified by e.g. $PATH? Thanks. > > Daehyok ShinThe former. No documentation says otherwise, so why would you think that it might search somewhere else? Andy
The reason I asked is to separate script files from data files. Usually, I am working in the directory containing data files, but some script files are in other shared directories. In the case, is there any way to access the script files conveniently without specifying its absolute path? In other word, any way to set up default search paths for script files? Daehyok Shin (Peter)> > The former. No documentation says otherwise, so why would you > think that it > might search somewhere else? > > Andy > > > ------------------------------------------------------------------ > ------------ > Notice: This e-mail message, together with any attachments, > contains information of Merck & Co., Inc. (One Merck Drive, > Whitehouse Station, New Jersey, USA 08889), and/or its affiliates > (which may be known outside the United States as Merck Frosst, > Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be > confidential, proprietary copyrighted and/or legally privileged. > It is intended solely for the use of the individual or entity > named on this message. If you are not the intended recipient, > and have received this message in error, please notify us > immediately by reply e-mail and then delete it from your system. > ------------------------------------------------------------------ > ------------ >
Not really. The best I can come up with is something like: runScript <- function(script, dir="", ...) source(file.path(dir, script, ...) scriptdir <- "/path/to/scripts" runScript(scriptdir, "myScript.R") Andy> From: Shin, Daehyok > > The reason I asked is to separate script files from data files. > Usually, I am working in the directory containing data files, but some > script files are in other shared directories. In the case, is > there any way > to access the script files conveniently without specifying > its absolute > path? In other word, any way to set up default search paths for script > files? > > Daehyok Shin (Peter) > > > > > The former. No documentation says otherwise, so why would you > > think that it > > might search somewhere else? > > > > Andy > > > > > > ------------------------------------------------------------------ > > ------------ > > Notice: This e-mail message, together with any attachments, > > contains information of Merck & Co., Inc. (One Merck Drive, > > Whitehouse Station, New Jersey, USA 08889), and/or its affiliates > > (which may be known outside the United States as Merck Frosst, > > Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be > > confidential, proprietary copyrighted and/or legally privileged. > > It is intended solely for the use of the individual or entity > > named on this message. If you are not the intended recipient, > > and have received this message in error, please notify us > > immediately by reply e-mail and then delete it from your system. > > ------------------------------------------------------------------ > > ------------ > > > > >
How about this: search.path <- function(fn, paths = strsplit(Sys.getenv("PATH"), split = ";")[[1]], fsep = "\\") { for(d in paths) { f <- file.path(d, fn, fsep = fsep) if (file.exists(f)) return(f) } return(NULL) } source(search.path("myscript.R")) Shin, Daehyok <sdhyok <at> email.unc.edu> writes: : : Considering replies to my question, typical practices of R users seem: : 1. Creating a special function to source frequently used scripts. : 2. Creating a personal package containing frequently used scripts. : : Both of them needs additional steps to edit the function or to : create/install the package : when a script file is edited or added. My suggestion can save the effort. : In the sense to make R more convenient environment to users, is it trivial? : : Daehyok Shin : : > -----Original Message----- : > From: Roger D. Peng [mailto:rpeng <at> jhsph.edu] : > Sent: Saturday, July 10, 2004 PM 11:14 : > To: sdhyok <at> email.unc.edu : > Cc: Liaw, Andy; R, Help : > Subject: Re: [R] where does R search when source()? : > : > : > In fact, there is an elegant solution, and that is to write a : > package. If this is all for personal use, then writing a package : > can be as simple as creating a few directories, copying the : > script files, and then running R CMD INSTALL. I do this all the : > time when I have multiple projects that use the same code. : > : > -roger : > : : ______________________________________________ : R-help <at> stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : :
I do agree with you: in my opinion, creating a package is not a general solution when you just want to save the script of a whole data analysis for the purpose of, say, a paper or a report. To meet this goal, I save the script in a text file and I use a text editor with sourcing facilities (e.g. WinEdt + R-WinEdt, Xemacs + ESS,...: see Software ==> Other section on CRAN). See recent threads on this topic, e.g. http://tolstoy.newcastle.edu.au/R/help/04/07/0195.html Best, Renaud Shin, Daehyok wrote:> Considering replies to my question, typical practices of R users seem: > 1. Creating a special function to source frequently used scripts. > 2. Creating a personal package containing frequently used scripts. > > Both of them needs additional steps to edit the function or to > create/install the package > when a script file is edited or added. My suggestion can save the effort. > In the sense to make R more convenient environment to users, is it trivial? > > Daehyok Shin > > >>-----Original Message----- >>From: Roger D. Peng [mailto:rpeng at jhsph.edu] >>Sent: Saturday, July 10, 2004 PM 11:14 >>To: sdhyok at email.unc.edu >>Cc: Liaw, Andy; R, Help >>Subject: Re: [R] where does R search when source()? >> >> >>In fact, there is an elegant solution, and that is to write a >>package. If this is all for personal use, then writing a package >>can be as simple as creating a few directories, copying the >>script files, and then running R CMD INSTALL. I do this all the >>time when I have multiple projects that use the same code. >> >>-roger >> > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Dr Renaud Lancelot, v??t??rinaire C/0 Ambassade de France - SCAC BP 834 Antananarivo 101 - Madagascar e-mail: renaud.lancelot at cirad.fr tel.: +261 32 04 824 55 (cell) +261 20 22 665 36 ext. 225 (work) +261 20 22 494 37 (home)