-
Notifications
You must be signed in to change notification settings - Fork 0
/
versionApp_priceL.R
165 lines (134 loc) · 5.19 KB
/
versionApp_priceL.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
## Functioning app
## Using price/L
# Beer data from the LDB in BC and US economic data
library(shiny)
library(ggplot2)
library(dplyr)
library(data.table)
library(shinythemes)
library(DT)
############################
# beer data stored locally # ## make SQL database for these data??
############################
state <- read.csv("beer_by_state.csv", stringsAsFactors = FALSE)
ldb_clean <- read.csv("bcl-data-beer-clean-subset.csv", stringsAsFactors = FALSE)
heatmap_subset <- read.csv("ldb_heatmap_subset.csv", stringsAsFactors = FALSE)
###########
# UI code #
###########
ui <- fluidPage(theme = shinytheme("flatly"),
titlePanel("BeerApp!", windowTitle = "Beer Picker"),
"The beer industry has exploded in recent years.",
br(),
"With so many new beers on the market, it can be hard decide",
strong("what to drink"),
"and who is making",
strong("the best value beers."),
br(), br(),
"This app will help!",
br(), br(),
sidebarLayout(
sidebarPanel(strong("What kind of beer are you looking for?"),
br(), br(),
sliderInput("priceInput", "Price", min = 0, max = 30,
value = c(4, 7), pre = "$"),
checkboxGroupInput("regionInput", label = "Choose Region(s)", choices = c("ASIA", "CANADA", "EUROPE", "OTHER AMERICAS", "USA"), selected = c("CANADA", "USA")), #"AFRICA", "OCEANIA",
uiOutput("styleSelectOutput")),
mainPanel(
h3(textOutput("beerSummary")),# br(),
h3(textOutput("statSummary")), br(),
tabsetPanel(
tabPanel("Ratings for selected beers", plotOutput("beerRating")),
tabPanel("Results table", dataTableOutput("results"))
))),
br(), br(),
fluidRow(column(width = 12,
h3("Get the most bang for your buck!"),
plotOutput("heatmap"))),
br(),
fluidRow(column(width = 12, plotOutput("beerUS")))
)
###############
# Server code #
###############
server <- function(input, output) {
filter_results <- reactive({
if (is.null(input$regionInput)) {
return(NULL)
}
ldb_clean %>%
filter(Price_L >= input$priceInput[1],
Price_L <= input$priceInput[2],
Style %in% input$styleInput,
Region %in% input$regionInput
)
})
heatmap_results <- reactive({
if (is.null(input$regionInput)) {
return(NULL)
}
heatmap_subset %>%
filter(Price_L >= input$priceInput[1],
Price_L <= input$priceInput[2],
Style %in% input$styleInput,
Region %in% input$regionInput
)
})
# output$regionOutput <- renderUI({
# selectInput("regionInput", "Region of Origin",
# sort(unique(ldb_clean_subset$Region)),
# selected = "CANADA")
# })
output$styleSelectOutput <- renderUI({
selectInput("styleInput", "Beer Style",
sort(unique(ldb_clean$Style)),
multiple = TRUE,
selected = c("IPA", "PORTER", "LAGER"))
})
output$beerRating <- renderPlot({
if (is.null(filter_results())){
return()
}
ggplot(filter_results(), aes(Rating, fill = Region)) +
geom_histogram() + scale_fill_manual(values = alpha(c("#FF4081", "#5E35B1", "#2962FF", "#1DE9B6", "#AEEA00", "#F57C00", "#E040FB")))
})
output$results <- renderDataTable({
filter_results()
})
output$heatmap <- renderPlot({
if (is.null(heatmap_results())){
return()
}
ggplot(heatmap_results(), aes(x = Region, y = Style)) +
geom_tile(aes(fill = Value_metric_norm), color = "white") +
scale_fill_gradient2(low = "yellow", mid = "orange", high = "blue", midpoint = 0.5) +
#scale_fill_gradient2(low = "white", mid = "orange", high = "blue", midpoint = 0.5) +
labs(fill = "Value") +
theme(legend.title = element_text(face = "bold", size = 14)) +
theme(panel.background = element_rect(fill = "white"),
axis.text.x = element_text(size = 10, face = "bold"),
plot.title = element_text(size = 20, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold"))
})
# output$results <- renderDataTable({
# filter_results()
# })
#
output$beerSummary <- renderText({
numBeers <- nrow(filter_results())
if (is.null(numBeers)) {
numBeers <- 0
}
paste0(numBeers, " beers meet your criteria")
})
output$statSummary <- renderText({
statSummary <- round(mean(filter_results()$Rating), 2)
if (is.na(statSummary)) {
statSummary <- 0
}
paste0("The average rating for your selection is ", statSummary, " out of 5")
})
}
# Run the app
shinyApp(ui = ui, server = server)
# color = c("#FF4081", "#5E35B1", "#2962FF", "#1DE9B6", "#AEEA00", "#F57C00", "#E040FB")