You must open the file for writing:
out = file('testfile', 'w') ...
When R opens (or does not open) connections automatically, it is a little complicated, but explained in the help ( ?file ).
If you don't pass 'w' , each write call opens and closes the file, and I assume this causes the weird behavior you observe.
If you want to open an existing file to add, use
out = file('testfile', 'a')
Johnb
source share