[R] Understanding tryCatch
I've read the help and the archives on tryCatch but I'm still stuggling trying to understand how it works exactly and how I can use it to get the result I need. I have a data.frame of urls which point to 11 .zip files. Basically I use RCurl to get the list of files from a ftp and then reduce that directory dump to the 11 zip files I want to download. Its easy enough to do that in a loop over the elements in data.frame. The problem is that the site is very inconsistent. All 11 files will download, but I will get random warnings that the size of some of the files does not match what was downloaded. So at the end of the loop I may get 5 warnings about the file size not matching. These files, when tried a second or third time, will eventually download properly. What I want to do is record which files had this download warning. [[alternative HTML version deleted]] __ 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.
Re: [R] Understanding tryCatch
tryCatch will only execute it's error function on errors. If you are getting warnings, you may find it helpful to set: options(warn = 2) ?options tryCatch(warning(!), error = function(x) print(Error!)) -- Jonathan P. Daily Technician - USGS Leetown Science Center 11649 Leetown Road Kearneysville WV, 25430 (304) 724-4480 Is the room still a room when its empty? Does the room, the thing itself have purpose? Or do we, what's the word... imbue it. - Jubal Early, Firefly r-help-boun...@r-project.org wrote on 03/18/2011 03:04:41 PM: [image removed] [R] Understanding tryCatch steven mosher to: r-help 03/18/2011 03:07 PM Sent by: r-help-boun...@r-project.org I've read the help and the archives on tryCatch but I'm still stuggling trying to understand how it works exactly and how I can use it to get the result I need. I have a data.frame of urls which point to 11 .zip files. Basically I use RCurl to get the list of files from a ftp and then reduce that directory dump to the 11 zip files I want to download. Its easy enough to do that in a loop over the elements in data.frame. The problem is that the site is very inconsistent. All 11 files will download, but I will get random warnings that the size of some of the files does not match what was downloaded. So at the end of the loop I may get 5 warnings about the file size not matching. These files, when tried a second or third time, will eventually download properly. What I want to do is record which files had this download warning. [[alternative HTML version deleted]] __ 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. __ 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.
Re: [R] Understanding tryCatch
Try this: tryCatch(log(rnorm(10)), warning = function(m)deparse(m$call[[2]])) Where log(rnorm(10)) is your expr to evaluate. On Fri, Mar 18, 2011 at 4:04 PM, steven mosher mosherste...@gmail.com wrote: I've read the help and the archives on tryCatch but I'm still stuggling trying to understand how it works exactly and how I can use it to get the result I need. I have a data.frame of urls which point to 11 .zip files. Basically I use RCurl to get the list of files from a ftp and then reduce that directory dump to the 11 zip files I want to download. Its easy enough to do that in a loop over the elements in data.frame. The problem is that the site is very inconsistent. All 11 files will download, but I will get random warnings that the size of some of the files does not match what was downloaded. So at the end of the loop I may get 5 warnings about the file size not matching. These files, when tried a second or third time, will eventually download properly. What I want to do is record which files had this download warning. [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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.
Re: [R] Understanding tryCatch
That works # hydeUrls = vector of urls to file names #destList = vector of destination file names for(files in 1:length(hydeUrls)){ filesFailed[files]- FALSE filesFailed[files] - tryCatch(download.file(url=hydeUrls[files],destfile=destList[files],quiet=FALSE,mode=wb),warning=function(m) m-TRUE) print(filesFailed[files]) } And below I get the result I want which has flagged this file as improperly downloaded. note the difference between the file length and the amount downloaded. trying URL 'ftp://ftp.mnp.nl/hyde/hyde31_final/1870ad_pop.zip'ftp data connection made, file length 14394402 bytesopened URLdownloaded 2.8 Mb [1] 1 And on successful download trying URL 'ftp://ftp.mnp.nl/hyde/hyde31_final/1910ad_pop.zip'ftp data connection made, file length 14694703 bytesopened URLdownloaded 14.0 Mb [1] 0 On Fri, Mar 18, 2011 at 12:17 PM, Henrique Dallazuanna www...@gmail.comwrote: Try this: tryCatch(log(rnorm(10)), warning = function(m)deparse(m$call[[2]])) Where log(rnorm(10)) is your expr to evaluate. On Fri, Mar 18, 2011 at 4:04 PM, steven mosher mosherste...@gmail.com wrote: I've read the help and the archives on tryCatch but I'm still stuggling trying to understand how it works exactly and how I can use it to get the result I need. I have a data.frame of urls which point to 11 .zip files. Basically I use RCurl to get the list of files from a ftp and then reduce that directory dump to the 11 zip files I want to download. Its easy enough to do that in a loop over the elements in data.frame. The problem is that the site is very inconsistent. All 11 files will download, but I will get random warnings that the size of some of the files does not match what was downloaded. So at the end of the loop I may get 5 warnings about the file size not matching. These files, when tried a second or third time, will eventually download properly. What I want to do is record which files had this download warning. [[alternative HTML version deleted]] __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ 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.