1

Is there a new line constant that's platform independent in R? I'm used to C# and there's Environment.NewLine which will return \r\n on windows and \n otherwise. Searching turned up nothing, but I assume there has to be something somewhere so that scripts can be platform independent.

Related question: Is there a way to detect the platform a script is running on? This could be useful to know for other reasons (which I haven't thought of yet).

EDIT: Here's why I'm asking. I'm downloading files from an FTP server, but want to get a list of files and only download files that are on the server that don't exist locally. Here's how I'm getting the list of files:

filesonserver <- unlist(strsplit(getURL(basePath, ftp.use.epsv=F, dirlistonly=T), "\n"))

On windows, the files are separated by \r\n. On my mac (where I'm currently working), they're separated by \n. I was looking for a way to make this platform independent. I haven't tried just separating by \n on windows, which might work. There might also be a way to get the list of files as a vector without having to split them, which would avoid this entirely...

Tim Coker
  • 6,484
  • 2
  • 31
  • 62
  • For the platform info, how about `R.Version()$platform` or `sessionInfo()$platform`? – Jota Mar 12 '16 at 19:41
  • 1
    http://stackoverflow.com/questions/4747715/how-to-check-the-os-within-r – rawr Mar 12 '16 at 19:42
  • 2
    Generally you shouldn't have to worry about newlines. R handles a lot of the complexity of reading text for you. – Hong Ooi Mar 12 '16 at 19:42

2 Answers2

1

The package tryCatchLog has a function determine.platform.NewLine():

If you consequently use this string instead of hard-coded "\n" your new lines will work platform-independently.

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

The answer to the initial question appears to be there isn't a new line constant like C# has. But it doesn't matter in my case, as the comments pointed out. It didn't occur to me until after I edited in the details that I probably didn't need to worry about it. Splitting by \n works fine on windows, even though the string containing the files names returned by getURL() is split by \r\n.

Tim Coker
  • 6,484
  • 2
  • 31
  • 62