75

I'm new using R. I'm trying to add (append) new lines to a file with my existing data in R. The problem is that my data has about 30000 rows and 13000 cols. I already try to add a line with the writeLines function but the resulting file contains only the line added.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
Sergio Vela
  • 751
  • 1
  • 5
  • 3
  • Possible duplicate of [Write lines of text to a file in R](http://stackoverflow.com/questions/2470248/write-lines-of-text-to-a-file-in-r) – Michael Ohlrogge Jan 23 '17 at 22:26
  • 3
    @MichaelOhlrogge Are you sure this is a dupe? The Q [Write lines of text to a file in R](http://stackoverflow.com/q/2470248/3817004) you have linked to is about writing lines to a file in general while this Q asks specifically about _appending_ lines to an existing file. – Uwe Jan 23 '17 at 23:04
  • @UweBlock True, but the linked Q discusses the append option in many of its answers. – Michael Ohlrogge Jan 23 '17 at 23:33
  • @MichaelOhlrogge It's just 2 of the 8 answers in the linked Q. Both suggest `cat()`. Both are rather terse. None of them explains why they use the `append` option or the effect this option has. No other answer (`writeLines()`, `sink()`) mention or discuss the `append` option. – Uwe Jan 24 '17 at 08:35
  • @UweBlock Ok, sure, I'll retract – Michael Ohlrogge Jan 24 '17 at 15:25

4 Answers4

127

Have you tried using the write function?

line="blah text blah blah etc etc"
write(line,file="myfile.txt",append=TRUE)
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
  • 13
    make sure you don't use a connection as file, but just put in the file path, otherwise append won't work! – Ansjovis86 Oct 28 '17 at 19:48
  • 1
    @Ansjovis86 could you pls elaborate a bit more on "connection as file"? – munmunbb Dec 20 '17 at 05:47
  • @munmunbb This is only when you use a connect setup. If you don't have it don't worry about. If you have it, drop it from your code and this answer will work. – Ansjovis86 Dec 21 '17 at 09:12
  • 1
    While this may be very obvious to many, I had alway previously read the existing file, used rbind to add a line to it and then written the updated file again. I now see that this is grossly inefficient and that using write to append a line is far faster. – JamesF Aug 24 '18 at 11:43
  • 1
    `write` is not really meant to write a simple string to a file. [Use `cat` for that](https://stackoverflow.com/a/7741730/1048186). As [the documentation for the write command](https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/write) states, "The data (usually a matrix) `x` are written to file `file`." – Josiah Yoder Jun 10 '21 at 18:39
  • I've had issues with `write` doing very odd things with my data -- I'm guessing because it is somehow interpreting it. – Josiah Yoder Jun 10 '21 at 18:50
42

write.table, write.csv and others all have the append= argument, which appends append=TRUE and usually overwrites if append=FALSE. So which one you want to / have to use, depends on your data.

By the way, cat() can also be used to write text to a file and also has the append= argument.

joran
  • 169,992
  • 32
  • 429
  • 468
Rainer
  • 8,347
  • 1
  • 23
  • 28
  • 7
    Or even `sink(append = TRUE)`. – Roman Luštrik Oct 12 '11 at 14:35
  • 1
    For plain text data, use `cat()` instead of `write()`. As [the documentation for the write command](https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/write) states, "The data (usually a matrix) `x` are written to file `file`." `write()` is not meant for simple string data. It's meant to format more complex types and it wraps around `cat()` to do the actual writing to the file. When using `cat()`, you will need to `paste0(line,"\n")` to put a newline on the end of the file. – Josiah Yoder Jun 10 '21 at 18:53
1
lapply(listOfVector, function(anyNameofVect){ write(anyNameofVect, file="outputFileName", sep="\t", append=TRUE, ncolumns=100000) })

or

lapply(listOfVector, write, file="outputFileName", sep="\t", append=TRUE, ncolumns=100000)
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
0

You can open a connection in append mode to append lines to an existing file with writeLines.

writeLines("Hello", "output.txt") #Create file
CON <- file("output.txt", "a")    #Open connection to append
writeLines("World", CON)          #Append: World
writeLines("End", CON)            #Append: End
close(CON)                        #Close connection

The same but using cat.

cat("Hello\n", file = "output.txt")
cat("World\n", file = "output.txt", append = TRUE)
cat("End", file = "output.txt", append = TRUE)

In case of creating a file and appending subsequent.

CON <- file("output.txt", "w")
writeLines("Hello", CON)
writeLines("World", CON)
writeLines("End", CON)
close(CON)
GKi
  • 37,245
  • 2
  • 26
  • 48