On Aug 5, 2010, at 2:48 PM, Archana Dayalu wrote:

Hello,
I am using read.table to read files directly from a public ftp site. I have a general list of files that may or may not exist in the ftp directory, but my hope was that R would read the file if it existed and ignored it if it didn't exist and move on to the next one. However, when R arrives at a file that does not exist I get the error message "Error in file(file, "rt") : cannot open the connection" This makes sense, but I was wondering if there was any way I could circumvent this error message and have R instead give me
a warning message without terminating my entire loop.

Yes.

?try


Ideally, I would get a
warning message saying the connection does not exist, and then have R skip
to the next file.
My code is copied below.


Something like these modifications .... untested:

hourly.years <- c(2000:2008)
hourly.species <- c('ch4','co2','co')
station.names <-
c ('alt482n00 ','chm449n00 ','egb444n01 ','etl454n00','fsd449n00','llb454n01','wsa443n00','cdl453n00')
for (kk in hourly.years) {
   for (i in hourly.species) {
       for (nn in station.names) {
           file1 <- paste('ftp://gaw.kishou.go.jp/pub/data/current/
',i,'/hourly/y',kk,'/',nn,'.ec.as.cn.',i,'.nl.hr',kk,'.anc',sep='')
#ancillary data
           file2 <- paste('ftp://gaw.kishou.go.jp/pub/data/current/
',i,'/hourly/y',kk,'/',nn,'.ec.as.cn.',i,'.nl.hr',kk,'.dat',sep='')
#concentration data
dumm.anc <- try( read.table(file1,skip=32,header=F,as.is=T) )
if (class(dumm.anc) == "try-error" {} else {
           colnames(dumm.anc) <- c('DATE','TIME','WD','WS','RH','AT')
           r.obj.anc <- paste(substr(nn,1,3),i,kk,'anc.cont',sep='.')
           assign(r.obj.anc,dumm.anc)
                                             }
dumm.dat <- try( read.table(file2,skip=32,header=F,as.is=T) )
if (class(dumm.dat) == "try-error" {} else { #will skip over following commands if "try-error-ed"
           colnames(dumm.dat) <-
c('DATE','TIME','DATE','TIME','CH4','ND','SD','F','CS','REM')
            r.obj.dat <- paste(substr(nn,1,3),i,kk,'dat.cont',sep='.')
            assign(r.obj.dat,dumm.dat)
                  }

# --------------presumably these do not depend on the read- tries---------
status<-paste(i,nn,kk,'----EC HOURLY/CONTINUOUS DAT/ANC read
complete',sep=' ')
           print(status,quote=F)
           }
       }
   }


--

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to