-2

Possible Duplicate:
Write lines of text to a file in R

Is there anyway to write information to a file in R like other languages such as Python or Java? I mean first open a file to be written and then write the contents of what I do line by line. Actually I do not want to put all my data in a matrix or list first. Thanks

Community
  • 1
  • 1
Layla
  • 5,234
  • 15
  • 51
  • 66

2 Answers2

4

You want ?connections.

For example, from the help for ?file:

 zz <- file("ex.data", "w")  # open an output file connection
 cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
 cat("One more line\n", file = zz)
 close(zz)
 readLines("ex.data")

[1] "TITLE extra line" "2 3 5 7" "" "11 13 17"
[5] "One more line"

Clean up:

 unlink("ex.data")

See ?cat, ?writeLines, ?writeBin, and several others in the See Also section of the ?connections help page.

You can also append with write.table, either with its append argument or by writing to an open connection.

mdsumner
  • 29,099
  • 6
  • 83
  • 91
2

You could use ?write. You can write line by line using append=TRUE. Also ?sink might be of interest to you.

johannes
  • 14,043
  • 5
  • 40
  • 51