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

jim holtman jholtman at gmail.com
Thu Apr 9 13:54:13 CEST 2009


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 <jasonkrupert at yahoo.com> 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 <jholtman at gmail.com> wrote:
>
>> From: jim holtman <jholtman at gmail.com>
>> Subject: Re: [R] R File I/O Capability - Writing output to specific lines of  existing file
>> To: jasonkrupert at yahoo.com
>> Cc: R-help at r-project.org
>> 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
>> <jasonkrupert at yahoo.com> 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.
>> >
>> > ______________________________________________
>> > 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
>> 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?




More information about the R-help mailing list