-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
205 lines (150 loc) · 5.97 KB
/
Copy pathserver.R
File metadata and controls
205 lines (150 loc) · 5.97 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
require(shiny)
require(DT)
require(shinythemes)
require(dplyr)
require(ggvis)
load("categories.app.Rdata")
# Loading two objects here.
# categories.app: contains the categories along with the corresponding distribution of star ratings (min. # of businesses = 10)
# category.association.app: contains the frequency of a business belong to any two categories (with the min. frequency of 5)
# The data is processed by process_categories.R.
shinyServer(function(input, output, session) {
getCategoryByMeanRating <- function (rating) {
categories.app$category[order((categories.app$mean.stars.businesses - rating) %>% abs)][1]
}
# Create the drop-down of categories
output$cat <- renderUI({
selectInput(inputId = "category",
label = h4("Choose the category of businesses:"),
choices = sort(categories.app$category),
selected = "Restaurants",
multiple = F
)
})
# Display the average score.
output$heading <- renderUI({
h4(paste0("The mean Yelp rating for ",input$category," is ",
round(categories.app$mean.stars.businesses[categories.app$category==input$category],2),"."
),p(),paste0("(",
categories.app$business.count[categories.app$category==input$category],
" ",input$category, " businesses in the dataset)"
))
})
output$category <- renderUI ({h4(input$category)})
# Update the data to be plotted
histogram.data <- reactive({
# If the input controls haven't loaded yet, we're going to set the category to "Restaurants".
if (is.null(input$category)) {
category <- "Restaurants"
}
else {
category <- input$category
}
# If the slider input doesn't correspond to the selected category mean, it means it was changed. Let's react.
# We will make the category input box equal to the category whose mean score is closest to the slider score.
myData <-categories.app[categories.app$category==category,
c("businesses.1.0stars",
"businesses.1.5stars",
"businesses.2.0stars",
"businesses.2.5stars",
"businesses.3.0stars",
"businesses.3.5stars",
"businesses.4.0stars",
"businesses.4.5stars",
"businesses.5.0stars")] %>%
as.numeric %>%
cbind(seq(1,5,.5)) %>%
as.data.frame %>%
setNames(c("number","stars"))
myData$percent <- round(myData$number/ sum(myData$number) * 100)
myData$percent.label <- paste0(round(myData$number/ sum(myData$number) * 100),"%")
myData
})
# Plot the histogram
histogram.data %>%
ggvis(~stars, ~percent) %>%
layer_text(text := ~percent.label, dy := -10, dx :=-5) %>%
layer_bars(width = .3, fill = "00AAE3", stroke = "00AAE3") %>%
hide_legend(scales = c("fill","stroke")) %>%
set_options(width="100%") %>%
add_tooltip(function (data) {
paste0(data$stack_upr_,
"% of ",
input$category,
" businesses have the Yelp rating of ",
(data$xmin_ + data$xmax_) / 2,
"."
)
}) %>%
add_axis(type = "x",
title="Yelp Star Rating",
title_offset = 30,
grid = F
) %>%
add_axis(type = "y",
title="% of businesses",
title_offset = 30,
grid = F,
ticks =5
) %>%
bind_shiny("categoryvis")
# Get the list of related categories sorted by how frequently categories are associated together (limit to 10 rows)
output$relatedcategories <- renderUI ({
if (!is.null(input$category)) {
category <- input$category
}
else {
category <- "Restaurants"
}
related.categories <-
category.association.app %>%
filter(category1==category) %>%
select(category2,count) %>%
setNames(c("category1","count")) %>%
rbind(
category.association.app %>%
filter(category2==category) %>%
select(category1,count)
) %>%
arrange (desc(count)) %>%
top_n (10) %>%
select(category1) %>%
t %>%
as.vector
related.categories <- related.categories[related.categories %in% categories.app$category]
# If there's a related a category selected, that overrides the main category.
if (!is.null(input$related.category) && input$related.category !="(Select a related category)") {
updateSelectInput(session = session,
inputId = "category",
selected = input$related.category)
}
selectInput("related.category",h4(paste0("Choose from categories similar to ",
input$category,":")),
c("(Select a related category)", related.categories),
multiple = F)
})
output$avgSlider <- renderUI({
if (!is.null(input$category)) {
category <- input$category
}
else {
category <- "Restaurants"
}
sliderInput("avgrating", label = h4("Find a business category by average score:"),
min = round(min(categories.app$mean.stars.businesses),3),
max = round(max(categories.app$mean.stars.businesses),3),
step = 0.001,
ticks = F,
value = categories.app$mean.stars.businesses[categories.app$category==category],
width = "100%")
})
# Update the category from the slider
observeEvent(input$avgrating, {
if (!is.null (input$avgrating) &
input$viewType=="Mean Yelp star rating"
) {
category <- getCategoryByMeanRating(input$avgrating)
updateSelectInput(session, "category", selected = category)
}
})
})