[R] About source()

Rolf Turner rolf.turner at xtra.co.nz
Tue Jan 3 07:50:56 CET 2012


On 03/01/12 17:02, Li SUN wrote:
> Hello,
>
> I am a beginner to the R language and find it fantastic and
> well-designed, quite different from other programming languages.
     What a refreshingly sensible attitude!!! :-)
> This is the first time I post on the r-help mailing list.
>
> In invoking the function source(filename), it seems that the filename
> has to exist in the current working directory, otherwise it has to be
> specified in full path. So is there any mechanism(such as environment
> variable) to specify an additional directory of .R files that source()
> could search in?

I'm no expert on this, and others may correct me, but

     (1) I don't believe any such mechanism exists.

     (2) It's probably not a good idea, even if such a mechanism
     were to exist.  Directories have a tree structure, rather than
     being linearly ordered in the way that data bases on your
     R search path are ordered.

     I believe you would run all sorts of risks of confusion and
     of getting the wrong file were you to invoke such a mechanism.

     It is ``good practice'' to have separate directories associated
     with different projects and to situate all files, that you might wish
     to source in respect of a given project, in the directory associated
     with that project.

I know that this is an irritating sort of response --- ``No, you can't
do that, and you shouldn't do it anyway!'' --- but I sincerely believe
this to be true.

That being said, I also believe that you could program up such a mechanism
yourself. I.e. build a function "source2()" which would have a "hard 
coded" list
of directories to search, and would make use of the "try()" function.

Might be a good exercise for you, given that you are starting out in R
and looking to upgrade your skills! :-)

     cheers,

         Rolf Turner



More information about the R-help mailing list