0

I am simply pasting two strings but the created new line is not platform specific but always "\n".

I was expecting that the new line is converted to the platform specific code(s) where R is running:

charToRaw(paste0("a", "\n", "b"))
[1] 61 0a 62

I would expect

[1] 61 0d 0a 62

on Windows and

[1] 61 0a 62

on all other platforms.

How can I achieve this without explicitly using the correct platform-specific new line code(s)?

PS: I do not want to hard code "\r\n" to stay platform-independent...

See also: New line constant

Edit 1: Even when I print the string the "\n" is not converted to "\r\n" on Windows: charToRaw(print(paste0("a", "\n", "b")))

r2evans
  • 141,215
  • 6
  • 77
  • 149
R Yoda
  • 8,358
  • 2
  • 50
  • 87

1 Answers1

1

The premise of your assumption is flawed.

You are using a literal "\n" and hoping that R will infer that you want to make it "\r\n". If this were the case, then it would be impossible (or indeed very tricky) for R on windows to create a non-CRLF document for non-windows uses.

I am not recommending you install data.table's package, but its fwrite function uses this to determine what each line's ending should be:

str(formals(data.table::fwrite))
# Dotted pair list of 23
# ...truncated...
#  $ eol         : language if (.Platform$OS.type == "windows") "\r\n" else "\n"
# ...truncated...

I suggest you use something like this in your code:

EOL <- if (.Platform$OS.type == "windows") "\r\n" else "\n"
charToRaw(paste0("a", EOL, "b"))
# [1] 61 0d 0a 62

On linux, this works out to be

EOL <- if (.Platform$OS.type == "windows") "\r\n" else "\n"
charToRaw(paste0("a", EOL, "b"))
# [1] 61 0a 62

as one would hope/expect.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    THX, this is indeed what I am doing currently but I am still wondering why R seems to be completely new line agnostic (or I am blind to see the solution ;-) – R Yoda Jul 23 '20 at 14:08
  • 1
    R is not always agnostic, though it appears inconsistent at times. I agree that similar to `.Platform$file.sep`, it would seem logical to have `.Platform$eol` (sounds like a really simple/good PR for R-devel). The hard part would be convincing R-core that it should be *used* in base functions. For instance, `formals(writeLines)$sep` is hard-coded to `"\n"` on both windows and linux, perhaps it should be either a global option or default to a platform-specific value. (I for one dislike CRLF, but I recognize that is far from a universal preference.) – r2evans Jul 23 '20 at 14:12
  • 1
    I have mailed a question at r-devel: https://stat.ethz.ch/pipermail/r-devel/2020-July/079802.html – R Yoda Jul 25 '20 at 22:40
  • From [Duncan's comment](https://stat.ethz.ch/pipermail/r-devel/2020-July/079803.html), using `file("filename", open="w")` and `open="wb"` (arguments to `writeLines(..., con=file(...))`) produce CRLF and CR files (on windows), respectively. In linux it produces CR both times. Not sure if that's a help or just the less-convenient way you know about and are trying to shortcut. – r2evans Jul 26 '20 at 19:51