Reading data from APIs

Jeffrey Leek
Johns Hopkins Bloomberg School of Public Health

Application programming interfaces

Creating an application

Creating an application

Creating an application

Accessing Twitter from R

myapp = oauth_app("twitter",
                   key="yourConsumerKeyHere",secret="yourConsumerSecretHere")
sig = sign_oauth1.0(myapp,
                     token = "yourTokenHere",
                      token_secret = "yourTokenSecretHere")
homeTL = GET("https://api.twitter.com/1.1/statuses/home_timeline.json", sig)

Converting the json object

json1 = content(homeTL)
json2 = jsonlite::fromJSON(toJSON(json1))
json2[1,1:4]
                      created_at           id             id_str
1 Mon Jan 13 05:18:04 +0000 2014 4.225984e+17 422598398940684288
                                                                                                                                         text
1 Now that P. Norvig's regex golf IPython notebook hit Slashdot, let's see if our traffic spike tops the previous one: http://t.co/Vc6JhZXOo8

How did I know what url to use?

In general look at the documentation

In general look at the documentation

  • httr allows GET, POST, PUT, DELETE requests if you are authorized
  • You can authenticate with a user name or a password
  • Most modern APIs use something like oauth
  • httr works well with Facebook, Google, Twitter, Githb, etc.