Berry Boessenkool
Berry Boessenkool

Reputation: 1538

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")?

Upvotes: 1

Views: 56

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78832

A far more succinct, platform agnositc alternative that also uses the curl package:

library(curl)
readLines(curl(link, handle=new_handle(dirlistonly=TRUE, ftp_use_epsv=TRUE)))
## [1] "hourly"       "daily"        "monthly"      "multi_annual" "subdaily"

Upvotes: 0

Tensibai
Tensibai

Reputation: 15784

I would split on all carriage return, with a regex including both and filter empty entries (cause by \n\r doing two split) 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"

Upvotes: 1

Related Questions