kanimbla
kanimbla

Reputation: 890

R - Twitter - fromJSON - get list of tweets

I would like to retrieve a list of tweets from Twitter for a given hashtag using package RJSONIO in R. I think I am pretty close to the solution, but I seem to miss one step.

My code reads as follows (in this example, I use #NBA as a hashtag):

library(httr)
library(RJSONIO)

# 1. Find OAuth settings for twitter:
#    https://dev.twitter.com/docs/auth/oauth
oauth_endpoints("twitter")

#    Replace key and secret below
myapp <- oauth_app("twitter",
                   key = "XXXXXXXXXXXXXXX",
                   secret = "YYYYYYYYYYYYYYYYY"
)

# 3. Get OAuth credentials
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)

# 4. Use API
req=GET("https://api.twitter.com/1.1/search/tweets.json?q=%23NBA&src=typd",
        config(token = twitter_token))
req <- content(req, as = "text")
response=fromJSON(req)

How can I get the list of tweets from object 'response'?

Eventually, I would like to get something like:

searchTwitter("#NBA", n=5000, lang="en")

Thanks a lot in advance!

Upvotes: 2

Views: 783

Answers (1)

pnulty
pnulty

Reputation: 36

The response object should be a list of length two: statuses and metadata. So, for example, to get the text of the first tweet, try:

response$statuses[[1]]$text

However, there are a couple of R packages designed to make just this kind of thing easier: Try streamR for the streaming API, and twitteR for the REST API. The latter has a searchTwitter function exactly as you describe.

Upvotes: 2

Related Questions