I have a text file of R commands. Some times I only want to run a few lines of the R commands in an existing R session and wonder whether there is a simple way to do this. To run a few lines in a new session of R, I could use sed to pick up the lines from the file and pipe them into R. source() does not allow me to specify which lines to be included/excluded. Is there any function that is similar to source() but allows me to specify lines included/excluded? Paul. [[alternative HTML version deleted]]
On Fri, Mar 11, 2011 at 4:41 PM, Paul Y. Peng <pywpeng at gmail.com> wrote:> I have a text file of R commands. Some times I only want to run a few lines > of the R commands in an existing R session and wonder whether there is a > simple way to do this. > > To run a few lines in a new session of R, I could use sed to pick up the > lines from the file and pipe them into R. > > source() does not allow me to specify which lines to be included/excluded. > Is there any function that is similar to source() but allows me to specify > lines included/excluded? >To just source lines 15 through 16 try this: source(pipe("sed -n 15,16p myfile.R")) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
You can do: source(readLines(yourFile)[10:20]) # lines 10-20 of the file On Fri, Mar 11, 2011 at 4:41 PM, Paul Y. Peng <pywpeng at gmail.com> wrote:> I have a text file of R commands. Some times I only want to run a few lines > of the R commands in an existing R session and wonder whether there is a > simple way to do this. > > To run a few lines in a new session of R, I could use sed to pick up the > lines from the file and pipe them into R. > > source() does not allow me to specify which lines to be included/excluded. > Is there any function that is similar to source() but allows me to specify > lines included/excluded? > > Paul. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
I meant to say, but my fingers got ahead of my brain: source(textConnection(readLines(yourFile)[10:20])) On Fri, Mar 11, 2011 at 4:53 PM, jim holtman <jholtman at gmail.com> wrote:> You can do: > > source(readLines(yourFile)[10:20]) ? # lines 10-20 of the file > > On Fri, Mar 11, 2011 at 4:41 PM, Paul Y. Peng <pywpeng at gmail.com> wrote: >> I have a text file of R commands. Some times I only want to run a few lines >> of the R commands in an existing R session and wonder whether there is a >> simple way to do this. >> >> To run a few lines in a new session of R, I could use sed to pick up the >> lines from the file and pipe them into R. >> >> source() does not allow me to specify which lines to be included/excluded. >> Is there any function that is similar to source() but allows me to specify >> lines included/excluded? >> >> Paul. >> >> ? ? ? ?[[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > > > -- > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?