[Rd] Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?

@osp@m m@iii@g oii @itieid-im@de @osp@m m@iii@g oii @itieid-im@de
Sat Jul 25 22:48:33 CEST 2020


Dear R developers,

I am developing an R package which returns strings with new line codes.
I am not sure if I should use "\r\n" or "\n" in my returned strings on Windows platforms.

What is the recommended best practice for package developers (and code in base R) for coding new lines in strings?

And just out of curiosity: What is the reason (or history) for preferring "\n" in R even on Windows (see examples below)?

Best regards

Jürgen

PS: Examples from base R:

R seems to use (almost) only "\n" for new lines internally - even on Windows platforms, eg.:

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

    # eol default is "\n"
    write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
                eol = "\n", na = "NA", dec = ".", row.names = TRUE,
                col.names = TRUE, qmethod = c("escape", "double"),
                fileEncoding = "")

On the other hand some external interfaces require Windows-style new lines ("\r\n"), eg. text file outputs seen ti care internally:

    writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)
    # Excerpt from the documentation:
    #     Normally writeLines is used with a text-mode connection,
    #     and the default separator is converted to the normal separator
    #     for that platform (LF on Unix/Linux, CRLF on Windows).

    # calls internally do_writelines():
    # https://github.com/wch/r-source/blob/8db7b85953127f364f52d201ec057911db4601e5/src/main/connections.c#L4023
    # But: Where is the conversion done (hidden in the call to Riconv()?)



More information about the R-devel mailing list