简体   繁体   English

在R地理编码功能中处理500个内部服务器错误

[英]Handling 500 internal server error in R geocoding function

I have written a function using the googleway package to geocode addresses, that unfortunately crashes when it encounters a 500 internal server error. 我使用googleway软件包编写了一个对地址进行地址解析的函数,不幸的是,当它遇到500个内部服务器错误时崩溃。 The function is as follows: 功能如下:

rugeocoder.fun <- function(addr){
              require(googleway)
              output <- vector("list", length=length(addr))
              for(i in 1:length(addr)){
                  output[[i]] <- google_geocode(address=addr[i], key="myapikey", language="ru", simplify=T)
                  print(i)
                }
              return(output)
              }

(Yes, I know I could accomplish this using lapply instead of a loop inside a function, but I like having the counter print to the console.) (是的,我知道我可以使用lapply而不是函数内部的循环来完成此操作,但是我喜欢将计数器打印到控制台。)

Naturally, this results in me losing all the output up to this point because of a relatively simple error. 自然地,由于相对简单的错误,这导致我失去了到目前为止的所有输出。 Is there something I can do to either have the function: a) save the output up to that point, so I can restart it at that address or b) keep trying until the server error disappears (I imagine a 500 error is likely to be temporary?). 我可以做些什么来拥有该功能:a)保存输出到该点,以便我可以在该地址重新启动它,或b)继续尝试直到服务器错误消失(我想可能是500错误)临时?)。

Per the suggestions in the comments, I was able to keep the loop from crashing when it encounters an error with tryCatch(): 根据注释中的建议,当tryCatch()遇到错误时,我能够防止循环崩溃:

rugeocoder.fun <- function(addr){
              require(googleway)
              output <- vector("list", length=length(addr))
              tryCatch({
                for(i in 1:length(addr)){
                  output[[i]] <- google_geocode(address=addr[i], key="myapikey", language="ru", simplify=T)
                  print(i)

                }},error=function(e) output[[i]] <- "Error: reattempt")
              return(output)
              }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM