AndrewPage
2010-Jul-21 21:33 UTC
[R] Command that is conditional upon file retrieval: is it possible?
Hi all, I'm currently working on an R program where I have to access an FTP server to download some of the data I need. However, the people who post up the files I access are at times inconsistent with regards to time posted, if they post at all, etc.... Here's some of the code I use: library(RCurl) url1 = paste("ftp://user:password at a.great.website.com/", "file", num1, ".csv", sep = "") data1 = getURL(url1) write(data1, file = paste("inMyFolder", num1, ".csv", sep = "")) Sometimes this process works perfectly, and sometimes I get an error message like this attached to "data1 = getURL(url1)": Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) : RETR response: 550 That's because that particular file isn't on the FTP server yet. Now... let's just imagine that there's another way for me to access the file elsewhere, and I can drag it into the working directory with the same name as the file I'm telling R to write immediately after finding it on the FTP server. So here's my question: is it possible to write a command that will write the file if there isn't an error message going along with "data1 getURL(url1)", but won't write the file if it can't find it???? As of right now, if I got the error message, dragged the file into the working directory and ran the program again, R will overwrite my good file with an empty one because in all cases, I'm telling it to write a file with that name that includes the information in "data1". Thanks in advance, Andrew -- View this message in context: http://r.789695.n4.nabble.com/Command-that-is-conditional-upon-file-retrieval-is-it-possible-tp2297811p2297811.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Jul-21 22:46 UTC
[R] Command that is conditional upon file retrieval: is it possible?
On Jul 21, 2010, at 5:33 PM, AndrewPage wrote:> > Hi all, > > I'm currently working on an R program where I have to access an FTP > server > to download some of the data I need. However, the people who post > up the > files I access are at times inconsistent with regards to time > posted, if > they post at all, etc.... Here's some of the code I use: > > library(RCurl) > > url1 = paste("ftp://user:password at a.great.website.com/", "file", num1, > ".csv", sep = "") > > data1 = getURL(url1) > > write(data1, file = paste("inMyFolder", num1, ".csv", sep = "")) > > > Sometimes this process works perfectly, and sometimes I get an error > message > like this attached to "data1 = getURL(url1)": > > Error in curlPerform(curl = curl, .opts = opts, .encoding > = .encoding) : > RETR response: 550 > > That's because that particular file isn't on the FTP server yet. > Now... > let's just imagine that there's another way for me to access the file > elsewhere, and I can drag it into the working directory with the > same name > as the file I'm telling R to write immediately after finding it on > the FTP > server. > > So here's my question: is it possible to write a command that will > write the > file if there isn't an error message going along with "data1 > getURL(url1)", but won't write the file if it can't find it?????try Untested in absence of workable example... Return <- try(getURL(url1), silent=TRUE) # silent keeps error from showing up on console if(grep("550", Return[1]) { # not sure about exact structure of error message # but could do nothing } else { # assign data data1 <- Return if(("550", Return[1]) { # go to the other site Return2 <- try( getURL(url2), silent =TRUE ) # and so on> As of right > now, if I got the error message, dragged the file into the working > directory > and ran the program again, R will overwrite my good file with an > empty one > because in all cases, I'm telling it to write a file with that name > that > includes the information in "data1". > > Thanks in advance,Search help archives for try examples or perhaps try-error or tryCatch which might be more specific.> Andrew > ---- David Winsemius, MD West Hartford, CT