http_error(实际上来自库httr)是一个非常简单的函数:它加载由字符(http_error.character)给出的url,检索响应(http_error.response)并最终查看响应代码(http_error.integer)。如果响应码是>=400,则函数返回TRUE,否则返回FALSE。
您的错误说明是您(或您的链中的任何函数)试图在 simpleError 对象上调用 http_error。我的猜测是您的防火墙设置阻止了该请求。因为请求被阻止,底层httr::RETRY(从oadoi_fetch 调用)返回错误而不是正确的响应对象,http_error 只看到这个错误对象并中断。
如果我在本地关闭代理(我可以通过它发出请求),我也会收到错误消息:
library(roadoi)
Sys.unsetenv(c("HTTP_PROXY", "HTTPS_PROXY"))
oadoi_fetch("10.1038/nature12373", email = "name@whatever.com")
# Error in UseMethod("http_error") :
# no applicable method for 'http_error' applied to an object of class
# "c('simpleError', 'error', 'condition')"
只要我的代理设置正确,我就会得到
Sys.setenv(HTTPS_PROXY = my_proxy, HTTP_PROXY = my_proxy)
oadoi_fetch("10.1038/nature12373", email = "name@whatever.com")
# # A tibble: 1 x 16
# doi best_oa_location oa_locations data_standard is_oa genre journal_is_oa journal_is_in_d~ journal_issns journal_name publisher title year updated non_compliant authors
# <chr> <list> <list> <int> <lgl> <chr> <lgl> <lgl> <chr> <chr> <chr> <chr> <chr> <chr> <list> <list>
# 1 10.1038~ <tibble [1 x 10]> <tibble [4 x~ 2 TRUE journa~ FALSE FALSE 0028-0836,147~ Nature Springer ~ Nanometre-s~ 2013 2019-04-0~
如果问题确实出在代理上,我会尝试以下方法,这对我的公司 Windows 计算机有所帮助,但可能取决于您本地的 IT 设置:
## get the proxy settings
system("netsh winhttp show proxy")
Sys.setenv(HTTP_PROXY = <the proxy from netsh>, HTTPS_PROXY = <the proxy from netsh>)
其实你可以很容易地重现错误:
httr::http_error(simpleError("Cannot reach the page"))
# Error in UseMethod("http_error") :
# no applicable method for 'http_error' applied to an object of class
# "c('simpleError', # 'error', 'condition')"