[R] About source()

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Jan 4 10:39:04 CET 2012



On 03.01.2012 21:22, Li SUN wrote:
> Thanks, Rolf, Justin and Uwe!
>
> Actually I wanted to run .R file as a script, just like what people do
> for bash scripts or python scripts. It seems to me that adding
> "#!/usr/bin/R -f" at the first line is what I need. Is this true?


Perhaps even "Rscript" rather than "R".

Uwe Ligges

> Li Sun
>
>
> 2012/1/2 Rolf Turner<rolf.turner at xtra.co.nz>:
>> 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
>>
>>
>
> ______________________________________________
> 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.



More information about the R-help mailing list