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()?)
Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?
3 messages · @osp@m m@iii@g oii @itieid-im@de, Duncan Murdoch, M. Edward (Ed) Borasky
On 25/07/2020 4:48 p.m., nospam at 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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
I will also add that shell scripts that are in Docker containers will often crash with confusing error messages if they have Windows line endings.
On Sat, Jul 25, 2020 at 2:39 PM Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
On 25/07/2020 4:48 p.m., nospam at 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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Borasky Research Journal https://www.znmeb.mobi Markovs of the world, unite! You have nothing to lose but your chains!