Making a twitter dashboard with R

One my primary uses of twitter favorites is to bookmark interesting content. The trouble is they easily accumulate and the twitter search interface does not make it easy to re-find that one tweet from 3 months ago. In the following blog post I show how you can use the rtweet and DT packages to make yourself a searchable twitter favorites dashboard in the style of https://jsta.github.io/twitter-likes_dashboard/.

Load dependencies

First, lets make sure that some R packages are installed and loaded:

library(rtweet)
library(magrittr)
library(dplyr)
library(DT)

Retrieving an archive

Next, lets go through the steps of retrieving some tweet data from the Twitter API via the rtweet package.

  1. Setup rtweet

You should enable R access to the twitter API via an access token. See the instructions at http://rtweet.info/articles/auth.html.

  1. Pull archive

After you’ve setup an access token, you can pull some twitter favorites. In order to make things more readable lets select only a few columns and sort the data by descending date. For the purposes of this blog post I only show one pull. You may want to add a few lines of code here to compare the current pull against a previously pulled archive and merge the two.

user_name <- "__jsta"
my_likes  <- get_favorites(user_name, n = 100) %>% 
  select("created_at", "screen_name", "text", "urls_expanded_url") %>%
  arrange(desc(created_at))
  1. Datestamp and URL formatting

Now you may want to deal with the fact that the datestamp includes the time (this will take up unnecessary space in our dashboard). Although there are many ways to fix the datestamp, I’ll use old-school non-lubridate formatting:

my_likes$created_at <- strptime(as.POSIXct(my_likes$created_at), 
                                format = "%Y-%m-%d")
my_likes$created_at <- format(my_likes$created_at, "%Y-%m-%d")

Another bit of formatting you many want is to make the content URLs clickable. In their current form they will simply appear as text. I used this stackoverflow tip by BigDataScientist to make the URLs clickable:

createLink <- function(x) {
  if(is.na(x)){
    return("")
  }else{
    sprintf(paste0('<a href="', URLdecode(x),'" target="_blank">', 
                   substr(x, 1, 25) ,'</a>'))
  }
}

my_likes$urls_expanded_url <- lapply(my_likes$urls_expanded_url, 
                                     function(x) sapply(x, createLink))

Rendering dashboard

Finally, we can use the datatable package to create our searchable dashboard. The first list of options is passed directly to the DataTables javascript library wrapped by the DT package. Here I also set some other options to supress row numbering, fill as much of the browser window as possible, and clean up the column name labels. You can read more about datatable adjustments at https://rstudio.github.io/DT/. Next, I ran the dashboard through some additional formatting lines to make it more readable by decreasing the font size and making the tweet text column larger.

my_table <- datatable(my_likes, 
                      options = list(scrollX = TRUE, autoWidth = TRUE,
                                     columnDefs = list(list(
                                           width = '70%', 
                                           targets = c(2)))), 
                      rownames = FALSE,
                      fillContainer = TRUE,
                      width = "100%", 
                      colnames = c("Date", "Handle", "Text", "URL"))

my_table <- formatStyle(my_table, columns = 1:4, fontSize = '70%')
my_table <- formatStyle(my_table, columns = 3, width = '500px')

Thats it. We’ve made a searchable twitter favorite dashboard! If you are using RStudio you can view it in the Viewer pane by evaluating the dashboard name in the console:

my_table

You can also save the dashboard for local viewing in a browser with saveWidget (the following assumes you use firefox):

temp_file <- tempfile()
saveWidget(my_table, temp_file)

system(paste("firefox", temp_file))