Thursday, November 19, 2015

Getting Data via twitteR

Source

R package twitteR (by Jeff Gentry)

The simplest and quickest way to get data from twitter is by using the
functions of the R package twitteR (check twitteR vignette for more info)
Some (but not all) of the things you can get with twitteR
public tweets
trending topics
tweets containing a given hashtag
tweets containing a given word / term
tweets from a given user

Let's see different examplesStart: First we need to load the package twitteR
(remember to install it first!)
# load package twitteR
library(twitteR)

Case 1: Getting recent public tweets with publicTimeline
(limited to 20 tweets by default)
# get recent public tweets
public_tweets = publicTimeline()
> head(public_tweets, 3)
[[1]]
[1] "Na_rinhaa: tava dormindo ate agora man"
[[2]]
[1] "thesillykid: Photo: http://t.co/E0FGXQKJ"
[[3]]
[1] "LilBuffy2Bhad: Because jr its the only way I can get too the weed. Y u ask so many questions about it.?"
Case 2: Getting trending topics with the function getTrends

(there's a limit of past days to retrieve information from; roughly 10-14 days)
# get trending topics of the day
# (returns top 20 trending topics per hour for given data)
today_trends = getTrends(period = "daily", date=Sys.Date())


> # 5 trends from last hour
> today_trends[1:5]
[[1]]
[1] "La #MarchaYoSoy132"
[[2]]
[1] "#ThingsThatMakeMeHulkMad"
[[3]]
[1] "#Sopio"
[[4]]
[1] "#LawsonAdamFollows6"
[[5]]
[1] "Of Mice and Men"


Case 3: Getting trending topics from yesterday
# get trending topics from yesterday
yest_trends = getTrends(period = "daily", date=Sys.Date() - 1)


> # 5 trends from 24 hours ago
> yest_trends[1:5]
[[1]]
[1] "#AlwaysShadyNeverBieber"
[[2]]
[1] "#LawsonAdamFollows7"
[[3]]
[1] "Zux"
[[4]]
[1] "#CongratsHarry"
[[5]]
[1] "Zayn and Lux"


Case 4: Getting trends of the week
# get trending topics of the week
# (returns top 30 trending topics for each day)
week_trends = getTrends(period = "weekly")

> week_trends[1:5]
[[1]]
[1] "La #MarchaYoSoy132"
[[2]]
[1] "51 Million Facebook Monsters"
[[3]]
[1] "Chelsea <3"
[[4]]
[1] "Goodmorning <3"
[[5]]
[1] "#HowToMakePeopleMad"



Case 5: Getting tweets containing a given hashtag with searchTwitter
You can also search a maximum of 'n' tweets in a given language (given by an ISO 639-1 code)
For instance, get tweets with "#datamining"
# Search tweets with '#datamining'
dmhash_tweets = searchTwitter("#datamining")

# search a maximum of 20 tweets in french
dm20fr_tweets = searchTwitter("#datamining", lang="fr", n=20)

# search a maximum of 10 tweets in german
dm10de_tweets = searchTwitter("#datamining", lang="de", n=10)


> dm20fr_tweets
[[1]]
[1] "Alice_Begue: Le #Datamining pour les nuls http://t.co/jvYJKUMv"
[[2]]
[1] "marionlapeyroux: Le #Time2market, c'est cette semaine, rdv &amp; #conférences avec les spécialistes du #marketing #digital, #datamining.. http://t.co/R3os0Yro"
[[3]]
[1] "zeggar: RT @DZSSUG: Un Club de rugby utilise #BusinessIntelligence et #DataMining pour prévenir les blessures de ses joueurs. http://t.co/tBsksJWl"
> dm10de_tweets
[[1]]
[1] "WJastrinsky: Welche Trendfarben tragen die Menschen - in diesem Moment - in Paris? Color Forecast von pimkie. #datamining http://t.co/6vxTlqjx"


Case 6: Getting tweets containing a given word/term with searchTwitter
For instance, get tweets with "data mining"

# search tweets containing "data mining"
dm1_tweets = searchTwitter("data mining")

# search tweets between two dates (you will have to change the dates!)
dm2_tweets = searchTwitter("data mining", since='2012-05-12', until='2012-05-17')

# search tweets around a given radius of 5 miles of latitude/longitude
dm3_tweets = searchTwitter("data mining", geocode="37.857253,-122.270558,5mi")


Case 7: Get tweets from a given user with userTimeline
For instance, look for @weather's tweets

# tweets from @weather
wea_tweets = userTimeline("weather")


> wea_tweets[1:4]
[[1]]
[1] "weather: RT @science: Dramatic trailer for PBS Dust Bowl documentary http://t.co/i3wuASV9"
[[2]]
[1] "weather: Video: Twin waterspouts filmed near Grand Isle, LA. http://t.co/w99JkPUO"
[[3]]
[1] "weather: Prince Charles reads the Scottish weather forecast http://t.co/eUh8UdVo"
[[4]]
[1] "weather: Tornado damages 890 homes in Japan, kills teenager http://t.co/0TDZNYPs (via @newsalert)"



No comments:

Post a Comment