R File I/O Capability - Writing output to specific lines of existing file

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

R File I/O Capability - Writing output to specific lines of existing file

JustADude

Currently I am using the R "write" command to output results to a *.txt file and then copying those results into an initialization file.  In an attempt to continue to automate the process I would like to have R write to the location in the existing initialization file, instead of me copying the data over.  

By any chance are there any R commands to help?

Primarily, I will be using Windows, and the initialization file is a simple flat file, i.e. not XML or binary.

I looked at:
(a) http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/R.io/html/00Index.html

(b) http://search.r-project.org/cgi-bin/namazu.cgi?query=File+I%2FO&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08

But, this did not appear to provide the functionality to edit an existing file by adding information to the middle of the file.

Thank you again for any help and insight.

______________________________________________
[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
|

Re: R File I/O Capability - Writing output to specific lines of existing file

jholtman
You can always read in the initialization file, make the updates to it
and then write it back out.  If it is a text file, it would be very
hard to write into the middle of it since there is no structure to the
file.  You can read it in as a table (read.table) or just as lines
(readLines) and the make any changes you want.  You can use regular
expressions if you want to put something in the middle of one of the
lines you have read in.

On Wed, Apr 8, 2009 at 10:13 AM, Jason Rupert <[hidden email]> wrote:

>
> Currently I am using the R "write" command to output results to a *.txt file and then copying those results into an initialization file.  In an attempt to continue to automate the process I would like to have R write to the location in the existing initialization file, instead of me copying the data over.
>
> By any chance are there any R commands to help?
>
> Primarily, I will be using Windows, and the initialization file is a simple flat file, i.e. not XML or binary.
>
> I looked at:
> (a) http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/R.io/html/00Index.html
>
> (b) http://search.r-project.org/cgi-bin/namazu.cgi?query=File+I%2FO&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08
>
> But, this did not appear to provide the functionality to edit an existing file by adding information to the middle of the file.
>
> Thank you again for any help and insight.
>
> ______________________________________________
> [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
Cincinnati, OH
+1 513 646 9390

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
|

Re: R File I/O Capability - Writing output to specific lines of existing file

JustADude

Darn.

I was afraid of this.  Always kind of weak when it comes to file I/O approaches, so I guess I will have to stretch and try to put something together.  

Yeah.  The input flat text file is about 600 lines long.  I will be replacing about 200.  I already have the template for those 200 lines.  I guess I will just need to read down to line 400 and replace until line 600.  

Thanks again for all the insights regarding this.



--- On Wed, 4/8/09, jim holtman <[hidden email]> wrote:

> From: jim holtman <[hidden email]>
> Subject: Re: [R] R File I/O Capability - Writing output to specific lines of  existing file
> To: [hidden email]
> Cc: [hidden email]
> Date: Wednesday, April 8, 2009, 8:02 PM
> You can always read in the initialization file, make the
> updates to it
> and then write it back out.  If it is a text file, it would
> be very
> hard to write into the middle of it since there is no
> structure to the
> file.  You can read it in as a table (read.table) or just
> as lines
> (readLines) and the make any changes you want.  You can use
> regular
> expressions if you want to put something in the middle of
> one of the
> lines you have read in.
>
> On Wed, Apr 8, 2009 at 10:13 AM, Jason Rupert
> <[hidden email]> wrote:
> >
> > Currently I am using the R "write" command
> to output results to a *.txt file and then copying those
> results into an initialization file.  In an attempt to
> continue to automate the process I would like to have R
> write to the location in the existing initialization file,
> instead of me copying the data over.
> >
> > By any chance are there any R commands to help?
> >
> > Primarily, I will be using Windows, and the
> initialization file is a simple flat file, i.e. not XML or
> binary.
> >
> > I looked at:
> > (a)
> http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/R.io/html/00Index.html
> >
> > (b)
> http://search.r-project.org/cgi-bin/namazu.cgi?query=File+I%2FO&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08
> >
> > But, this did not appear to provide the functionality
> to edit an existing file by adding information to the middle
> of the file.
> >
> > Thank you again for any help and insight.
> >
> > ______________________________________________
> > [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
> Cincinnati, OH
> +1 513 646 9390
>
> 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
|

Template Engine Package for R?

JustADude

Related to the posting below, by any chance is there a "Template Engine" package for R?

http://en.wikipedia.org/wiki/Template_processor

This may make this type of editing much easier?  Maybe...

Thanks again.



--- On Wed, 4/8/09, Jason Rupert <[hidden email]> wrote:

> From: Jason Rupert <[hidden email]>
> Subject: Re: [R] R File I/O Capability - Writing output to specific lines of existing file
> To: "jim holtman" <[hidden email]>
> Cc: [hidden email]
> Date: Wednesday, April 8, 2009, 11:26 PM
> Darn.
>
> I was afraid of this.  Always kind of weak when it comes to
> file I/O approaches, so I guess I will have to stretch and
> try to put something together.  
>
> Yeah.  The input flat text file is about 600 lines long.  I
> will be replacing about 200.  I already have the template
> for those 200 lines.  I guess I will just need to read down
> to line 400 and replace until line 600.  
>
> Thanks again for all the insights regarding this.
>
>
>
> --- On Wed, 4/8/09, jim holtman <[hidden email]>
> wrote:
>
> > From: jim holtman <[hidden email]>
> > Subject: Re: [R] R File I/O Capability - Writing
> output to specific lines of  existing file
> > To: [hidden email]
> > Cc: [hidden email]
> > Date: Wednesday, April 8, 2009, 8:02 PM
> > You can always read in the initialization file, make the
> > updates to it and then write it back out.  If it is a text
> > file, it would be very hard to write into the middle of
> > it since there is no structure to the file.  You can read
> > it in as a table (read.table) or just as lines
> > (readLines) and the make any changes you want.  You can use
> > regular expressions if you want to put something in the
> > middle of one of the lines you have read in.
> >
> > On Wed, Apr 8, 2009 at 10:13 AM, Jason Rupert
> > <[hidden email]> wrote:
> > >
> > Currently I am using the R "write"command
> > to output results to a *.txt file and then copying those
> > results into an initialization file.  In an attempt to
> > continue to automate the process I would like to have R
> > write to the location in the existing initialization file,
> > instead of me copying the data over.
> > By any chance are there any R commands to help?
> > Primarily, I will be using Windows, and the
> > initialization file is a simple flat file, i.e. not XML or
> > binary.
> >
> > I looked at:
> > (a)
> http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/R.io/html/00Index.html
> > (b) http://search.r-project.org/cgi-bin/namazu.cgi?query=File+I%2FO&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08
> >
> > But, this did not appear to provide the functionality
> > to edit an existing file by adding information to the middle
> > of the file.
> >
> > Thank you again for any help and insight.
> >




______________________________________________
[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
|

Re: R File I/O Capability - Writing output to specific lines of existing file

jholtman
In reply to this post by JustADude
With only 600 lines, it is relatively easy to read it in and replace
some lines.  For example (untested) is you wanted to replace lines
150:250 with your data, you would do

input <- readLines('filename')
input <- c(input[1:149], newData, input[251:length(input)])
writeLines(input, file='filename')

That does not seem too hard.

On Thu, Apr 9, 2009 at 12:26 AM, Jason Rupert <[hidden email]> wrote:

>
> Darn.
>
> I was afraid of this.  Always kind of weak when it comes to file I/O approaches, so I guess I will have to stretch and try to put something together.
>
> Yeah.  The input flat text file is about 600 lines long.  I will be replacing about 200.  I already have the template for those 200 lines.  I guess I will just need to read down to line 400 and replace until line 600.
>
> Thanks again for all the insights regarding this.
>
>
>
> --- On Wed, 4/8/09, jim holtman <[hidden email]> wrote:
>
>> From: jim holtman <[hidden email]>
>> Subject: Re: [R] R File I/O Capability - Writing output to specific lines of  existing file
>> To: [hidden email]
>> Cc: [hidden email]
>> Date: Wednesday, April 8, 2009, 8:02 PM
>> You can always read in the initialization file, make the
>> updates to it
>> and then write it back out.  If it is a text file, it would
>> be very
>> hard to write into the middle of it since there is no
>> structure to the
>> file.  You can read it in as a table (read.table) or just
>> as lines
>> (readLines) and the make any changes you want.  You can use
>> regular
>> expressions if you want to put something in the middle of
>> one of the
>> lines you have read in.
>>
>> On Wed, Apr 8, 2009 at 10:13 AM, Jason Rupert
>> <[hidden email]> wrote:
>> >
>> > Currently I am using the R "write" command
>> to output results to a *.txt file and then copying those
>> results into an initialization file.  In an attempt to
>> continue to automate the process I would like to have R
>> write to the location in the existing initialization file,
>> instead of me copying the data over.
>> >
>> > By any chance are there any R commands to help?
>> >
>> > Primarily, I will be using Windows, and the
>> initialization file is a simple flat file, i.e. not XML or
>> binary.
>> >
>> > I looked at:
>> > (a)
>> http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/R.io/html/00Index.html
>> >
>> > (b)
>> http://search.r-project.org/cgi-bin/namazu.cgi?query=File+I%2FO&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08
>> >
>> > But, this did not appear to provide the functionality
>> to edit an existing file by adding information to the middle
>> of the file.
>> >
>> > Thank you again for any help and insight.
>> >
>> > ______________________________________________
>> > [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
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>
>
>
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

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
|

Re: Template Engine Package for R?

barry rowlingson
In reply to this post by JustADude
On Thu, Apr 9, 2009 at 12:42 PM, Jason Rupert <[hidden email]> wrote:
>
> Related to the posting below, by any chance is there a "Template Engine" package for R?
>
> http://en.wikipedia.org/wiki/Template_processor
>
> This may make this type of editing much easier?  Maybe...

Have you tried the 'brew' package? It lets you embed tagged R
expressions inside text files:

http://ftp.heanet.ie/mirrors/cran.r-project.org/web/packages/brew/index.html

 (or a mirror near you)

Barry

______________________________________________
[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
|

Re: Template Engine Package for R?

Jeffrey Horner
In reply to this post by JustADude
Jason Rupert wrote:
> Related to the posting below, by any chance is there a "Template Engine" package for R?

Indeed there is. It's called brew:

http://cran.r-project.org/web/packages/brew/index.html

Jeff

>
> http://en.wikipedia.org/wiki/Template_processor
>
> This may make this type of editing much easier?  Maybe...
>
> Thanks again.
>
>
>
> --- On Wed, 4/8/09, Jason Rupert <[hidden email]> wrote:
>
>> From: Jason Rupert <[hidden email]>
>> Subject: Re: [R] R File I/O Capability - Writing output to specific lines of existing file
>> To: "jim holtman" <[hidden email]>
>> Cc: [hidden email]
>> Date: Wednesday, April 8, 2009, 11:26 PM
>> Darn.
>>
>> I was afraid of this.  Always kind of weak when it comes to
>> file I/O approaches, so I guess I will have to stretch and
>> try to put something together.  
>>
>> Yeah.  The input flat text file is about 600 lines long.  I
>> will be replacing about 200.  I already have the template
>> for those 200 lines.  I guess I will just need to read down
>> to line 400 and replace until line 600.  
>>
>> Thanks again for all the insights regarding this.
>>
>>
>>
>> --- On Wed, 4/8/09, jim holtman <[hidden email]>
>> wrote:
>>
>>> From: jim holtman <[hidden email]>
>>> Subject: Re: [R] R File I/O Capability - Writing
>> output to specific lines of  existing file
>>> To: [hidden email]
>>> Cc: [hidden email]
>>> Date: Wednesday, April 8, 2009, 8:02 PM
>>> You can always read in the initialization file, make the
>>> updates to it and then write it back out.  If it is a text
>>> file, it would be very hard to write into the middle of
>>> it since there is no structure to the file.  You can read
>>> it in as a table (read.table) or just as lines
>>> (readLines) and the make any changes you want.  You can use
>>> regular expressions if you want to put something in the
>>> middle of one of the lines you have read in.
>>>
>>> On Wed, Apr 8, 2009 at 10:13 AM, Jason Rupert
>>> <[hidden email]> wrote:
>>> Currently I am using the R "write"command
>>> to output results to a *.txt file and then copying those
>>> results into an initialization file.  In an attempt to
>>> continue to automate the process I would like to have R
>>> write to the location in the existing initialization file,
>>> instead of me copying the data over.
>>> By any chance are there any R commands to help?
>>> Primarily, I will be using Windows, and the
>>> initialization file is a simple flat file, i.e. not XML or
>>> binary.
>>>
>>> I looked at:
>>> (a)
>> http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/R.io/html/00Index.html
>>> (b) http://search.r-project.org/cgi-bin/namazu.cgi?query=File+I%2FO&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08
>>>
>>> But, this did not appear to provide the functionality
>>> to edit an existing file by adding information to the middle
>>> of the file.
>>>
>>> Thank you again for any help and insight.
>>>
>
>
>
>
> ______________________________________________
> [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.


--
http://biostat.mc.vanderbilt.edu/JeffreyHorner

______________________________________________
[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.