2014-02-18 31 views
0

我正在使用調用Web服務的算法R來查詢數據庫並返回JSON對象。來自JSON的R無法打開連接

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 

沒有爲URL的一些問題爲https?

回答

2

cannot open the connection這樣的錯誤通常意味着該文件不存在,或者您沒有權限讀取它。

你不說你是否使用rjsonRJSONIO包,但由於你包含了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的詳細信息部分。

+0

使用你的代碼,我得到的命令錯誤download.file download.file(url,json_file)的錯誤:不支持的URL方案 – perondi