次のような 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
?でエラーを直接管理することは可能です。