Quantcast

How to source a part of the file

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to source a part of the file

Paul Y. Peng-2
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

Gabor Grothendieck
On Fri, Mar 11, 2011 at 4:41 PM, Paul Y. Peng <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

jholtman
In reply to this post by Paul Y. Peng-2
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

jholtman
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 <[hidden email]> 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 <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

Paul Y. Peng-2
Many thanks to Jim Holtman and Gabor Grothendieck for your quick responses.
Jim's solution works beautifully for my tasks. Thanks. I also tried Gabor's
solution based on pipe(). Unfortunately it only works on a linux PC, not on
Windows 7/Vista. Submitting the command in Windows results in nothing. I
suspect that it is because pipe() function behaviors differently in the two
systems.


On Fri, Mar 11, 2011 at 4:56 PM, jim holtman <[hidden email]> wrote:

> 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 <[hidden email]> 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 <[hidden email]> 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]]
> >>
> >> ______________________________________________
> >> [hidden email] 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?
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

Gabor Grothendieck
On Sun, Mar 13, 2011 at 11:32 PM, Paul Y. Peng <[hidden email]> wrote:
> Many thanks to Jim Holtman and Gabor Grothendieck for your quick responses.
> Jim's solution works beautifully for my tasks. Thanks. I also tried Gabor's
> solution based on pipe(). Unfortunately it only works on a linux PC, not on
> Windows 7/Vista. Submitting the command in Windows results in nothing. I
> suspect that it is because pipe() function behaviors differently in the two
> systems.
>
>

You did specifically write in your post that you could use sed which
suggests that you had it on your system.

If that is not, in fact, the case you could get it from Duncan
Murdoch's Rtools distribution:
http://www.murdoch-sutherland.com/Rtools/
which you would need in any case if you intend to develop R packages on Windows.

I did run a test on my own Windows Vista system prior to posting so it
certainly does work on Windows under the assumptions of the post.

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

Paul Y. Peng-2
Yes, I do have Rtools installed on two Windows PCs I tested, and I used them
to build R packages. I use sed all the time with no problems. Thanks for
confirming me that it works on your Windows PC. I will test it again and
will update you if I find out what's wrong in my pc.

On Sun, Mar 13, 2011 at 11:56 PM, Gabor Grothendieck <
[hidden email]> wrote:

> On Sun, Mar 13, 2011 at 11:32 PM, Paul Y. Peng <[hidden email]> wrote:
> > Many thanks to Jim Holtman and Gabor Grothendieck for your quick
> responses.
> > Jim's solution works beautifully for my tasks. Thanks. I also tried
> Gabor's
> > solution based on pipe(). Unfortunately it only works on a linux PC, not
> on
> > Windows 7/Vista. Submitting the command in Windows results in nothing. I
> > suspect that it is because pipe() function behaviors differently in the
> two
> > systems.
> >
> >
>
> You did specifically write in your post that you could use sed which
> suggests that you had it on your system.
>
> If that is not, in fact, the case you could get it from Duncan
> Murdoch's Rtools distribution:
> http://www.murdoch-sutherland.com/Rtools/
> which you would need in any case if you intend to develop R packages on
> Windows.
>
> I did run a test on my own Windows Vista system prior to posting so it
> certainly does work on Windows under the assumptions of the post.
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to source a part of the file

Gabor Grothendieck
On Mon, Mar 14, 2011 at 12:07 AM, Paul Y. Peng <[hidden email]> wrote:
> Yes, I do have Rtools installed on two Windows PCs I tested, and I used them
> to build R packages. I use sed all the time with no problems. Thanks for
> confirming me that it works on your Windows PC. I will test it again and
> will update you if I find out what's wrong in my pc.

Presumably you don't have it on your path at the point that you are using R.

There are some batch files at http://batchfiles.googlecode.com that
not only call R but also temporarily add Rtools to the path if they
find Rtools on your system.

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Loading...