Hi, I have recently started using R again (switched from MatLab) and have a question regarding programming. How can I set up a file that will run several lines of R code? For example, in Fortran and DOS this used to be done via a batch file, and in MatLab I would write the code and save it in a script file, e.g., Test.m. An example of what I am trying to do is the following: x=matrix(c(1,2,3,4,5,6,7,8,9),3,3) y=matrix(c(rnorm(9),3,3) z1=x*y z2=x%*%y eigen(z1) eigen(z2) I would like to store these commands in a file and simply run/call/invoke that file rather than have to type in the line commands everyday. The above is a very simple snapahot of what I am doing. Right Now I have a function that runs these commands. Also, I can easily COPY the commands from some editor (e.g., Word) and PASTE to the R Console, and R will automatically run the commands. The question is, Is there anyway to invoke a series of commands outside of the function? In MatLab, as I mentioned, I would save the commands in a script file say TEST.m and run the commands from the MatLab command line prompt simply by typing TEST <enter>. Thanks in advance. Rob
?source> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Kissell, Robert [EQRE] > Sent: Thursday, March 04, 2004 3:19 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Command Line Programs > > > Hi, > I have recently started using R again (switched from MatLab) > and have a question regarding programming. How can I set up a > file that will run several lines of R code? For example, in > Fortran and DOS this used to be done via a batch file, and in > MatLab I would write the code and save it in a script file, > e.g., Test.m. > > An example of what I am trying to do is the following: > > x=matrix(c(1,2,3,4,5,6,7,8,9),3,3) > y=matrix(c(rnorm(9),3,3) > z1=x*y > z2=x%*%y > > eigen(z1) > eigen(z2) > > > I would like to store these commands in a file and simply > run/call/invoke that file rather than have to type in the > line commands everyday. The above is a very simple snapahot > of what I am doing. > > Right Now I have a function that runs these commands. Also, I > can easily COPY the commands from some editor (e.g., Word) > and PASTE to the R Console, and R will automatically run the commands. > > The question is, Is there anyway to invoke a series of > commands outside of the function? In MatLab, as I mentioned, > I would save the commands in a script file say TEST.m and run > the commands from the MatLab command line prompt simply by > typing TEST <enter>. > > Thanks in advance. > > > Rob > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help > PLEASE > do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Save the lines in a file. At the R prompt, you would type source("myscript.R"). Outside of R, it depends on which OS you're using. On *nix, you can do R CMD BATCH script.R. On Windows, read the rw-FAQ. Andy> From: Kissell, Robert [EQRE] > > Hi, > I have recently started using R again (switched from MatLab) > and have a question regarding programming. How can I set up a > file that will run several lines of R code? For example, in > Fortran and DOS this used to be done via a batch file, and in > MatLab I would write the code and save it in a script file, > e.g., Test.m. > > An example of what I am trying to do is the following: > > x=matrix(c(1,2,3,4,5,6,7,8,9),3,3) > y=matrix(c(rnorm(9),3,3) > z1=x*y > z2=x%*%y > > eigen(z1) > eigen(z2) > > > I would like to store these commands in a file and simply > run/call/invoke that file rather than have to type in the > line commands everyday. The above is a very simple snapahot > of what I am doing. > > Right Now I have a function that runs these commands. Also, I > can easily COPY the commands from some editor (e.g., Word) > and PASTE to the R Console, and R will automatically run the commands. > > The question is, Is there anyway to invoke a series of > commands outside of the function? In MatLab, as I mentioned, > I would save the commands in a script file say TEST.m and run > the commands from the MatLab command line prompt simply by > typing TEST <enter>. > > Thanks in advance. > > > Rob > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
"Kissell, Robert [EQRE]" <robert.kissell at citigroup.com> writes:> Right Now I have a function that runs these commands. Also, I can > easily COPY the commands from some editor (e.g., Word) and PASTE to > the R Console, and R will automatically run the commands. > > The question is, Is there anyway to invoke a series of commands > outside of the function? In MatLab, as I mentioned, I would save the > commands in a script file say TEST.m and run the commands from the > MatLab command line prompt simply by typing TEST <enter>. > > Thanks in advance.source("foo.R") # in R R CMD BATCH foo.R # from a command line interface Rcmd BATCH foo.R # Windows variation of same Was this really not to be found in any of the documents available to you? -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Kissell, Robert [EQRE] wrote:>How can I set up a file that will run several lines of R code? For example, in Fortran and DOS this used to be done via a batch file, and in MatLab I would write the code and save it in a script file, e.g., Test.m. > >From within R: source('yourscript.R',echo=T) From the command line (under unix/Linux): R BATCH yourscript.R or R <yourscript.R -- Christophe