error handling - Use tryCatch with a while loop in R -


i trying implement trycatch while loop in r, have been running issues. have tried implement number of proposed solutions (which revolve around loops), no success.

essentially querying api r , looping through number of relevant parameters (longitudes , latitudes precise). reason why need trycatch block because url request fail, in turn stops script running. want able ignore error, increase looping counter 1 , continue extraction.

the while loop have set (fyi - length refers length of dataframe being looped over):

i <- 1 while(i <= length) { x_cord <- geocode_area$x[i] y_cord <- geocode_area$y[i] target <- geturl(x_cord,y_cord) dat <- fromjson(target) geocode_area$block[i] <- dat$result$geographies$`2010 census blocks`[[1]]$block print(paste(i/length*100,"% completed",sep="")) print(dat$result$geographies$`2010 census blocks`[[1]]$block) <- + 1  } 

the geturl() function defined as:

geturl <- function(x,y) { root <- "http://geocoding.geo.census.gov/geocoder/geographies/coordinates?" u <- paste0(root,"x=", x,"&y=", y,"&benchmark=4&vintage=4&format=json") return(urlencode(u)) } 

the input data.frame while loop looks (note have thrown in character strings simulate error test trycatch working):

          x                 y          block 1 -122.425891675136 37.7745985956747     0 2  -122.42436302145 37.8004143219856     0 3 -122.426995326766 37.8008726327692     0 4 -122.438737622757 37.7715411720578     0 5               abc            zsads     0 

i have tried number of , other solutions, results not seem working properly. can help?

thanks!

jack

as general note - code kind of weird. recommend either for loop, or possibly better, function stuff. can make loop work.

# minimal working version library(rjsonio) options(stringsasfactors = false)  # create data frame example data geocode_area <- data.frame(x = c("-122.425891675136","-122.42436302145","-122.426995326766","-122.438737622757","abc"),                            y = c("37.7745985956747","37.8004143219856","37.8008726327692","37.7715411720578","zsads"),                            block = c(0,0,0,0,0))  # old function, unchanged geturl <- function(x,y) {      root <- "http://geocoding.geo.census.gov/geocoder/geographies/coordinates?"     u <- paste0(root,"x=", x,"&y=", y,"&benchmark=4&vintage=4&format=json")     return(urlencode(u)) }  # getting length parameter length <- nrow(geocode_area) <- 1 while(i <= length) {      x_cord <- geocode_area$x[i]     y_cord <- geocode_area$y[i]     target <- geturl(x_cord,y_cord)      # here new code     # try(), silent = true, suppresses outputs stderr     # in principle, dangerous - better approach strip out offending data before invoking     # errors are, after all, there reason     dat <- try(fromjson(target),silent = true)      # now, conditionally complete next steps     # if class of dat not try-error, perform normal operations     # otherwise, bypass , print note console     if(class(dat) != "try-error") {          geocode_area$block[i] <- dat$result$geographies$`2010 census blocks`[[1]]$block         print(paste(i/length*100,"% completed",sep=""))         print(dat$result$geographies$`2010 census blocks`[[1]]$block)     } else if (class(dat) == "try-error") {print("error encountered, bypassing")} <- + 1 } 

edited add: obviously, uses try() instead of trycatch(). however, poster ended using try() , may represent different way it, thought i'd leave up.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -