【问题标题】:Error in UseMethod("http_error") in roadoiroadoi 中的 UseMethod("http_error") 错误
【发布时间】:2019-04-10 15:48:50
【问题描述】:

我正在尝试 roadoi 从 R 访问 Unpaywall,但无论我尝试查询什么,我都会收到以下回复:

UseMethod("http_error") 中的错误:没有适用的方法 'http_error' 应用于类“c('simpleError', 'error', '条件')"

运行methods(http_error) 给了我这个:

[1] http_error.character* http_error.integer*   http_error.response*

这可能是因为我位于机构防火墙后面吗? (即便如此,这会是回应似乎很奇怪......)

有办法解决吗?

【问题讨论】:

    标签: r http-error


    【解决方案1】:

    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')"
    

    【讨论】:

    • 是的!谢谢!这解决了我通过工作场所系统访问互联网时遇到的多个问题。非常感谢您的详细回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2020-04-10
    • 2016-08-05
    • 2019-09-12
    相关资源
    最近更新 更多