forked from ZiningFan00/EDAV-final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.0.R
107 lines (86 loc) · 2.65 KB
/
v1.0.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
library(shiny)
library(leaflet)
library(dplyr)
library(ggplot2)
library(geojsonio)
library(tigris)
library(zoo)
state=states(cb=T)
# you would need to modify the implementation here, we need column having:
# id: the full name of each state
# region: the abbreviation of each state: like NY for new york
# date:
# value: the totals
geodata = function (selectedyear){
data <- read.csv("nics-firearm-background-checks copy.csv") %>%
select(1,2, 27,28)
data$month <- as.Date(as.yearmon(data$month))
year = paste0(toString(selectedyear),"-10","-01")
data_new <- data %>%
filter(data$month==year)
data_new$state <- tolower(data_new$state)
data_new <- data_new %>%
filter(data_new$state!='guam')
data_new <- data_new %>%
filter(data_new$state!='mariana islands')
data_new <- data_new %>%
filter(data_new$state!='puerto rico')
data_new <- data_new %>%
filter(data_new$state!='virgin islands')
names(data_new)[2] <- 'id'
names(data_new)[4] <-'region'
names(data_new)[3] <-'value'
return(data_new)
}
#data_new <- data_new[c(1,3,4)]
ui = fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("yearSlider",
"Please select a year",
min = 1998,
max = 2019,
value = 2019,
step = 1)
),
mainPanel(
leafletOutput("map"),
plotOutput('tim')
)
)
#textOutput("temp"),
)
#server.r
#df$location <- gsub( " " , "+" , df$location)
server = function(input, output, session) {
output$map <- renderLeaflet({
data_new = geodata(input$yearSlider)
states_merged_value <- geo_join(state, data_new, "STUSPS", "region")
bins <- c(0, 1000, 2000, 5000, 10000, 20000, 50000, 100000, Inf)
pal <- colorBin("YlOrRd", domain=states_merged_value$value,bins = bins)
leaflet()%>%
addTiles()%>%
setView( -98.483330, 38.712046, zoom = 4 ) %>%
addPolygons(
data = states_merged_value,
layerId=states_merged_value$id,
fillColor = ~pal(value),
weight = 1,
highlight=highlightOptions(weight=10, color="blue",bringToFront=TRUE)
)
})
output$temp <- renderPrint({
#input$map_shape_click
})
observeEvent(input$map_shape_click,{
output$tim <- renderPlot({
# this is where we would draw the other pictures
#plot = function(state, year)
#plot(input$map_shape_click$id,input$yearSlider)
data_new = geodata(input$yearSlider)
temp <- data_new %>% filter( data_new$id == input$map_shape_click$id)
ggplot(data = temp, aes(id, value)) + geom_point()
})
})
}
shinyApp(ui = ui, server = server)