Skip to content
Prev 318918 / 398521 Next

how to get xmlToList() to retry if http fails

Hi,
On Mar 6, 2013, at 4:12 PM, Waichler, Scott R wrote:

            
You can wrap it in a try function as in the following (untested).  I have made the thing stop if the second try fails, but you may want to do something more useful.  Check out tryCatch, too.

 for(i in 1:num.target.locs) {
   url <- paste(sep="/", "http://www.earthtools.org/timezone", lat[i], lon[i])
   tmp <- try(xmlToList(url))
   if (inherits(tmp, "try-error")) {
	Sys.sleep(2)
	tmp <- try(xmlToList(url))
	if (inherits(tmp, "try-error")) stop("Error fetching data")
   }
   df$time.offset[i] <- tmp$offset
   system("sleep 1")  # wait 1 second per requirements of above web service
 }  # end loop thru target locations

Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org