January, 2018

Day 2 - Shiny Project

twitter API

  • install the following packages: httr, rtweet, tidyverse, tidytext, wordcloud shiny, leaflet
  • Setup R to use the proxy:
library(httr)
set_config(
  use_proxy(
    url="proxy.za.deloitte.com", 
    port=8080, username=rstudioapi::askForPassword(prompt = "username"),
    password=rstudioapi::askForPassword(prompt = "password")
  )
)

twitter API

## create token named "twitter_token. Replace the app, consumer_key & consumer_secret with your own.
twitter_token <- create_token(
  app = "XXXXX",
  consumer_key = "XXXXX", 
  consumer_secret = "XXXXX"
)
#Save for later use
saveRDS(twitter_token, file = "twitter_token.rds")
twitter_token <- readRDS("twitter_token.rds")
  • Test that it all works
# Get some tweets to test
user_tweets<- rtweet::get_timeline("@realDonaldTrump",n = 25)%>%
  as.tibble()

Exercise

Functions:

  1. Write the following functions:
    • A function that downloads data from twitter. It has two inputs the username and n_obs. This funtion should also transform the data to have the columns: {created, status, longitude, latitude}.
    • A function that takes the clean data and tokenises the status and removes stopword.
    • A function that takes the tokens and calculates the proportion positive & negative words
    • A function that takes the tokens and calculates the number of words for each sentiment exlcuding positive and negative.
    • A function that takes the tokens and calculates the number of times the positive and negative words appear. Converts the output to a term matrix where the columns are {positive, negative} and the rownames are the words.

Exercise

Dashboard layout:

  1. Create a shinydashboard app (ShinyTwitter) with two tabs (Dashboard & Data).
  2. Add the following inputs to the sidebar:
    • A textInput (username) with default value @realDonaldTrump.
    • A sliderInput (n_obs) with default value 50.
    • A actionButton (refresh).

Exercise

Dashboard tabs

  1. Data tab
    • Create a eventReactive (data) on the refresh button that runs the data import and cleaning function.
    • create a dataTableOutput (data_table) that displays the data when the refresh button is clicked.
    • Create a reactive (tokens) that runs the tokenise function
  2. In the dashboard tab create a two column view with width 4 & 8.

Exercise

  1. Column 1:
    • In the first column create 3 valueBoxOutput boxes (observations, positive, negative) where the widths are all 12.
    • For the observations valueBox display the number of observations in the data
    • For the positive & negative valueBox create a reactive that runs the positive and negative function and then map the correct value to the correct valueBox.
  2. Column 2:
    • Create a reactive that runs the term matrix function
    • Add a plotOutput that runs comparison.cloud(tm(),colors = c("red", "green"), max.words = 100 ). Where tm() is the term matrix reactive.
    • Create a reactive runs the sentiment function
    • add a plotlyOutput that shows a barchart of the sentiments.
  3. Add a leafletOutput that displays a marker for each tweet that has a location with a popup that show the tweet when clicked