[R] Using source()

Michael Just mgjust at gmail.com
Thu Oct 16 09:05:12 CEST 2008


On Thu, Oct 16, 2008 at 1:42 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
>
> On Thu, 16 Oct 2008, Michael Just wrote:
>
>> Hello,
>
>> I have never used source and I am a R beginner.
>
> Not a 'beginner' R-help poster, though.  (32 posts this month so far under this name.)
I hope this isn't bad form on my part. My thought was beginners may
need the most help and therefore post the most. I hope my name or
posts won't cause fatigue.
>
>> If I have text file that contains 1,000's of lines of code. Can I use source to bring this code into R and execute the code?
>
> Yes.  But surely after you have written (or even understood) a file with 1,000's of lines of code you are no longer a 'beginner'.

I think I am still a beginner, hence all the questions. I am pretty
sure my code has redundancies and I use excel as an text editor, its
the fastest way I know how (lots of "dragging formulas"). I have
1,000's because I have 1000's of locations in my dataset and I am
running a few regressions on each. I think that this 1,000's of lines
R code should not discredit my 'beginner' status. I am still just
using univariate statistics and learning how to do EDA in R.
>
>> Does it run the code one line at a time?
>
> No.  It parses the file and runs the parsed code one expression at a time.
>
Thank you.
>
>> Is there a best way to setup source() for maximum effieciency?
>
> Worry about efficiency when you have to (when you are no longer a 'beginner'). Remember Jackson's Rules of Optimization (in the context of programming):
>
> 1) Don't do it.
> 2) (For experts only) Don't do it yet.
>
Thanks, I had to look up Jackson's Rules of Optimization. I asked
about efficiency to make sure that I wasn't suggesting to do something
totally ridiculous. The only 'programming' I do is for R and this is
partly why I have so many questions and furthermore posts.  I came to
R after using Excel or Systat for statistical analysis.

> But you can use other means to run large collections of code, e.g. make a package or use Rscript.
>
>> After reading ?source() would I just do:
>>
>> source("my_Rcode.txt")
>
> Yes (most people use extension 'R' for R source, though).  Note that you have to explicitly print() in code run this way: auto-printing is not done.

Thanks for .R and print() tips. I would not have done those.

>
>>
>> Thanks,
>> Michael
>>
>>        [[alternative HTML version deleted]]
>
> After so many postings, please do follow the posting guide.  (No HTML.)

Thanks for bringing this to my attention.



More information about the R-help mailing list