Download presentation
Presentation is loading. Please wait.
Published byWilliam Morton Modified over 7 years ago
1
Dashboards – Part One “I think, aesthetically, car design is so interesting - the dashboards, the steering wheels, and the beauty of the mechanics. I don't know how any of it works, I don't want to know, but it's inspirational.” Paloma Picasso
2
Dashboard Tableau Atlas
3
Dashboards with R Integrated development environment (IDE) Rstudio
Packages shiny A package for building interactive web applications in R. Does not require knowledge of traditional web development tools such as HTML, CSS or JavaScript shinydashboard Uses shiny to create dashboards Documentation
4
Shiny
5
Dashboard in R— Class Exercise
Today we will learn how to create a shiny dashboard illustrating the results (i.e., histogram) of the sentiment analysis we performed on Twitter. We will begin with the basics and then add more information to our dashboard’s header, body, and server
6
R code – The basics
7
Header, Sidebar, Body Header Sidebar Body library(shiny)
library(shinydashboard) header <- dashboardHeader() sidebar <- dashboardSidebar() body <- dashboardBody() ui <- dashboardPage(header,sidebar,body) server <- function(input, output) {} shinyApp(ui, server)
8
Header, Sidebar, Body
9
Sentiment Analysis Also open a new R script Open your R scripts for
The sentiment analysis and The sentiment function! Also open a new R script
10
library(plyr) library(twitteR) library(stringr) # Use the information from your account to set up access to Twitter's API connection consumer_key <- '' consumer_secret <- '' access_token <- '' access_secret <- '' # Set up handshake authorization setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret) # Get the 1,500 most recent tweets mentioning in English delta_tweets <- n = 1500, lang="en", resultType = "recent") length(delta_tweets) class(delta_tweets) # Check out the first tweet delta_tweets[[1]] # Use laply method in plyr to get the text of each delta tweet delta_text <- laply(delta_tweets, function(t) t$getText()) head(delta_text)
11
R function for sentiment analysis
12
score. sentiment = function(sentences, pos. words, neg. words,
score.sentiment = function(sentences, pos.words, neg.words, .progress='none') { library(plyr) library(stringr) # split sentence into words scores = laply(sentences, function(sentence, pos.words, neg.words) { # clean up sentences with R's regex-driven global substitute, gsub(): sentence = gsub('[[:punct:]]', '', sentence) sentence = gsub('[[:cntrl:]]', '', sentence) sentence = gsub('\\d+', '', sentence) # and convert to lower case: sentence = tolower(sentence) # split into words. str_split is in the stringr package word.list = str_split(sentence, '\\s+') # sometimes a list() is one level of hierarchy too much words = unlist(word.list) # compare words to the list of positive & negative terms pos.matches = match(words, pos.words) neg.matches = match(words, neg.words) # match() returns the position of the matched term or NA # we just want a TRUE/FALSE: pos.matches = !is.na(pos.matches) neg.matches = !is.na(neg.matches) # and conveniently, TRUE/FALSE will be treated as 1/0 by sum(): score = sum(pos.matches) - sum(neg.matches) return(score) }, pos.words, neg.words, .progress=.progress ) scores.df = data.frame(score=scores, text=sentences) return(scores.df) }
13
Run the code up to the result line! (testing the algorithm)
# Remove all graphical characters delta_text <- str_replace_all(delta_text,"[^[:graph:]]", " ") # Read in Hu & Lu's 2004 opinion lexicon (positive and negative words) urlp <- " pos.words <- scan(urlp,what='character', comment.char=';') urln <- " neg.words <- scan(urln,what='character', comment.char=';') # Add a few industry-specific terms pos.words <- c(pos.words, 'upgrade') neg.words <- c(neg.words, 'wtf', 'wait', 'waiting', 'epicfail', 'mechanical') # Score sentiment for each tweet # Perform algorithm sanity check first sample <- c("You're awesome and I love you", "I hate and hate and hate. So angry. Die!", "Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.") result <- score.sentiment(sample, pos.words, neg.words)
14
Open a new script and add a title to your dashboard
library(shiny) library(shinydashboard) header <- dashboardHeader(title = on Twitter") sidebar <- dashboardSidebar() body <- dashboardBody() ui <- dashboardPage(header,sidebar,body) server <- function(input, output) {} shinyApp(ui, server)
15
Open a new script and add a title to your dashboard
16
Now add two boxes to your dashboard (body)
library(shiny) library(shinydashboard) header <- dashboardHeader(title = on Twitter") sidebar <- dashboardSidebar() body <- dashboardBody(fluidRow( box(plotOutput("plot1", height = 250)), box( title = "Controls", sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))) ui <- dashboardPage(header,sidebar,body) server <- function(input, output) {} shinyApp(ui, server)
17
Now add two boxes to your dashboard (body)
18
Finally input @delta results to your dashboard (server)
library(shiny) library(shinydashboard) header <- dashboardHeader(title = on Twitter") sidebar <- dashboardSidebar() body <- dashboardBody(fluidRow( box(plotOutput("plot1", height = 250)), box( title = "Controls", sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))) ui <- dashboardPage(header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) }) } shinyApp(ui, server)
19
Finally input @delta results to your dashboard (server)
20
Conclusion Shinydashboard is powerful!
On Wednesday we will add content to our dashboard’s sidebar. We will also do a couple more things with shiny (e.g., infobox) In the meantime, if you are curious, learn more from the documentation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.