Dimitri Liakhovitski
2011-Feb-24 15:21 UTC
[R] Running code sequentially from separate scripts (but not functions)
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
Jonathan P Daily
2011-Feb-24 15:39 UTC
[R] Running code sequentially from separate scripts (but not functions)
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2011-Feb-24 15:39 UTC
[R] Running code sequentially from separate scripts (but not functions)
You say you are aware that you can source them from a common script, yet you don't seem satisfied with that. Perhaps you could be more specific about why that solution is not satisfactory? One thing you said could indicate a misunderstanding... you do not have to wrap the code in functions before you source the files. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Dimitri Liakhovitski <dimitri.liakhovitski@gmail.com> wrote: 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@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. [[alternative HTML version deleted]]
rex.dwyer at syngenta.com
2011-Feb-24 15:51 UTC
[R] Running code sequentially from separate scripts (but not functions)
You don't need to write functions to "source" files: source("code1.R") source("code2.R") source("code3.R") When you source a file with a bunch of function definitions, the definitions are just assignment statements: f <- function (x)... g <- function (x,y,z) ... Did you think you would break your computer if you just tried this to see if it worked? :) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, February 24, 2011 10:22 AM To: r-help Subject: [R] Running code sequentially from separate scripts (but not functions) 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. message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.
Dimitri Liakhovitski
2011-Feb-24 16:05 UTC
[R] Running code sequentially from separate scripts (but not functions)
Thanks a lot, everyone! Honestly - I just did not know one could source everything and not just functions. What you wrote is simple, but pretty eye-opening for me (and extremely helpful)! Dimitri On Thu, Feb 24, 2011 at 10:51 AM, <rex.dwyer at syngenta.com> wrote:> You don't need to write functions to "source" files: > > source("code1.R") > source("code2.R") > source("code3.R") > > When you source a file with a bunch of function definitions, the definitions are just assignment statements: > > f <- function (x)... > g <- function (x,y,z) ... > > Did you think you would break your computer if you just tried this to see if it worked? ?:) > > > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski > Sent: Thursday, February 24, 2011 10:22 AM > To: r-help > Subject: [R] Running code sequentially from separate scripts (but not functions) > > 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. > > > > > message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited. > >-- Dimitri Liakhovitski Ninah Consulting www.ninah.com