Adding text to the recording function [R]

I am trying to add a line to an existing file .txt. But my syntax overwrites this file :(

   fileConn <- file( "realization1.txt" )
      write(x =as.character(max(cumsum( rnorm( 10^7)))),
            file = fileConn,
            append = TRUE, sep = " ")


      write(x =as.character(max(cumsum( rnorm( 10^7)))),
            file = fileConn,
            append = TRUE, sep = " ")
   }

   close( fileConn )

Does anyone have any solutions? Thanks for the help!

+4
source share
3 answers

I believe that your difficulty arises from the inability to open the file with the corresponding attributes.

If you create a connection with fileConn <- file( "realization1.txt" ,open="a"), then everything will work as you expect. Basically, as far as I can tell, write(which is a wrapper for cat) cannot be added if the connection to the file was not opened with the β€œadd”.

+4
source

writeLines, 20 , write. , .

sink("outfile.txt", append = T)

x <- as.character(max(cumsum( rnorm( 10^7))))
writeLines(x)

sink()
+1

I would just use the write.table command

write.table(max(cumsum( rnorm( 10^7))),file="realization1.txt",append=TRUE,row.names=FALSE,col.names=FALSE)

write.table(max(cumsum( rnorm( 10^7))),file="realization1.txt",append=TRUE,row.names=FALSE,col.names=FALSE)

You will find 2 values ​​in the file 'realizaion1.txt'

0
source

All Articles