-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
167 lines (140 loc) · 5.03 KB
/
app.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
#loading library
library(shiny)
library(tidyverse)
library(DT)
library(shinythemes)
library(leaflet)
# library(rgdal)
library(shinydashboard)
#loading data
data <- read.csv("lgNepal.csv")
#setting lat col as numeric
data$lat <- as.numeric(data$lat)
# shapes <- readOGR("local_level.shp",GDAL1_integer64_policy = TRUE)
#renaming data
data$name_of_province <- as.character(data$name_of_province)
class(data$name_of_province)
for(i in 1:7){
names<- c("Pradesh 1","Madhesh Pradesh","Bagmati Pradesh","Gandaki Pradesh","Lumbini Pradesh","Karnali Pradesh","Sudurpashchim Pradesh")
data$name_of_province[data$name_of_province ==i] <-names[i]
}
# Define UI for application
ui <- dashboardPage(
dashboardHeader(title = "Local Government"),
dashboardSidebar(
selectInput("province","Select Province",unique(data$name_of_province)),
selectizeInput("district","Select District",choices = NULL),
selectizeInput("level","Local Level Type",choices = NULL)
),
dashboardBody(
tags$head(
tags$style(
HTML('
.content {
padding-left: 20px;
height:100vh;
}
')
)
),
fluidRow(
#theme
# theme = shinytheme("simplex"),
# Application title
titlePanel("Local Levels of Nepal नेपालको स्थानीय तह"),
# Sidebar for input filter
# sidebarLayout(
# sidebarPanel(
# selectInput("province","Select Province",unique(data$name_of_province)),
# selectizeInput("district","Select District",choices = NULL),
# selectizeInput("level","Local Level Type",choices = NULL)
# ),
# Show a table
# mainPanel(
tabsetPanel(
tabPanel("Table",
DT::DTOutput("maintable"),
fluidRow(
valueBoxOutput("total_pop"),
valueBoxOutput("total_households"),
valueBoxOutput("total_area")
),
),
tabPanel("Map", leafletOutput("map")),
)
# )
)
)
)
# Define server logic required
server <- function(input, output,session) {
#----reactive calculations
district_sel <- reactive({
data %>% filter(name_of_province == input$province)
})
observeEvent(district_sel(),{
updateSelectizeInput(session,"district", choices = district_sel()$district_name_english)
level_sel <- reactive({
data %>% filter(name_of_province == input$province,
district_name_english == input$district)
})
observeEvent(level_sel(),{
updateSelectizeInput(session,"level",choices = level_sel()$palika_type)
#logic for all selection
# if(input$name_of_province != "All"){
# data <- subset(data, name_of_province ==input$name_of_province)
# }
filter_data <- reactive({
data %>% filter(name_of_province == input$province,
district_name_english == input$district,
palika_type == input$level)
})
#-------------------------Table render--------
output$maintable <- DT::renderDT({
filter_data() %>%
select("Local Level Name" = palika_name_english, District = district_name_english,
Type = palika_type, Province = name_of_province, Website = website)
})
#--------------------------Map Render--------------
output$map <- renderLeaflet({
map_data <- filter_data() %>%
mutate(pop_info = paste("<h4><b>",palika_name_english,"</b></h4>",
"<br/>","<b>Area (in sq.km):</b>",area_sq_km,
"<br/>","<b>Total Population:</b>",total_population,
"<br/>","<b>Total Households:</b>",total_households,
"<br/>","<b>Municapal Center:</b>",municipal_center_english,
"<br/>","<b>Total Wards:</b>",total_wards
))
colors <- c("red","blue")
pal <- colorFactor(colors, map_data$total_population)
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
setView(map_data$long[1], map_data$lat[1], zoom = 9) %>%
addCircleMarkers(data = map_data, lng= ~long, lat= ~lat, radius = ~sqrt(map_data$area_sq_km),
popup = ~pop_info, color = ~pal(total_population))
})
#------total pop
#---------------------- Value box Extra Render---------------
output$total_pop <- renderValueBox({
valueBox(
paste0(sum(filter_data()$total_population)), "Total Population", icon = icon("users"),
color = "purple"
)
})
output$total_households <- renderValueBox({
valueBox(
paste0(sum(filter_data()$total_households)), "Total Households", icon = icon("home"),
color = "blue"
)
})
output$total_area <- renderValueBox({
valueBox(
paste(sum(filter_data()$area_sq_km)), "Total Area (in sq.km)", icon = icon("chart-area"),
color = "orange"
)
})
})
})
}
# Run the application
shinyApp(ui = ui, server = server)