-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
168 lines (142 loc) · 6.06 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# libraries.
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rtweet)
library(tidyverse)
library(tidytext)
library(textdata)
library(RColorBrewer)
library(htmltools)
library(shinyjs)
# for mine eyes only.
api_key = "REDACTED"
api_secret_key = "REDACTED"
access_token = "REDACTED"
access_token_secret = "REDACTED"
# token for twitter api
me_token <- create_token(
app = "pubhealth_tweetmap",
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = access_token,
access_secret = access_token_secret)
# sentiment dictionary
afinn_dict <- read_csv("afinn_dict.csv")
# shiny ui
ui <- bootstrapPage(
useShinyjs(),
tags$style(type = "text/css",
"html, body {width:100%;height:100%}"),
leafletOutput('mapo', width = "100%", height = "100%"),
absolutePanel(top = 10, right =10,
tags$h2("Twitter Sentiment Map: Bay Area"),
span(textOutput("no_data"), style="color:red"),
textInput("keyword", "", placeholder = "Type Keyword Here.."),
actionButton("button", "Map it!"),
actionButton("button_2", "Reset Map"),
p(),
HTML(paste("This app collects geolocated tweets based on keywords<p> up to 9 characters long and evaluates their <a href = 'https://darenr.github.io/afinn/'>sentiment</a>. <p>Zoom in, click on the shapes to access a tweet's content.<p>
Author: <a href='https://www.linkedin.com/in/averysaurus/'>Avery Richards</a> <p>"
))
),
selectInput("colors", "",
rownames(subset(brewer.pal.info,
category %in% c("seq", "div")))
),
checkboxInput("legend", "", TRUE)
)
# server
server <- function(input, output, session){
output$mapo <- renderLeaflet({
leaflet() %>%
addProviderTiles("Esri.WorldGrayCanvas") %>%
setView(-122.2712, 37.8044, zoom = 10 ) %>%
addResetMapButton()
})
observeEvent(input$button_2, {
session$reload()
})
observeEvent(input$button, {
withProgress(message = "fetching tweets from API..",
value = 2/5, {
filteredData <- reactive({
# pull tweets from api
tw <- search_tweets(isolate(input$keyword),
n = 3000, token = me_token,
geocode="37.8044,-122.2712,50km",
include_rts = FALSE,
retryonratelimit = FALSE, lang = "en")
# pull geocodes
tw_geo <- lat_lng(tw, coords = c("coords_coords",
"bbox_coords", "geo_coords"))
# tweet pre-process and aggregate.
tw_geo_map <- tw_geo %>%
dplyr::select(screen_name, created_at, text, lat, lng) %>%
filter(lat != 'NA' | lng != 'NA')
tw_text <- tw_geo_map %>%
mutate(text = str_to_lower(text)) %>%
mutate(text = str_remove_all(text, "http\\S+")) %>%
mutate(text = str_remove_all(text, "@\\w+")) %>%
mutate(text = str_remove_all(text, "[[:punct:]]")) %>%
mutate(text = str_replace_all(text, "amp", "and"))
tw_text <- rowid_to_column(tw_text)
tw_act_tweets <- tw_text %>%
dplyr::select(rowid, text)
tw_text_tokens <- tw_text %>%
unnest_tokens(word, text) %>%
anti_join(stop_words)
afinn_text <- tw_text_tokens %>%
inner_join(afinn_dict) %>%
unite(coords, lat:lng, sep = ",") %>%
group_by(coords) %>%
separate(coords, c("lat", "lng"), sep = ",") %>%
mutate(lat = as.numeric(lat)) %>%
mutate(lng = as.numeric(lng)) %>%
group_by(word, lat, lng, rowid) %>%
summarise(score = mean(value)) %>%
left_join(tw_act_tweets, by = "rowid") %>%
distinct(rowid, .keep_all = T) %>%
mutate(lag = jitter(lat, factor = .005)) %>%
mutate(lng = jitter(lng, factor = .005))
})
# progress bar
for (i in filteredData()){
incProgress(1/5)
}
})
colorpal <- reactive({
colorNumeric(input$colors, domain = c(-5, 5))
})
# logic no NA keywords.
if (dim(filteredData()) == 0) {
output$no_data <-
renderText({"No data for that keyword, try again?"})
delay(3000, session$reload())
}else{
# map
observe({
pal <- colorpal()
leafletProxy("mapo", data = filteredData()) %>%
addCircleMarkers(fillColor= ~pal(score),
radius=~(score^2)*3, stroke=FALSE, weight=1,
fillOpacity = .2,
popup = ~htmlEscape(as.character(isolate(filteredData()$text))),
popupOptions = popupOptions(closeButton = F)) %>%
addLabelOnlyMarkers(label = filteredData()$score,
labelOptions = labelOptions(noHide = F, textOnly = T) )
})
observe({
proxy <- leafletProxy("mapo", data = filteredData())
if (input$legend) {
pal <- colorpal()
proxy %>% addLegend(position = "bottomleft",
title = "Sentiment Score",
pal = pal, values = ~score
)
}
})
}
})
}
shinyApp(ui, server)