Define the search string for a user below in searchUserString
In [1]:
searchUserString = "@realDonaldTrump"
Define the search string for topic below in searchTopicString
In [2]:
searchTopicString = "#MachineLearning"
Define the limit of number of tweets to be searched
In [3]:
LIMIT = 200
Define all the libraries which needs to be set for operations here
In [4]:
library("twitteR")
library("DBI")
library("RSQLite")
Sys.setlocale(category = "LC_ALL", locale = "C")
Setup the twitter app key for authentication
In [5]:
setup_twitter_oauth('YOUR KEY')
Searches and collects a given number of tweets from twitter on a given topic
In [6]:
topicTweets = searchTwitter(searchTopicString,LIMIT)
Prints the top few tweets
In [7]:
head(topicTweets)
Removes duplicate tweets and prints the top few tweets
In [8]:
head(strip_retweets(topicTweets, strip_manual=TRUE, strip_mt=TRUE))
Fetches the given User's information from twitter
In [9]:
userInfo = getUser(searchUserString)
Prints the searched User's description
In [10]:
userInfo$getDescription()
Prints the number of followers the seached User have
In [11]:
userInfo$getFollowersCount()
Prints a given number of name and id of the seached User's friends
In [12]:
userInfo$getFriends(n = 5)
Prints a given number of favorites tweets of the seached User
In [13]:
userInfo$getFavorites(n = 5)
Converts the tweets to a data frame
In [14]:
topicDf = twListToDF(topicTweets)
Prints a few top tweets in data frame format
In [15]:
head(topicDf)
1st line creates a temporary sqlite db file
2nd line store and load tweets database which is backend registered
3rd line store the tweets in a table named "tweets", which is automatically provided by twitterR
In [16]:
sql_lite_file = tempfile()
register_sqlite_backend(sql_lite_file)
store_tweets_db(topicTweets)
Loads the stored tweets from table, here its is "tweets", which is automatically provided by twitterR
In [17]:
from_db_tweets = load_tweets_db()
Prints top few tweets which are retrieved from the db
In [18]:
head(from_db_tweets)
Searches tweets from given User's timeline by default only 20 are fetched
In [19]:
userTweets = userTimeline(searchUserString)
Prints 5 tweets of the timeline
In [20]:
userTweets[1:5]
Searches given number of tweets from given User's timeline
In [21]:
userTweetsLarge = userTimeline(searchUserString, n = 100)
Prints the size of the tweets collected in the previous step
In [22]:
length(userTweetsLarge)
The availableTrendLocations function will return a data.frame with a location in each row and the woeid giving that location’s WOEID
In [23]:
availTrends = availableTrendLocations()
Prints the top few trends in a data frame format
In [24]:
head(availTrends)
The closestTrendLocations function is passed a latitude and longitude and will return the same style data.frame as of availableTrendLocations.
In [25]:
closeTrends = closestTrendLocations(34.05223,-118.2437)
Prints top trending locations
In [26]:
head(closeTrends)
The getTrends function is used to pull current trend information from a given location, which is specified using a WOEID
In [27]:
trends = getTrends(2442047)
Prints top few trends info in a data frame format
In [28]:
head(trends)
Collects given number of tweets on a search topic
In [29]:
r_tweets = searchTwitter(searchTopicString, n = 300)
Extract source user agent of all the tweets fetched in the previous step
In [30]:
sources = sapply(r_tweets,function(x)x$getStatusSource())
Removed the anchored URL string if any and replaces with nothing
In [31]:
sources = gsub("</a>","",sources)
Split the elements of a character vector souces into substrings according to the matches to substring split within them.
In [32]:
sources = strsplit(sources,">")
Removes any data source which has a length greater than 1
In [33]:
sources = sapply(sources,function(x)ifelse(length(x)>1,x[2],x[1]))
Stores data in a table
In [34]:
source_table=table(sources)
Shows a pie chart based on the table generated above
In [35]:
pie(source_table[source_table>10])
In [ ]: