我正在使用一种算法 R,它调用一个 Web 服务,该服务对数据库进行查询并返回一个 JSON 对象。
url <- paste ('https://example.com?id=1'')
document <- fromJSON (content = url, method = 'C')
在我的机器上,当我连接到服务器并运行时,该算法通常效果不佳,出现以下错误:
Error in file(con, "r") : cannot open the connection
Calls: fromJSON -> fromJSON -> I -> structure -> unique
Execution halted
https 的 url 有问题吗?
错误如cannot open the connection
通常意味着该文件不存在,或者您没有读取该文件的权限。
你没有说你是否正在使用rjson
or RJSONIO
包,但因为你包括了method
争论,我猜是前者。rjson::fromJSON
将其第一个参数视为 JSON 字符串。您应该使用file
相反。
document <- fromJSON(file = url)
作为最佳实践,在解析来自互联网的内容时,您应该首先下载它;然后解析它(分两个单独的步骤)。这样,当出现问题并引发错误时,您就不会耗尽带宽来重新下载它。
尝试将您的代码分成:
json_file <- "path/to/save/it/to/the_data.json"
download.file(url, json_file)
document <- fromJSON(file = json_file)
注意download.file
不支持https
默认情况下。在Windows下,你可以这样做setInternet2()
使用Internet Explorer的连接DLL,然后它就可以工作了。请参阅详细信息部分?download.file
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)