This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from gadenbuie/rsconf_tweets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
202 lines (185 loc) · 7.29 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
library(shiny)
library(rtweet)
library(dplyr)
library(stringr)
library(purrr)
requireNamespace('httr', quietly = TRUE)
requireNamespace('shinythemes', quietly = TRUE)
requireNamespace('DT', quietly = TRUE)
library(glue)
source("init.R")
get_tweet_blockquote <- function(screen_name, status_id) {
bq <- httr::GET(glue("https://publish.twitter.com/oembed?url=https://twitter.com/{screen_name}/status/{status_id}?omit_script=true"))
if (bq$status_code >= 400)
'<blockquote style="font-size: 90%">Sorry, unable to get tweet ¯\\_(ツ)_/¯</blockquote>'
else {
httr::parsed_content(bq)$html
}
}
close_to_sd <- function(lat, lng) {
sandiego <- rtweet::lookup_coords("Sand Diego, CA")$point %>% as_radian
# from http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates
delta_lat <- 50/3958.76
delta_lng <- asin(sin(as_radian(lat))/cos(delta_lat))
lat <- as_radian(lat)
lng <- as_radian(lng)
lat >= sandiego['lat'] - delta_lat &
lat <= sandiego['lat'] + delta_lat &
lng >= sandiego['lng'] - delta_lng &
lng <= sandiego['lng'] + delta_lng
}
as_radian <- function(degree) degree * pi / 180
ui <- fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
tags$script(src="twitter.js")),
titlePanel("rstudio::conf_twitter()"),
theme = shinythemes::shinytheme('yeti'),
column(
width = 4,
wellPanel(
selectInput('view', 'Tweet Group', c('Popular', 'Tips', "Talks", "Pictures", "All")),
uiOutput('help_text'),
uiOutput('filters')
),
wellPanel(class = 'tweetbox', htmlOutput('tweet')),
tags$div(class = 'colophon',
tagList(
tags$p(
"Made with", HTML("❤️"), "+", HTML("\u2615\uFE0F"), "by",
tags$a(href = 'https://twitter.com/grrrck/', '@grrrck'),
'with', HTML("💪"), 'from',
HTML(paste(
tags$a(href = 'http://rtweet.info/', 'rtweet'),
tags$a(href = 'https://www.rstudio.com/', 'RStudio'),
tags$a(href = 'https://shiny.rstudio.com/', 'Shiny'),
tags$a(href = 'https://www.tidyverse.org/', 'tidyverse'),
tags$a(href = 'http://code.databio.org/simpleCache/', 'simpleCache'),
sep = ', '
))),
tags$p(
HTML("💾"), tags$a(href = 'https://github.com/gadenbuie/rsconf_tweets', 'View on GitHub')
, "or", downloadLink('download_tweets', "Download Tweets")
),
tags$p(
"Final update: 2018-02-07 11:48:02 EST"
)
)
)
),
column(8, DT::dataTableOutput('tweets'))
)
server <- function(input, output) {
output$help_text <- renderUI({
req(input$view)
switch(
input$view,
'Popular' = helpText(HTML("💯"), "Most popular (retweets + favs) first"),
'Tips' = helpText(HTML("💡"), "Original or quote tweets that mention a tip"),
'Talks' = helpText(HTML("🎓"), "Original or quote tweets that mention \"slides\", \"presentations\", etc."),
'Pictures' = helpText(HTML("📸"), "Tweets that come with a picture"),
'All' = helpText(HTML("🤗"), "All the tweets"),
NULL
)
})
tweets <- reactive({
x <- switch(
input$view,
'Popular' = rsconf_tweets %>%
arrange(desc(retweet_count + favorite_count),
-map_int(mentions_screen_name, length)),
'Tips' = rsconf_tweets %>% filter(relates_tip, !is_retweet),
'Talks' = rsconf_tweets %>% filter(relates_session, !is_retweet),
'Pictures' = rsconf_tweets %>% filter(!is_retweet, !is.na(media_url)),
rsconf_tweets
)
if (input$view %in% c('All', 'Popular')) {
if (length(input$filter_binary)) {
for (filter_binary in input$filter_binary) {
x <- switch(
filter_binary,
'Not Retweet' = filter(x, !is_retweet),
'Not Quote' = filter(x, !is_quote),
'Has Media' = filter(x, !is.na(media_url)),
'Has Link' = filter(x, !is.na(urls_url)),
'Has Github Link' = filter(x, str_detect(urls_url, "github")),
"Retweeted" = filter(x, retweet_count > 0),
"Favorited" = filter(x, favorite_count > 0),
"Probably There IRL" = x %>% lat_lng() %>%
filter(
str_detect(tolower(place_full_name), "san diego") |
close_to_sd(lat, lng) |
user_id %in% users_there_IRL
)
)
}
}
if (length(input$filter_hashtag)) {
x <- filter(x, !is.null(hashtags))
for (filter_hashtag in input$filter_hashtag) {
x <- filter(x, map_lgl(hashtags, function(h) filter_hashtag %in% h))
}
}
}
x
})
hashtags_related <- reactive({
req(input$view %in% c('All', 'Popular'))
if (is.null(input$filter_hashtag) || input$filter_hashtag == '') return(top_10_hashtags)
limit_to_tags <- related_hashtags %>%
filter(tag %in% input$filter_hashtag) %>%
pull(related) %>%
unique()
top_10_hashtags %>%
filter(`Top 10 Hashtags` %in% c(limit_to_tags, input$filter_hashtag)) %>%
pull(`Top 10 Hashtags`)
})
output$filters <- renderUI({
selected_hashtags <- isolate(input$filter_hashtag)
selected_binary <- isolate(input$filter_binary)
if (input$view %in% c('All', 'Popular')) {
tagList(
checkboxGroupInput('filter_binary', 'Tweet Filters',
choices = c("Not Retweet", "Not Quote", "Has Media", "Has Link", "Has Github Link", "Retweeted", "Favorited", "Probably There IRL"),
selected = selected_binary,
inline = TRUE),
selectizeInput('filter_hashtag', 'Hashtags', choices = c("", hashtags_related()), selected = selected_hashtags,
multiple = TRUE, options = list(plugins = list('remove_button')), width = "100%")
)
}
})
output$tweets <- DT::renderDataTable({
tweets() %>%
select(created_at, screen_name, text, retweet_count, favorite_count, mentions_screen_name) %>%
mutate(created_at = strftime(created_at, '%F %T', tz = 'US/Pacific'),
mentions_screen_name = map_chr(mentions_screen_name, paste, collapse = ', '),
mentions_screen_name = ifelse(mentions_screen_name == 'NA', '', mentions_screen_name))
},
selection = 'single',
rownames = FALSE,
colnames = c("Timestamp", "User", "Tweet", "RT", "Fav", "Mentioned"),
filter = 'top',
options = list(lengthMenu = c(5, 10, 25, 50, 100), pageLength = 5)
)
output$tweet <- renderText({
if (!is.null(input$tweets_rows_selected)) {
tweets() %>%
slice(input$tweets_rows_selected) %>%
mutate(
html = suppressWarnings(get_tweet_blockquote(screen_name, status_id))
) %>%
pull(html)
} else {
HTML('<blockquote style="font-size: 90%">Choose a tweet from the table...</blockquote>')
}
})
output$download_tweets <- downloadHandler(
filename = function() {
paste("rstudio-conf-tweets-", Sys.Date(), ".RDS", sep="")
},
content = function(file) {
saveRDS(rsconf_tweets, file)
}
)
}
shinyApp(ui = ui, server = server)