-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
118 lines (100 loc) · 3.8 KB
/
server.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
library(shiny)
library(tidyverse)
library(shinyjs)
library(rsconnect)
# replace state thing with this later: https://shiny.rstudio.com/gallery/custom-input-control.html
server <- function(input, output) {
# Get beer data
getData <- reactive({
userFile <- read.csv(req(input$BeerFile$datapath), header=T)
tidyFile <- userFile %>% filter(!is.na(IBU) & !is.na(ABV) & !is.na(Style)) %>% filter(userFile$state %in% input$states)
})
# Downloadable csv of beers dataset
output$downloadData <- downloadHandler(
filename = "beer.csv",
content = function(file) {
serverFile <- read.csv("./data/beer.csv", header=T)
serverFile <- serverFile %>% filter(!is.na(IBU) & !is.na(ABV) & !is.na(Style))
write.csv(serverFile, file, row.names = FALSE)
}
)
# Render IBU Plots
output$IBUplot <- renderPlot({
# req(input$RadioPlotType != "scatterplot")
# show("IBUplot")
# hide("ScatterPlot")
x <- getData() %>% select(IBU)
x <- as.numeric(x$IBU)
IBUbins <- seq(min(x), max(x), length.out = input$IBUbins + 1)
if (input$RadioPlotType == "boxplot") {
boxplot(x, col = "#75AADB", border = "black",
xlab = "Beer Distribution",
ylab = "IBU Rating",
main = "Boxplot of International Bitterness Units (IBU)")
} else {
hist(x, breaks = IBUbins, col = "#75AADB", border = "white",
xlab = "IBU Rating",
ylab = "Number of Beers",
main = "Histogram of International Bitterness Units (IBU)")
}
})
# Render ABV Plots
output$ABVplot <- renderPlot({
# req(input$RadioPlotType != "scatterplot")
# show("ABVplot")
# hide("ScatterPlot")
x <- getData()
x <- as.numeric(x$ABV)
ABVbins <- seq(min(x), max(x), length.out = input$ABVbins + 1)
if (input$RadioPlotType == "boxplot") {
boxplot(x, col = "#75AADB", border = "black",
xlab = "Beer Distribution",
ylab = "ABV Rating",
main = "Boxplot of Alcohol by Volume (ABV)")
} else {
hist(x, breaks = ABVbins, col = "#75AADB", border = "white",
xlab = "ABV Rating",
ylab = "Number of Beers",
main = "Histogram of Alcohol by Volume (ABV)")
}
})
# Render Scatter Plot
output$ScatterPlot <- renderPlot({
# req(input$RadioPlotType == "scatterplot")
# hide("ABVplot")
# hide("IBUplot")
# show("ScatterPlot")
plot <- ggplot(getData(), aes(x=IBU, y= ABV)) +
geom_point() +
theme_classic() +
theme(axis.text.x=element_text(size=rel(1.0))) +
labs(x="IBU",y="ABV") +
ggtitle("Correlation between IBU and ABV")
if(input$line == T){
plot + geom_smooth(method=lm)
}
else {
plot
}
})
# Calculate Top 10 Cities with the most Breweries
output$CityPlot <- renderPlot({
TopCities <- getData() %>% mutate(City = paste0(City, ', ', state)) %>% group_by(City) %>%
summarise(Breweries = n()) %>% arrange(desc(Breweries)) %>% top_n(10)
ggplot(data=TopCities, mapping=aes(x=City, y=Breweries)) + geom_col(color="white",fill="#75AADB") +
ggtitle("Top 10 Cities with Most Breweries") +
theme_classic() +
theme(axis.text.x = element_text(angle = 60, vjust=1, hjust=1))
})
# Calculate Top 10 Breweries with the most original beers
output$BrewPlot <- renderPlot({
TopBreweries <- getData() %>% mutate(Brewery = paste0(Brewery,'\n', City, ', ', state)) %>%
group_by(Brewery) %>% summarise(Beers = n()) %>% arrange(desc(Beers)) %>% top_n(10)
ggplot(data=TopBreweries, mapping=aes(x=Brewery, y=Beers)) + geom_col(color="white",fill="#75AADB") +
theme_classic() +
ggtitle("Top 10 Breweries with Most Original Beers") +
theme(axis.text.x = element_text(angle = 60, vjust=1, hjust=1))
})
}
#deployApp()
#shinyApp(ui, server)