@osp@m m@iii@g oii @itieid-im@de
2020-Jul-25 20:48 UTC
[Rd] Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?
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()?)
Duncan Murdoch
2020-Jul-25 21:39 UTC
[Rd] Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?
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 >
M. Edward (Ed) Borasky
2020-Jul-25 22:11 UTC
[Rd] Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?
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!
Apparently Analagous Threads
- Guidelines when to use LF vs CRLF ("\n" vs. "\r\n") on Windows for new lines (line endings)?
- should dovecot store maildir files with CRLF or LF?
- [PATCH] contrib/cygwin/ssh-host-config: Fix a CRLF/LF issue
- iconv to UTF-16 encoding produces error due to embedded nulls (write.table with fileEncoding param)
- iconv to UTF-16 encoding produces error due to embedded nulls (write.table with fileEncoding param)