knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(twitterapiR)
library(dplyr)
library(ggplot2)

Descritpion

The twitterapiR package is an R wrapper for Twitter API, which provides programmatic access to the popular social media platform Twitter. The twitterapiR package provides access to each of the endpoints of Twitter API, and it contains several functions, for each of which the following documentation offers usage examples.

Function Usage

The set_bearer() function sets key and secret as environment variables.

set_bearer('Tds7CCMzPsJbRZH7aikpKiObN', 'lPH7pIQIS1pVinxh48xvXWgqzGa9gre4Utb9tIZ2W1U0nSCgrz')

The searchTweets() function can be used to find recent tweets using characters including hashtags. In the example below the searchTweets() function returns all the users who recently used #ubc.

# Use `searchTweets` functions to search for the top 100 recent tweets using #ubc
ubc_tweets <- searchTweets("#ubc", resultType = "recent", count = 100)

# Select those with start=5 and end time=13
created_time <- substr(ubc_tweets$created_time, start = 5, stop = 13)

# Format the time to month, day, and hour
created_time <- strptime(created_time,format='%b %d %H')

# Save the results in a DataFrame
created_time_df <- as.data.frame(created_time)

created_time_df <-  created_time_df %>% group_by(created_time) %>% summarize(num_tweets = n())

# Plot the results
ggplot(created_time_df, aes(x = created_time, y = num_tweets)) + 
    geom_area(fill="firebrick") +
    geom_line(color="black", size=1) +
    geom_point(color="black", size=3) +
    theme_bw()+
    labs(x = "Created Time", y= "Number of Tweets")+
    ggtitle("The Latest 100 Trending Tweets Containing the Hashtag #ubc")
    theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())

The followersCount() function can be used to quickly get the number of followers a user has by using the user's screen name. In the plot below we used this function in a for loop to get the followers of the following state leaders as of 2022:

# Use followersCount to find the number of followers for each country leader
list = c("POTUS", "JustinTrudeau", "BorisJohnson", "EmmanuelMacron", "moonriver365")
followers = c()
for (i in list){
    followers[i]=followersCount(screen_name =i)$followers_count
}

# Save the results in a DataFrame
followers <- as.data.frame(followers)

# Add name and no_of_followers as column names
followers["name"] <- c("Joe Biden", "Justin Trudeau", "Boris Johnson", "Emmanuel Macron", "Moon Jae-in" )
followers["no_of_followers"] <- followers$followers

# Remove redundant column
followers$followers = NULL

If you want to visualize the comparison, you can plot the results as follows.

# pre-set the bw theme
theme_set(theme_bw())  

# Plot total number of followers per president
g <- ggplot(followers, aes(name, no_of_followers))

# Use a barplot
g + geom_col() + 
  labs(subtitle="example plot", 
       y="no of followers", 
       x="name", 
       title="Popular President?", 
       )

The user_friends() function can be used to retrieve the number of friends a user has by using the user's screen name. By default the maximum number of returned friends is 195.

Similar to the above plot, the plot below uses the user_friends() function in a for loop to get the friends of the following state leaders as of 2022:

# Use user_friends to find the number of friends for each country leader
list = c("POTUS", "JustinTrudeau", "BorisJohnson", "EmmanuelMacron", "moonriver365")
friends = c()
for (i in list){
    friends[i]=nrow(user_friends(screen_name=i))
}

# Store the results in a DataFrame
friends <- as.data.frame(friends)

# Add name and no_of_friends as column names
friends["name"] <- c("Joe Biden", "Justin Trudeau", "Boris Johnson", "Emmanuel Macron", "Moon Jae-in" )

friends["no_of_friends"] <- friends$friends

# Remove redundant column
friends$friends = NULL

If you want to visualize the comparison, you can plot the results as follows.

# pre-set the bw theme
theme_set(theme_bw())

# Plot the results
g <- ggplot(friends, aes(name, no_of_friends))

# Add barplot
g + geom_col() + 
  labs(subtitle="example plot", 
       y="no of friends", 
       x="name", 
       title="President with the Most friends?", 
       )


tangaot/twitterapiR documentation built on March 24, 2022, 12:40 p.m.