0

I want to write write a string that contains the usual new line character of R (\n) into a column of a database table.

How can I convert the new line into operating system specific representation (Windows = CR/LF, Linux = LF, Mac = CR...)?

I have learned that R does not provide the operating system specific representation so I have to find a work-around:

Any trial to print/cat the string did fail:

msg <- "I want to have \n a new line"
cat(msg)
# I want to have 
#  a new line

out <- capture.output(cat(msg))
out
# a vector with two elements (one for each row but no new line characters anymore)
# [1] "I want to have " " a new line"

paste(out, collapse = "\n")   # how could I inject the correct new line characters here?
# [1] "I want to have \n a new line"

# welcome endless-loop :-)

Is there any way to let R create the correct new line characters from \n in a string?

PS: I am playing around with the built-in tcltk package and puts but I always end with R "reconverting" the newline into \n... Another "cheat" could be to enclose the \n with quotation marks to read it as if it were one line. I have no idea so far how this could work...

R Yoda
  • 8,358
  • 2
  • 50
  • 87

2 Answers2

3

One way to correctly set the new line code in R is to query the operating system. Since both OS X and Linux behave the same way, it's a question of determining whether the operating system is Windows. One way to do this is to interrogate the OS environment variable as follows.

if(substr(Sys.getenv("OS"),1,7) == "Windows") {
    # set Windows newline
  newLine <- "\r\n"
}
else {
    # set non-Windows newline
    newLine <- "\n"
}

Next use paste() with the newLine object to generate the right characters for new line by operating system.

paste("my text string on a line",newline,sep="")

regards,

Len

Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • 2
    In fact that is my current work-around (using `Sys.info()`) and I would have expected that R provides such functionality OOTB since it is able to handle this correctly internally. – R Yoda Nov 25 '17 at 08:08
  • @RYoda -- that's one of the idiosyncrasies of R as an open source product. Sometimes "obvious" features aren't implemented. – Len Greski Nov 25 '17 at 16:59
  • I have just accepted your answer but do also post my personal solution later as separate answer which uses a slightly different apprach. THX again! – R Yoda Nov 28 '17 at 18:38
0

Here you find my final implementation as a possible alternative to the accepted answer:

# Returns the operating system specific new line character(s):
# CR LF on Windows, else only LF...
# Simlar to Microsofts .Net "Environment.NewLine"
platform.NewLine <- function() {

  is.windows <- grepl(tolower(.Platform$OS.type), "windows", fixed = TRUE)

  if (is.windows) {
    newline <- "\r\n"
  } else {
    newline <- "\n"
  }

  sys.name <- Sys.info()["sysname"]
  is.windows.2nd.opinion <- grepl(tolower(sys.name), "windows", fixed = TRUE)

  if (is.windows != is.windows.2nd.opinion)
    warning("R seems to run on Windows OS but this could not be recognized for sure")

  return(newline)
}

# Usage (examples) ------------------------------------------------------------------------------------------------

newline <- platform.NewLine()

# "print" shows the "symbolic" names (escape codes)
print(paste("Line1", "Line2", sep = newline))
# [1] "Line1\r\nLine2"
# uses "\n" or "\r\n" depending on your OS

# "cat" applies the newline escape codes to the output
cat(paste("Line1", "Line2", sep = newline))
# Line1
# Line2
R Yoda
  • 8,358
  • 2
  • 50
  • 87