Skip to content

Instantly share code, notes, and snippets.

@wesslen
Last active November 10, 2018 19:45
Show Gist options
  • Select an option

  • Save wesslen/25e12f4f76ad6de63dbed1ebd928785b to your computer and use it in GitHub Desktop.

Select an option

Save wesslen/25e12f4f76ad6de63dbed1ebd928785b to your computer and use it in GitHub Desktop.
solution to iviz-workshop charlotte protest shiny app: see https://ryanwesslen.shinyapps.io/protestApp/
# Project 1: Charlotte Protest
library(tidyverse); library(lubridate); library(xts); library(dygraphs); library(quanteda)
# 10% sample
protestData <- readRDS("../Protest.RData") %>%
mutate(time = paste0(substr(postedTime, 1, 13), "00:00 EDT")) %>% # convert to hourly
mutate(time = ymd_hms(time)) %>%
select(time, verb, postedTime, body)
# get daily counts
counts <- protestData %>%
count(Date = as.Date(postedTime), Type = verb) %>%
mutate(n = n * 100) # adjust for sampling
# dygraphs
counts <- protestData %>%
select(time, Type = verb) %>% # keep only columns
count(time, Type) %>% # count by terms
mutate(n = n * 100) %>% # adjust for 1% sample
spread(key = Type, value = n, fill = 0) # pivot to xts format
tweet.time <- xts(
x = counts[,-1],
order.by = counts$time,
tz = "EDT"
)
# quanteda
# create dfm
stopWords <- c("rt","http","t.co","https","amp", "t.c")
myCorpus <- corpus(protestData$body)
docvars(myCorpus, "time") <- protestData$time # used to filter
docvars(myCorpus, "verb") <- protestData$verb # used to filter
dfmAll <- corpus(myCorpus) %>%
dfm(remove_punct = TRUE,
remove_url = TRUE,
remove = c(stopwords("English"), stopWords))
rm(list=ls()) # need to delete due to Cloud 1GB limit
library(shiny)
source("app-prep.R")
#quanteda_options("threads" = 8) # set number of threads if larger dataset/cpu
# ui
ui <- fluidPage(
dygraphOutput("dygraph"),
tags$hr(), # add horizontal line
fluidRow(
column(sliderInput("num", "Number of words", min = 10, max = 100, value = 50, step = 5),
checkboxInput("retweet", "Exclude Retweets", value = FALSE),
width = 3),
column(plotOutput("network"), width = 9)
)
)
# server
server <- function(input, output) {
output$dygraph <- renderDygraph({
dygraph(tweet.time, main = "Charlotte Protest Tweet Counts (1% Sample)", group = "combine") %>%
dyLegend(show = "onmouseover") %>%
dyOptions(colors = RColorBrewer::brewer.pal(8, "Dark2"), includeZero = TRUE) %>%
dyAxis("x", drawGrid = FALSE) %>%
dyOptions(useDataTimezone = TRUE) %>%
dyRangeSelector() %>%
dyShading(from = "2016-09-20 21:00:00", to = "2016-09-21 03:00:00") %>%
dyShading(from = "2016-09-21 20:00:00", to = "2016-09-22 03:00:00")
})
output$network <- renderPlot({
if (!is.null(input$dygraph_date_window)) {
minTime <- input$dygraph_date_window[[1]]
maxTime <- input$dygraph_date_window[[2]]
} else {
minTime <- min(protestData$time)
maxTime <- max(protestData$time)
}
if (input$retweet){
dfm <- dfmAll %>%
dfm_subset(time >= minTime & time <= maxTime & verb == "post")
} else {
dfm <- dfmAll %>%
dfm_subset(time >= minTime & time <= maxTime)
}
topFeatures <- names(topfeatures(dfm, input$num))
fcm(dfm) %>%
fcm_select(topFeatures) %>% # ensure network isn't too large
textplot_network(min_freq = 0.1, edge_alpha = 0.8, edge_size = 1)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment