knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "figures/README-",
  out.width = "100%"
)
library(twitterapiR)
library(dplyr)
library(ggplot2)

Description

R-CMD-check

This project is an API wrapper for Twitter in R. This package provides user friendly functions for the users to query information from Twitter.

Current list of functions included in the wrapper:

Installation

if (!require("remotes")) {
  install.packages("remotes")
}
remotes::install_github("tangaot/twitterapiR")

Authentication

You will need to follow these instructions to continue. We are accessing Twitter programatically. twitterapiR uses the httr package under the hood to manage this.
The first step is to create a Twitter application for yourself. To do so, go to the Developer Platform's page and log in.

Follow the instruction and fill in some basic information. After your project is created, you can generate your consumer API and Secret key.

In your R session, you will want to do the following using the appropriate values from the web page:

                            set_bearer("API key", "API secret")

This will authenticate via httr. We recommend looking at the Token man page of this package for more information regarding how to manage the authentication and caching processes.

Getting Started

This document is intended to describe the usage of each function and to show examples of each function. To explore the source code or report some issues, we recommend going to our GitHub. Before exploring our functionality, please make sure you complete the following steps.

Function Usage

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

set_bearer('YOUR-KEY-HERE', 'YOUR-SECRET-HERE')

The get_bearer function is a helper function that can be used to generate the bearer key by using API key and API secret. It is embedded in the functions searchTweets, user_friends, and followersCount. The bearer key allows the users to query information from Twitter.

get_bearer()

The searchTweets() function can be used to search for related tweets that match a specified string.

Parameters:

The function will return a DataFrame containing the following columns:

In the example below searchTweets() function returns all the users who recently used #ubc.

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

head(ubc_tweets)
# 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())

created_time_df
# 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.

Parameters:

In the plot below we use 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 follower 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
    Sys.sleep(1)
}

# 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

followers

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="number 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.

Parameters:

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))
    Sys.sleep(1)
}

# 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

friends

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="number of friends", 
       x="name", 
       title="President with the Most friends?", 
       )

Code of Conduct

Please note that the twitterapiR project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



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