2

Is there an R equivalent for Environment.NewLine in .NET?

I'm looking for a character object that would represent a new line based on the environment, e.g. CR LF ("\r\n") on Windows and LF ("\n") on Unix. I couldn't find any such thing in the R documentation, or the default R options.

Narvarth
  • 345
  • 2
  • 13

2 Answers2

2

There’s no equivalent, but most of the time you won’t need it: as long as you’re writing to a text connection, the operating system will do the correct thing and treat '\n' according to the platform’s specification; for example, the documentation of writeLines says:

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).

Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141
  • I was initially using `write` and noticed that it called `cat` with `\n`, so I decided to go with `writeBin` to force `\r\n`. I guess as long as one sends text to a file, `writeLines` is fine. Still, too bad there's no equivalent. – Narvarth Oct 18 '17 at 20:56
0

The \n should still work:

> s = "line 1\nline 2"
> cat(s)
line 1
line 2

Here's a separate question which explains that print(s) doesn't quite work when trying to output strings with escape characters, and we should use cat or writeLine instead: Printing newlines with print() in R

lebelinoz
  • 4,380
  • 9
  • 27
  • 52