MBLT MBLT - 1 year ago 109
R Question

R: Getting Unix-like linebreak LF writing files with cat()

I try to write a character vector to a textfile under Windows 7 / R 3.2.2 x64, and I want unix LF - not Windows CRLF:

v <- c("a","b","c")


> a[CRLF]
> b[CRLF]
> c[CRLF]



> a[CRLF]
> b[CRLF]
> c

I have also tried write.table(eol="\n") - unsuccessfully as it seems to use cat internally.

I have looked for other workarounds;
I tried to find sth. in R\src\main\scan.c, locating the relevant code in line 387ff.

Anyone who knows how I can get UNIX-like LF in my output file?

xb. xb.
Answer Source

Try to open a file connection in "binary" mode (instead of "text" mode) to prevent system-dependent encoding of the end-of-line:

v <- c("a","b","c")
f <- file("textfile.txt", open="wb")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download