-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
215 lines (194 loc) · 9.14 KB
/
app.R
File metadata and controls
215 lines (194 loc) · 9.14 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
library(shiny)
library(shinydashboard)
library(tidyverse)
library(data.table)
library(ggplot2)
library(grid)
library(scales)
data = as.data.table(fread("MediumProcessed.csv") %>% select(-c(V1)))
timeData = as.data.table(fread("timeData.csv"))
body = dashboardBody(
fluidRow(
tabBox(
title = "Clap Data Visuals",
id = "vis1",
width = 15,
tabPanel("Clap Distribution", plotOutput("distPlotClaps")),#dataTableOutput("texted")),
tabPanel("Claps VS Reading TIme", plotOutput("ClapsToRT")),
tabPanel("Claps Over Time", h6("Note: Different from other tabs, here claps are included in monthly average if any tag is present, not all"),plotOutput("ClapsOverTime"))
)
),
fluidRow(
uiOutput("summary")
)
)
# Define UI for application
ui = dashboardPage(
dashboardHeader(title = "Medium Article Data Exploration"),
# Sidebar
dashboardSidebar(
collapsed = FALSE,
checkboxGroupInput("tags",
"Tags of Article:",
c("Art" = "Tag_art",
"Artificial Intelligence" = "Tag_artificial_intelligence",
"Bitcoin" = "Tag_bitcoin",
"Blockchain" = "Tag_blockchain",
"Business" = "Tag_business",
"Computer Science" = "Tag_computer_science",
"Creativity" = "Tag_creativity",
"Cryptocurrency" = "Tag_cryptocurrency",
"Culture" = "Tag_culture",
"Design" = "Tag_design",
"Education" = "Tag_education",
"Entrepreneurship" = "Tag_entrepreneurship",
"Ethereum" = "Tag_ethereum",
"Fiction" = "Tag_fiction",
"Food" = "Tag_food",
"Health" = "Tag_health",
"Humor" = "Tag_humor",
"Inspiration" = "Tag_inspiration",
"Investing" = "Tag_investing",
"Javascript" = "Tag_javascript",
"Leadership" = "Tag_leadership",
"Life" = "Tag_life",
"Life Lessons" = "Tag_life_lessons",
"Love" = "Tag_love",
"Machine Learning" = "Tag_machine_learning",
"Motivation" = "Tag_motivation",
"Movies" = "Tag_movies",
"Music" = "Tag_music",
"News" = "Tag_news",
"Personal Development" = "Tag_personal_development",
"Photography" = "Tag_photography",
"Poetry" = "Tag_poetry",
"Politics" = "Tag_politics",
"Productivity" = "Tag_productivity",
"Programming" = "Tag_programming",
"Relationships" = "Tag_relationships",
"Self Improvement" = "Tag_self_improvement",
"Social Media" = "Tag_social_media",
"Sports" = "Tag_sports",
"Startup" = "Tag_startup",
"Tech" = "Tag_tech",
"Technology" = "Tag_technology",
"Travel" = "Tag_travel",
"UX" = "Tag_ux",
"Web Development" = "Tag_web_development",
"Writing" = "Tag_writing"))),
body
)
# Define server
server = function(input, output) {
options(shiny.error = browser)
options(rsconnect.max.bundle.size=2147483648)
dataSub = reactive({
newData = data
if(is.null(input$tags)){
newData = data
}else{
newData=filter_at(newData, vars(input$tags), all_vars(.==1))
}
})
correlationC = reactive({
round(cor(dataSub()$Claps, dataSub()$Reading_Time , method = "pearson", use = "na.or.complete" ) ^ 2,2)
})
xLimVal = reactive({
max(50, quantile(dataSub()$Claps, 0.95))
})
distData = reactive({
binData = dataSub() %>%
select(Claps) %>%
mutate(bin = Claps %/% 20) %>%
group_by(bin) %>%
summarise(binCount = n())
})
yLimVal = reactive({
max(distData()$binCount, na.rm = TRUE)
})
timeDataSub = reactive({
newTimeData = timeData
if (is.null(input$tags)){
newTimeData = timeData
}else{
newTimeData = newTimeData %>%
filter(tag %in% input$tags)
}
})
output$summary = renderUI({
if (nrow(dataSub()) ==0){
infoBox("There exist no articles with all of the selectd tags", width = 10)
}else{
val = summary(dataSub())
infoBox("Summary Stats of Claps for Articles Only with Selected Tags",
value = HTML(paste(gsub(pattern ="\\.:", replacement = "artile :",val[,5][1:6]), br())),
width = 10)
}
})
output$distPlotClaps = renderPlot({
plotdata = as.data.frame(dataSub())
if (nrow(plotdata) == 0){
ggplot(data.frame()) + geom_point() + annotation_custom(textGrob("There are no articles in the data with all of these tags\n Unselect tags to keep exploring!"), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
}else{
ggplot(distData(), aes(x =bin*20, y = binCount)) +
geom_col() +
stat_function(fun = dnorm, args = list(mean = mean(dataSub()$Claps), sd = sd(dataSub()$Claps)))+
scale_x_continuous(name = "Number of Claps",
breaks = seq(0, 1500, 50),
limits = c(-10, xLimVal()))+
scale_y_continuous(name = "Count",
limits = c(0, yLimVal())) +
ggtitle("Distribution of Claps Given to Articles with Only Selected Tags")+
theme(plot.title = element_text(hjust = 0.5))
}
})
output$ClapsToRT = renderPlot({
plotdata = as.data.frame(dataSub())
if (nrow(plotdata) == 0){
ggplot(data.frame()) + geom_point() + annotation_custom(textGrob("There are no articles in the data with all of these tags\n Unselect tags to keep exploring!"), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
}else{
ggplot(plotdata, aes(x = Reading_Time, y = Claps)) +
geom_point() +
scale_x_continuous(name = "Estimated Reading Time (Minutes)") +
scale_y_continuous(name = "Claps") +
annotate(x= Inf, y=Inf, hjust= 1, vjust = 1,
label=paste("Pearson Correlation Coefficient= ", correlationC()),
geom="text", size=5) +
ggtitle("Claps Versus Estimated Reading Time of Articles with Only Selected Tags")+
theme(plot.title = element_text(hjust = 0.5))
}
})
output$ClapsOverTime = renderPlot({
if (is.null(input$tags)){
pooledMonthMeans = timeDataSub()%>%
group_by(Date) %>%
summarize(meanClaps = mean(Claps))
ggplot(pooledMonthMeans, aes(x = as.Date(Date), y = meanClaps)) +
geom_point() +
scale_x_date(date_breaks = "1 month",
labels = date_format("%m-%Y"),
name = "Date")+
scale_y_continuous(name = "Average Number Claps")+
stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1) +
ggtitle("Pooled Averages for Each Month of Average Claps for All Selectable Tags")+
geom_text(x= Inf, y=Inf, hjust= 1,
vjust = 1,
label="Pick a tag from the sidebar to start seeing individual means!"
, size=5) +
theme(plot.title = element_text(hjust = 0.5))
}else{
ggplot(data = timeDataSub(), aes(x = as.Date(Date), y = Claps, col = tag)) +
geom_point() +
scale_x_date(date_breaks = "1 month",
labels = date_format("%m-%Y"),
name = "Date")+
scale_y_continuous(name = "Average Number Claps")+
geom_smooth(method = "lm") +
ggtitle("Average Number of Claps Each Month for each Selected Tag")+
theme(plot.title = element_text(hjust = 0.5))+
guides(color=guide_legend("Tag"))
}
})
}
# Run the application
shinyApp(ui = ui, server = server)