-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.R
227 lines (188 loc) · 7.14 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
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
216
217
218
219
220
221
222
223
224
225
226
library(shiny)
library(dplyr)
library(reshape)
library(tidyr)
library(magrittr)
library(dygraphs)
#library(ggplot2)
#library(ggthemes)
library(leaflet)
library(RColorBrewer)
library(maps)
library(maptools)
library(rgdal)
shinyServer(function(input, output) {
# DATA MANIPULATION ----------------------------------------------------------
dataset <- read.csv("data/data.csv")
# Convert value from integer to numeric
dataset$Value <- as.numeric(dataset$Value)
# Convert factor to character
dataset$state_name <- as.character(dataset$state_name)
# United States Average
usa_data <- aggregate(dataset[,"Value"], list(dataset$year), mean)
colnames(usa_data) <- c("year", "Value")
usa_data$state_name <- "UNITED STATES AVERAGE"
usa_data <- usa_data[,c(3,1,2)]
# Merge US data
dataset <- rbind(dataset, usa_data)
# Convert year values to columns
data_merge_state <- spread(dataset, year, Value)
# Change column names of data to be merged
colnames(data_merge_state) <- c("state_name", paste0("Value_", colnames(data_merge_state[2:length(data_merge_state)])))
# Read shapefile
states <- readOGR("shp/cb_2014_us_state_20m.shp", layer = "cb_2014_us_state_20m", verbose = FALSE)
# Convert state names to uppercase
states@data$state_name <- toupper(states@data$NAME)
# Remove non-contiguous states
states <- subset(states, states@data$state_name != "DISTRICT OF COLUMBIA" &
states@data$state_name != "ALASKA" &
states@data$state_name != "HAWAII" &
states@data$state_name != "PUERTO RICO")
# Merge data with shapefile
states@data <- merge(states@data, data_merge_state, by = "state_name" )
# Check NAs in Value and change to zeros
states@data[is.na(states@data)] <- 0
# LET USER DOWNLOAD DATA SETS ------------------------------------------------
# Create data for download
data_user_down <- reactive({
subset(dataset, dataset$state_name %in% input$stateInputDown)
})
Index <- reactive({
data.frame(state = data_user_down()$state_name, year = data_user_down()$year, Index = (data_user_down()$Value / data_user_down()[data_user_down()$year == input$Index, "Value"]) * 100)
})
# Let user download the data
output$downloadData <- downloadHandler(
filename = function() {
paste("dataset", '.csv', sep = '')
},
content = function(file) {
write.csv(dataset, file)
}
)
# Let user download the index for state
output$downloadIndex <- downloadHandler(
filename = function() {
paste("index", '.csv', sep = '')
},
content = function(file) {
write.csv(Index(), file)
}
)
# Let user download the index for state
output$downloadState <- downloadHandler(
filename = function() {
paste("state", '.csv', sep = '')
},
content = function(file) {
write.csv(data_user_down(), file)
}
)
# CALCULATIONS ---------------------------------------------------------------
# Create data for calculations
data_user_calc1 <- reactive({
subset(dataset, dataset$state_name %in% input$stateInput1)
})
data_user_calc2 <- reactive({
subset(dataset, dataset$state_name %in% input$stateInput2)
})
# Calculate results from user input
# PART 1
# Create index
Index1 <- reactive({
data.frame(state = data_user_calc1()$state_name, year = data_user_calc1()$year, Index = (data_user_calc1()$Value / data_user_calc1()[data_user_calc1()$year == input$indexYear1, "Value"]) * 100)
})
IndexPast1 <- reactive({
subset(Index1(), year == as.numeric(input$past1))[[3]]
})
IndexCurrent1 <- reactive({
subset(Index1(), year == as.numeric(input$current1))[[3]]
})
output$IndexPast1 <- renderPrint({IndexPast1()})
output$IndexCurrent1 <- renderPrint({IndexCurrent1()})
output$estpast <- renderPrint({input$valuecurrent1 * (IndexPast1()/IndexCurrent1())})
# PART 2
# Create index
Index2 <- reactive({
data.frame(state = data_user_calc2()$state_name, year = data_user_calc2()$year, Index = (data_user_calc2()$Value / data_user_calc2()[data_user_calc2()$year == input$indexYear2, "Value"]) * 100)
})
IndexPast2 <- reactive({
subset(Index2(), year == as.numeric(input$past2))[[3]]
})
IndexCurrent2 <- reactive({
subset(Index2(), year == as.numeric(input$current2))[[3]]
})
output$IndexPast2 <- renderPrint({IndexPast2()})
output$IndexCurrent2 <- renderPrint({IndexCurrent2()})
output$estcurrent <- renderPrint({input$valuepast * (IndexCurrent2()/IndexPast2())})
# TIME SERIES PLOT -----------------------------------------------------------
# Create data for plot
# data_plot <- reactive({
# subset(dataset, dataset$state_name %in% c(input$stateInput, "UNITED STATES AVERAGE") & (year >= input$dateRange[1] & year <= input$dateRange[2]))
# })
data_plot <- reactive({
subset(dataset, dataset$state_name %in% c(input$stateInput, "UNITED STATES AVERAGE")) %>% spread(state_name, Value)
})
# Create Plot
# output$indexPlot <- renderPlot({
#
# ggplot(data_plot()) + geom_line(aes(year, Value, colour = state_name), size = 2) +
# xlab("") + ylab("Dollars per Acres") + theme_economist() +
# theme(text = element_text(size = 20),
# axis.title.y = element_text(margin = margin(0,20,0,0)),
# legend.position = "bottom",
# legend.direction = "horizontal",
# legend.text = element_text(size = 15)) +
# labs(colour = "")
#
# })
output$dygraph <- renderDygraph({
dygraph(data_plot(), main = "Farmland Values (Dollars per Acres)") %>% dyRangeSelector() %>%
dyAxis("y", label = "Dollars per Acres") %>%
dyOptions(drawPoints = TRUE, pointSize = 3, fillGraph = TRUE, fillAlpha = 0.4) %>%
dySeries(strokeWidth = 3) %>% dySeries(strokeWidth = 3) %>%
dyLegend(show = "follow", width = 380)
})
# CREATE MAP -----------------------------------------------------------------
# User choice for year
var <- reactive({
paste0("Value_", input$mapyear)
})
# User choice for color
colorpal <- reactive({
colorNumeric(input$colors, states@data[,var()])
})
# Create Blank Map
output$map <- renderLeaflet({
leaflet(states) %>%
setView(lng = -98.35, lat = 39.50, zoom = 4) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5, fillColor = "Purples",
color = ~colorQuantile("YlOrRd", states@data[,var()])( states@data[,var()])
)
})
# Add polygons
observe({
pal <- colorpal()
leafletProxy("map", data = states) %>%
clearShapes() %>%
addPolygons(
stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5, fillColor = ~pal(states@data[,var()]),
color = ~colorQuantile("YlOrRd", states@data[,var()])( states@data[,var()])
)
})
# Add legend
observe({
proxy <- leafletProxy("map", data = states)
# Remove any existing legend, and only if the legend is
# enabled, create a new one.
proxy %>% clearControls()
if (input$legend) {
pal <- colorpal()
proxy %>% addLegend(position = "bottomright", title = "Farmland Value",
pal = pal, values = ~states@data[,var()]
)
}
})
# ENDS HERE
})