R 如何跟踪最有可能由服务器调用引起的错误?

R 如何跟踪最有可能由服务器调用引起的错误?,r,R,如果您已在C:/OSRM_API5/中安装了OSRM API版本5,则以下可复制示例中的以下循环会生成错误(或多或少是随机发生的。上次我运行代码是在I=17时。我们在3台不同的笔记本电脑上运行代码,并获得以下不同消息: viaroute5中的错误(locs$x[i],locs$y[i],locs$x[j],locs$y[j]): 另外未找到对象“res”:警告消息:1:In 文件(con,“r”):InternetOpenUrl失败:“Die Serververbindung” 2:价值[3L]

如果您已在
C:/OSRM_API5/
中安装了OSRM API版本5,则以下可复制示例中的以下循环会生成错误(或多或少是随机发生的。上次我运行代码是在
I=17
时。我们在3台不同的笔记本电脑上运行代码,并获得以下不同消息:

viaroute5中的错误(locs$x[i],locs$y[i],locs$x[j],locs$y[j]):
另外未找到对象“res”:警告消息:1:In 文件(con,“r”):InternetOpenUrl失败:“Die Serververbindung” 2:价值[3L]:达到 已用时间限制[cpu=1s,已用时间=1s]

选项()中出错:已达到运行时间限制。此外:警告 消息:在文件(con,“r”)中:InternetOpenUrl失败:“Die” “我不知道她是谁。”

粘贴错误(“dcall中的错误:”):已达到运行时间限制

最初我们认为,
setWinProgressBar
,但是现在错误仍然存在,尽管
setWinProgressBar
被注释掉了

有人知道发生了什么或如何追踪这个错误吗

viaroute5 <- function(lat1, lng1, lat2, lng2, instructions) {
  address <- "http://localhost:5000"
  request <- paste(address, "/route/v1/driving/",
                     lng1, ",", lat1, ";", lng2, ",", lat2,
                     "?overview=false", sep = "", NULL)

  R.utils::withTimeout({
    repeat {
      res <- try(
        route <- rjson::fromJSON(
          file = request))
      if (class(res) != "try-error") {
        if (!is.null(res)) {
          break
        } else {
          stop("???")
        }
      }
    }
  }, timeout = 1, onTimeout = "warning")

  if (res$code == "Ok") {
      return(res$routes[[1]]$duration)
    } else {
      t_guess <- 16*60
      warning("Route not found: ", paste(lat1, lng1, lat2, lng2, collapse = ", "),
              ". Time set to ", t_guess/60 , " min.")
  }
}


n <- 1e3 # ................................... if set to 10, everything is fine!
locs <- data.frame(x = c(47.424536, 47.427061),
                   y = c(9.365103, 9.365062), stringsAsFactors = FALSE)

# pb <- winProgressBar(title = "Test",
#                      label = "0% done", min=0, max=100, initial=0)
wd <- getwd()
setwd("C:/OSRM_API5")
shell(paste0("osrm-routed ", "switzerland-latest.osrm", " >nul 2>nul"), wait = F)
Sys.sleep(3) # OSRM needs time
setwd(wd)
for (i in 1:n) {
  print(i)
  # info <- paste0("done ", round(i/nrow(locs)*100, 1), "%")
  # setWinProgressBar(pb, i/nrow(locs)*100, label = info)
  for (j in 1:n) {
    viaroute5(locs$x[1], locs$y[1], locs$x[2], locs$y[2])
  }
}
shell("TaskKill /F /IM osrm-routed.exe >nul 2>nul")
# close(pb)

我认为这是由于请求的速度比OSRM处理的速度快。这导致OSRM请求队列已满,最后出现令人毛骨悚然的错误消息。有关部分解释,请参阅

使用github最新的
OSRMR
版本,您应该不会再出现这种错误了。在
v0.1.31
中,我更改了
OSRMR
包的
viaoroute
函数,通过添加一个新的可选参数
timeout
viaoroute
调用置于休眠状态默认情况下为1毫秒。
超时时间也可以调整

您可以通过以下方式从github安装最新的
OSRMR
版本:

devtools::install_github("ims-fhs/osrmr")
以下可复制的示例适用于我:

osrmr::run_server("switzerland-latest", "C:/OSRM_API5")

n <- 1e3
locs <- data.frame(x = c(47.424536, 47.427061),
                   y = c(9.365103, 9.365062), stringsAsFactors = FALSE)

for (i in 1:n) {
  print(i)
  for (j in 1:n) {
    osrmr::viaroute(locs$x[1], locs$y[1], locs$x[2], locs$y[2], instructions = FALSE, api_version = 5, localhost = TRUE, timeout = 0.001)
  }
}
osrmr::quit_server()
osrmr::run_server(“瑞士最新”、“C:/OSRM_API5”)
N
osrmr::run_server("switzerland-latest", "C:/OSRM_API5")

n <- 1e3
locs <- data.frame(x = c(47.424536, 47.427061),
                   y = c(9.365103, 9.365062), stringsAsFactors = FALSE)

for (i in 1:n) {
  print(i)
  for (j in 1:n) {
    osrmr::viaroute(locs$x[1], locs$y[1], locs$x[2], locs$y[2], instructions = FALSE, api_version = 5, localhost = TRUE, timeout = 0.001)
  }
}
osrmr::quit_server()