[R] Appending strings at the beginning of a text file

Paul Smith phhs80 at gmail.com
Wed Aug 26 15:31:35 CEST 2009


Henrique,

With paste(), it works only if the file has only one line; otherwise,

"Text at beginning of file"

is repeated after each line of the file.

Paul


On Wed, Aug 26, 2009 at 2:03 PM, Henrique Dallazuanna<wwwhsd at gmail.com> wrote:
> It works with paste, but I forgotten the close(fConn) after the writeLines.
>
> On Tue, Aug 25, 2009 at 2:48 PM, Paul Smith <phhs80 at gmail.com> wrote:
>>
>> Thanks, Henrique.
>>
>> I think you mean the following:
>>
>> fConn <- file('test.txt', 'r+')
>> Lines <- readLines(fConn)
>> writeLines(c("Text at beginning of file\n", Lines), con = fConn)
>> close(fConn)
>>
>> (With paste(), it does not work.)
>>
>> Paul
>>
>> On Tue, Aug 25, 2009 at 5:02 PM, Henrique Dallazuanna<wwwhsd at gmail.com>
>> wrote:
>> > Try this;
>> >
>> > fConn <- file('test.txt', 'r+')
>> > Lines <- readLines(fConn)
>> > writeLines(paste("Text at beginning of file", Lines, sep = "\n"),
>> >            con = fConn)
>> >
>> > On Tue, Aug 25, 2009 at 12:54 PM, Paul Smith <phhs80 at gmail.com> wrote:
>> >>
>> >> Dear All,
>> >>
>> >> I have a piece of text that I want to append to a text file at the
>> >> beginning of the text file.
>> >>
>> >> I have thought about using cat() with the option 'append=T', but the
>> >> appending, in this case, is done at the bottom of the text file. Any
>> >> ideas?
>> >>
>> >> Thanks in advance,
>> >>
>> >> Paul
>> >>
>> >> ______________________________________________
>> >> 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.
>> >
>> >
>> >
>> > --
>> > Henrique Dallazuanna
>> > Curitiba-Paraná-Brasil
>> > 25° 25' 40" S 49° 16' 22" O
>> >
>>
>> ______________________________________________
>> 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.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>




More information about the R-help mailing list