Assembling an RShiny application

Our RShiny application will be able to do the following:

  • Search Twitter for a keyword/hashtag
  • Display the top 100 results
  • Show the sentiment for the top 100 tweets

Let us first load all the necessary libraries and authenticate into our Twitter account:

library(shiny)
library(twitteR, quietly = TRUE)
library(stringr)
library(sentimentr, quietly = TRUE)
library(dplyr, quietly = TRUE)


consumer.key <- ""
consumer.secret <- ""
access.token <- ""
token.secret <- ""

setup_twitter_oauth(consumer.key, consumer.secret, access.token, token.secret)

Fill in your consumer.keys, secret, access.token, and token.secret.

Let us build the user interface part:

ui <- fluidPage(
navbarPage("TweetSenti",
tabPanel("Search Tweets"
, textInput("search","search",value="#bladerunner")
, dataTableOutput("results")
),
tabPanel("Tag Sentiment"
,dataTableOutput("sentiment"))
)
)

We have a navigation panel at the top. The first navigation panel has a textbox in which we can enter our search string. A default value is provided, so when you open the application, you will have the search results available for that string. We have a data table output following the textbox. This is where we display the tweet results.

In the second navigation panel, we have the data table output, where we want to show the tweets and their sentiments.

Now let us write the server side of the code:

server <- function(input, output) {

tweet.df <- reactive({
tweet.results <- searchTwitter(input$search, n=100,lang = "en")
tweet.df <- twListToDF(tweet.results)
# Remove retweets
tweet.df <- tweet.df[tweet.df$isRetweet == FALSE, ]
# Cleanup tweets
tweet.df <- data.frame(tweet.df['text'])
tweet.df$text =str_replace_all(tweet.df$text,"[\.\,\;]+", " ")
tweet.df$text =str_replace_all(tweet.df$text,"http\w+", "")
tweet.df$text =str_replace_all(tweet.df$text,"@\w+", " ")
tweet.df$text =str_replace_all(tweet.df$text,"[[:punct:]]", " ")
tweet.df$text =str_replace_all(tweet.df$text,"[[:digit:]]", " ")
tweet.df$text =str_replace_all(tweet.df$text,"^ ", " ")
tweet.df$text =str_replace_all(tweet.df$text," $", " ")
tweet.df$text =str_replace_all(tweet.df$text,"[<].*[>]", " ")
# Get sentiment
sentiment.score <- sentiment(tweet.df$text)
sentiment.score <- sentiment.score %>% group_by(element_id) %>% summarise(sentiment = mean(sentiment))
tweet.df$polarity <- sentiment.score$sentiment
tweet.df$sentiment <- ifelse(tweet.df$polarity <0, "Negative","Positive")

return(tweet.df)

})

output$results <- renderDataTable({
tweet.df()['text']
})

output$sentiment <- renderDataTable({
tweet.df()

})

}

tweet.df is declared as a reactive expression. It will change if the text in our search box changes. Here, we search Twitter for the search string provided. Capture the returned tweets, clean them up, calculate their polarity scores, and keep them ready.

It's simple from now on. output$results return a data frame. It takes the data frame from our tweet.df reactive expression. This one only returns the text column.

output$sentiment behaves the same way as output$results, except that it returns the whole data frame.

Reactive expressions are expressions that can read reactive values and call other reactive expressions. Whenever reactive value changes, any reactive expressions that depended on it are marked as invalidated and will automatically re-execute if necessary. - RShiny web page.

Finally, let us declare the complete application:

shinyApp(ui = ui, server = server)

When we run this application, we see the following screen:

You can see the cleaned up tweets for #BladeRunner.

We can change the search string and have the new results shown:

You can see that we have changed the query to R data science.

Let us look at the next tab:

In the preceding screenshot, we can see the sentiment for the tweets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset