-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.R
200 lines (179 loc) · 5.97 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
library(shiny)
library(dplyr)
library(purrr)
library(readr)
library(stringr)
library(magrittr)
library(reactable)
datasets <- read_tsv(file.path("data", "datasets.tsv"))
tweets <- read_tsv(file.path("data", "tweets.tsv")) %>%
filter(dataset_id != "x")
users <- tweets %>%
pull(screen_name) %>%
unique() %>%
sort()
ui <- fluidPage(
tags$head(
tags$script(src = "https://platform.twitter.com/widgets.js", charset = "utf-8"),
tags$link(href = "https://fonts.googleapis.com/css?family=Roboto+Mono", rel = "stylesheet"),
tags$style(HTML('
* {
font-family: Roboto Mono;
font-size: 100%;
}
#sidebar {
background-color: #fff;
border: 0px;
}
.rt-th {
display: none;
}
.rt-noData {
display: none;
}
.rt-pagination-nav {
float: left;
width: 100%;
}
'))
),
sidebarLayout(
sidebarPanel(
id = "sidebar",
tags$h1("tidytuesday.rocks"),
tags$br(),
HTML(paste0(
"<p><a href='https://github.com/rfordatascience/tidytuesday'>Tidy Tuesday</a> is a weekly data visualization challenge for R users hosted on Twitter with #TidyTuesday.</p>",
"<p>As of January 21, 2021, there are</p>",
"<ul><li> 7,117 tweets from</li><li>1,508 users across</li><li>147 datasets!</li></ul>",
"<p>Use the options below to filter the tweets by dataset or user and sort them by likes, retweets, and when they were posted. Happy plotting! \U0001F57A</p>",
"<p>(Made by <a href='https://twitter.com/nsgrantham'>@nsgrantham</a>. Source code <a href='https://github.com/nsgrantham/tidytuesdayrocks'>on GitHub</a>.)</p>"
)),
tags$br(),
tabsetPanel(
id = "selected_tab",
type = "tabs",
selected = "dataset",
tabPanel("Dataset tweets", value = "dataset",
tags$br(),
selectInput("dataset_name", NULL, rev(datasets$dataset_name), selected = rev(datasets$dataset_name)[1]),
selectInput("dataset_sort_by", NULL, c("Sorted by most likes", "Sorted by most retweets", "Sorted by most recent"), selected = "Sorted by most likes"),
uiOutput("dataset_links")
),
tabPanel("User tweets", value = "user",
tags$br(),
selectizeInput("user_name", NULL, users, selected = sample(users, 1)),
selectInput("user_sort_by", NULL, c("Sorted by most likes", "Sorted by most retweets", "Sorted by most recent"), selected = "Sorted by most likes"),
uiOutput("user_links")
)
),
width = 5
),
mainPanel(
conditionalPanel(
condition = "input.selected_tab == 'dataset'",
reactableOutput("embedded_dataset_tweets_table", width = "600px")
),
conditionalPanel(
condition = "input.selected_tab == 'user'",
reactableOutput("embedded_user_tweets_table", width = "600px")
),
width = 7
)
)
)
make_links <- function(urls, text, icon = NULL) {
if (is.na(urls)) return("")
split_urls <- unlist(str_split(urls, ","))
if (length(split_urls) > 1) {
text <- paste(text, 1:length(split_urls))
}
names(split_urls) <- text
links <- imap(split_urls, ~ tags$a(.y, href = .x, target = "_blank"))
c(icon, links)
}
embed_tweet <- function(tweet_url) {
paste0(
"<blockquote class='twitter-tweet'>",
"<p lang='en' dir='ltr'>",
"Loading...",
"<a href='", tweet_url, "'></a>",
"</p>",
"</blockquote>"
)
}
make_embedded_tweets_table <- function(tweets) {
reactable(
tweets,
showPageInfo = FALSE,
defaultPageSize = 1,
sortable = FALSE,
width = 600,
height = 700,
columns = list(status_url = colDef(cell = embed_tweet, html = TRUE)),
rowClass = JS("function() {twttr.widgets.load()}")
)
}
server <- function(input, output, session) {
chosen_dataset <- reactive({
datasets %>%
filter(dataset_name == input$dataset_name) %>%
transpose() %>%
extract2(1)
})
dataset_tweets <- reactive({
tweets %>%
filter(dataset_id == chosen_dataset()$dataset_id) %>%
select(screen_name, status_url, created_at, favorite_count, retweet_count)
})
sorted_dataset_tweets <- reactive({
switch(
input$dataset_sort_by,
"Sorted by most recent" = arrange(dataset_tweets(), desc(created_at)),
"Sorted by most likes" = arrange(dataset_tweets(), desc(favorite_count)),
"Sorted by most retweets" = arrange(dataset_tweets(), desc(retweet_count))
)
})
output$dataset_links <- renderUI({
tagList(
make_links(chosen_dataset()$dataset_files, "Data Download", "\U0001F4BE"), # floppy disk
make_links(chosen_dataset()$dataset_articles, "Article", "\U0001F5DE"), # rolled up newspaper
make_links(chosen_dataset()$dataset_sources, "Source", "\U0001F4CD") # red pin
)
})
output$embedded_dataset_tweets_table <- renderReactable(
sorted_dataset_tweets() %>%
select(status_url) %>%
make_embedded_tweets_table()
)
observeEvent(
sorted_dataset_tweets(),
updateReactable("embedded_dataset_tweets_table", page = 1)
)
user_tweets <- reactive(
tweets %>%
filter(screen_name == input$user_name) %>%
select(status_url, created_at, favorite_count, retweet_count)
)
sorted_user_tweets <- reactive(
switch(
input$user_sort_by,
"Sorted by most recent" = arrange(user_tweets(), desc(created_at)),
"Sorted by most likes" = arrange(user_tweets(), desc(favorite_count)),
"Sorted by most retweets" = arrange(user_tweets(), desc(retweet_count))
)
)
output$user_links <- renderUI(
tagList(make_links(paste0("https://twitter.com/", input$user_name), "Twitter", "\U0001F4AC")) # speech bubble
)
output$embedded_user_tweets_table <- renderReactable(
sorted_user_tweets() %>%
select(status_url) %>%
make_embedded_tweets_table()
)
observeEvent(
sorted_user_tweets(),
updateReactable("embedded_user_tweets_table", page = 1)
)
}
shinyApp(ui, server)