Berry Boessenkool Berry Boessenkool - 10 days ago 5
R Question

What line break does RCurl return on a mac?

RCurl
returns different line breaks of an ftp directory on Windows and Linux systems.

if(!requireNamespace("RCurl", quietly=TRUE)) install.packages("RCurl")
link <- "ftp://ftp-cdc.dwd.de/pub/CDC/observations_germany/climate"
RCurl::getURL(link, ftp.use.epsv=TRUE, dirlistonly=TRUE)
Sys.info()['sysname'] ; .Platform$OS.type

# Windows / windows: "climate\r\nphenology\r\nradiosondes\r\nclimate_urban\r\n"
# Linux / unix: "climate\nphenology\nradiosondes\nclimate_urban\n"


To get a vector of character strings, I can handle both with

p <- strsplit(p, "\n")[[1]]
p <- gsub("\r", "", p) # additional carriage return in windows libcurl


But: it seems that mac may return
\r
only from the eol explanation in https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html. In that case, my approach would fail.

Can someone with a mac test and post the mac (
Sys.info()['sysname']
: "Darwin") output? How about solaris ("SunOS")?

Answer

I would split on all carraige return, with a regex including both and filter empty entries after like this:

t<-"climate\r\nphenology\r\nradiosondes\r\nclimate_urban\r\n"
t1<-"climate\nphenology\nradiosondes\nclimate_urban\n" 

 p<-unlist(strsplit(t,"[\n\r]"))
 p<-p[nchar(p)>0]

 p1<-unlist(strsplit(t1,"[\n\r]"))
 p1<-p[nchar(p1)>0]

Which Gives:

> p
[1] "climate"       "phenology"     "radiosondes"   "climate_urban"
> p1
[1] "climate"       "phenology"     "radiosondes"   "climate_urban"
Comments