[R] Running code sequentially from separate scripts (but not functions)

Jonathan P Daily jdaily at usgs.gov
Thu Feb 24 16:39:15 CET 2011


If you are just wanting to run them once, you can just do:

source("code1.r")
source("code2.r")
source("code3.r")

or place the file names in a vector, say 'filenames' and:

sapply(filenames, source)

If you want to use them multiple times, try:

run1 <- parse("code1.r")
run2 <- parse("code2.r")
run3 <- parse("code3.r")

# Use this to run anytime
eval(run1)
eval(run2)
eval(run3)
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

r-help-bounces at r-project.org wrote on 02/24/2011 10:21:47 AM:

> [image removed] 
> 
> [R] Running code sequentially from separate scripts (but not functions)
> 
> Dimitri Liakhovitski 
> 
> to:
> 
> r-help
> 
> 02/24/2011 10:24 AM
> 
> Sent by:
> 
> r-help-bounces at r-project.org
> 
> Hello!
> 
> I am wondering if it's possible to run - in sequence - code that is
> stored in several R scripts.
> For example:
> 
> Script in the file "code1.r" contains the code:
> a = 3; b = 5; c = a + b
> 
> Script in the file "code2.r" contains the code:
> d = 10; e = d - c
> 
> Script in the file "code3.r" contains the code:
> result=e/a
> 
> I understand that I could write those 3 scripts as 3 functions and
> source them from another script.
> But maybe there is a way of having them run one by one as such?
> 
> Thanks a lot!
> 
> -- 
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
> 
> ______________________________________________
> 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