[R] Using source()

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 16 08:42:57 CEST 2008


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.)

> 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'.

> 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.

> 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.

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,
> Michael
>
> 	[[alternative HTML version deleted]]

After so many postings, please do follow the posting guide.  (No HTML.)

> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list