Do you want to track a conference hashtag — or any hashtag — on Twitter? This bonus Do More With R tutorial shows you step by step how to create an interactive Shiny web application to search, sort, and filter tweets with a specific hashtag (or any keyword).

If you don’t want to go through the whole Twitter R tutorial, you can download the Shiny and R app code plus this article in R Markdown and PDF format as a free Insider extra. 

Original R code to search tweets

Do More With R episode #41 demonstrated how to search for tweets with the rtweet package and create a sortable, filterable table with reactable. This first code block from that article searches for tweets and formats results to display in a table.

# Configure variables: number of tweets to download and hashtag search query
num_tweets_to_download <- 200
hashtag_to_search <- "#rstudioconf"
# Make sure to install any packages listed below that you don't already have on your system:
# Code to actually search for tweets
tweet_df <- search_tweets(hashtag_to_search, n = num_tweets_to_download, include_rts = FALSE)
# select a few desired columns and add a clickable link to tweet text for table data
tweet_table_data <- tweet_df %>%
  select(user_id, status_id, created_at, screen_name, text, favorite_count, retweet_count, urls_expanded_url) %>%
    Tweet = glue::glue("{text} <a href='{screen_name}/status/{status_id}'>>> </a>")
  select(DateTime = created_at, User = screen_name, Tweet, Likes = favorite_count, RTs = retweet_count, URLs = urls_expanded_url)

The next code block is a function to make URLs in the tweet URL column clickable. As I mentioned in the main episode, this is a bit more complicated than creating a link to a tweet because there can be more than one URL per tweet. There’s probably a more elegant way to generate clickable URLs, but the function below works.

make_url_html <- function(url) {
  if(length(url) < 2) {
    if(! {
      as.character(glue("<a title = {url} target = '_new' href = '{url}'>{url}</a>") )
    } else {
  } else {
    paste0(purrr::map_chr(url, ~ paste0("<a title = "", .x, "" target = '_new' href = "", .x, "">", .x, "</a>", collapse = ", ")), collapse = ", ")
tweet_table_data$URLs <- purrr::map_chr(tweet_table_data$URLs, make_url_html)

The third and final code block creates an interactive reactable table. Most of the code in this block tweaks the default styling and table behavior.

          filterable = TRUE, searchable = TRUE, bordered = TRUE, striped = TRUE, highlight = TRUE,
          showSortable = TRUE, defaultSortOrder = "desc", defaultPageSize = 25, showPageSizeOptions = TRUE, pageSizeOptions = c(25, 50, 75, 100, 200),
          columns = list(
            DateTime = colDef(defaultSortOrder = "asc"),
            User = colDef(defaultSortOrder = "asc"),
            Tweet = colDef(html = TRUE, minWidth = 190, resizable = TRUE),
            Likes = colDef(filterable = FALSE, format = colFormat(separators = TRUE)),
            RTs = colDef(filterable =  FALSE, format = colFormat(separators = TRUE)),
            URLs = colDef(html = TRUE)

Create a basic Shiny app to search tweets

Now let’s turn this data table into a local interactive Shiny app with even more filtering possibilities.

Source link