2

次のような Facebook Graph API にユーザーの詳細を要求しています。

require(RJSONIO)
response <- RJSONIO::fromJSON("http://graph.facebook.com/?ids=Jack")
print(response)
# $Jack
# id       first_name           gender        last_name           locale 
# "534213341"           "Jack"           "male"      "Lindamood"          "en_US" 
# name         username 
# "Jack Lindamood"   

すべて良い。

しかし、API から処理するエラーが発生することがあります。このエラー応答など(誰もこのユーザー名を取得しないことを願っています...)

{
   "error": {
      "message": "(#803) Some of the aliases you requested do not exist: this.username.does.not.exist.because.i.made.it.up",
      "type": "OAuthException",
      "code": 803
   }
}

RJSONIO で解析しようとすると

RJSONIO::fromJSON("http://graph.facebook.com /?ids=this.username.does.not.exist.because.i.made.it.up")

私は得る

Error in file(con, "r") : cannot open the connection

しかし、最初にjsonを解析するとRCurl、rjson形式のエラーメッセージが表示されます

require(RCurl)
json <- getURL("http://graph.facebook.com/?ids=this.username.does.not.exist.because.i.made.it.up")
RJSONIO::fromJSON(json)
$error
$error$message
[1] "(#803) Some of the aliases you requested do not exist: this.username.does.not.exist.because.i.made.it.up"

$error$type
[1] "OAuthException"

$error$code
[1] 803

RJSONIO?でエラーを直接管理することは可能です。

4

1 に答える 1

4

できるよ

result <- try(RJSONIO::fromJSON("http://graph.facebook.com/?ids=this.username.does.not.exist.because.i.made.it.up"), 
              silent=TRUE)`

class(result)処理する前に確認してください(try-error投稿したエラーが発生した場合になります)。

httrパッケージ (パッケージの最新のフォークを直接使用するRSJSONIO- jsonlite) とパッケージを比較して使用することもできRJSONIOます。

library(httr)

content(GET("http://graph.facebook.com/?ids=Jack"), as="parsed")
content(GET("http://graph.facebook.com/?ids=this.username.does.not.exist.because.i.made.it.up"),
        as="parsed")
## $error
## $error$message
## [1] "(#803) Some of the aliases you requested do not exist: this.username.does.not.exist.because.i.made.it.up"
## 
## $error$type
## [1] "OAuthException"
## 
## $error$code
## [1] 803
于 2015-02-01T02:38:55.350 に答える