[Rd] Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Sat Jul 25 23:39:04 CEST 2020
On 25/07/2020 4:48 p.m., nospam using altfeld-im.de wrote:
> 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)?
Most Windows run-times (including the version of MSVCRT that R uses)
convert \n to \r\n on text files, so you rarely need an explicit \r\n.
That's the difference between type text and type binary on connections.
Duncan Murdoch
>
> 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()?)
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list