-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
225 lines (179 loc) · 7.59 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
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
library(shiny)
library(shinyjs)
library(shinyauthr)
library(shinythemes)
library(shinycssloaders)
library(leaflet)
library(mapview)
options(shiny.jquery.version = 1)
# source dataframe that holds usernames, passwords and permissions
source("Rsource/credentials.R")
# load the switch code
source("Rsource/SwitchButton.R")
# render mapview doesn't work; this function works
myRenderMapview <- function(expr, env = parent.frame(), quoted = FALSE){
if (!quoted)
expr = substitute(mapview:::mapview2leaflet(expr))
htmlwidgets::shinyRenderWidget(expr, leafletOutput, env,
quoted = TRUE)
}
# read layer
aus_SA2 <- setNames(sf::read_sf("shiny-resources/aus_SA2.gpkg"), c("SA2_CODE21", "SA2_NAME21", "Region", "State", "Incursion risk", "Climatic suitability", "Establishment likelihood", "Tourist pathway", "Returning resident pathway", "Visiting friends & family pathway", "Sea cargo pathway", "Natural dispersal pathway", "Budwood pathway", "geom"))
# roads <- sf::read_sf("shiny-resources/roads.gpkg")
# layers
all_layers <- c("Incursion risk", "Climatic suitability", "Establishment likelihood", "Tourist pathway", "Returning resident pathway", "Visiting friends & family pathway", "Sea cargo pathway", "Natural dispersal pathway", "Budwood pathway")
data_full <-
aus_SA2 %>%
tibble::as_tibble() %>%
dplyr::select(-geom)
org_counts <-
data_full %>%
dplyr::count(State) %>%
dplyr::pull(n)
names(org_counts) <- sort(unique(data_full$State))
ui <- shinyUI(
navbarPage("Citrus Biosecurity",
id = "mainpage",
theme = shinytheme("flatly"),
tabPanel(
id = 'login_tab',
title = 'Login',
loginUI(
id = "login",
title = "Please log in",
user_title = "User Name",
pass_title = "Password",
login_title = "Log in",
error_message = "Invalid username or password!"
)
),
# Panel 1 -----------------------------------------------------------------
tabPanel(
"Risk maps",
splitLayout(
selectizeInput(inputId = "select_map",
label = "Select layer",
options = list(dropdownParent = 'body',
create = 0),
choices = all_layers),
sliderInput(inputId = "select_quant_map",
label = "Select cutoff (quantile)",
min = 0,
max = 1,
value = 0,
round = -2)
),
# map prediction maps
uiOutput("maps") %>%
withSpinner(color = "#2C3E50", type = 6)# "#0dc5c1"
),
# Panel 2 -----------------------------------------------------------------
tabPanel(
"Top risk",
includeCSS("www/button.css"),
splitLayout(
selectizeInput(inputId = "select_filt",
label = "Select filter",
options = list(dropdownParent = 'body',
create = 0),
choices = all_layers),
switchButton(inputId = "state",
label = "By state?",
value = FALSE,
col = "GB",
type = "TF"),
sliderInput(inputId = "select_quant_dt",
label = "Select cutoff (quantile)",
min = 0,
max = 1,
value = 0.9,
round = -2)
),
# filtered table
tableOutput("table_ui") %>%
withSpinner(color = "#2C3E50", type = 6), # "#0dc5c1"
useShinyjs()
)
)
)
server <- function(input, output, session){
# log-in ------------------------------------------------------------------
# call login module supplying data frame,
# user and password cols and reactive trigger
credentials <- shinyauthr::loginServer(
id = "login",
data = user_base,
user_col = user,
pwd_col = password,
log_out = reactive(logout_init())
)
# call the logout module with reactive trigger to hide/show
logout_init <- shinyauthr::logoutServer(id = "logout",
active = reactive(credentials()$user_auth))
# open the app after authentication
observe({
if(credentials()$user_auth) {
shinyjs::removeClass(selector = "nav", class = "collapse")
removeTab(inputId = 'mainpage',
target = 'Login', session = getDefaultReactiveDomain())
} else {
shinyjs::addClass(selector = "nav", class = "collapse")
}
})
user_info <- reactive({credentials()$info})
map <- reactive({
aus_SA2_filt <- aus_SA2 %>%
dplyr::filter(.data[[input$select_map]] >= quantile(.data[[input$select_map]], .env$input$select_quant_map))
ncols <- max(round(length(unique(aus_SA2_filt %>% dplyr::pull(input$select_map)))/10, 0), 2)
aus_SA2_filt %>%
mapview(map.types = "Esri.WorldStreetMap",
layer.name = input$select_map,
zcol = input$select_map,
label = aus_SA2_filt$SA2_NAME21,
col.regions = viridis::inferno(n = ncols))
})
# the maps
output$maps <- renderUI({
myRenderMapview(map())
})
tab <- reactive({
if(input$state){
data <- data_full %>%
dplyr::group_by(State) %>%
dplyr::filter(.data[[input$select_filt]] >= quantile(.data[[input$select_filt]], .env$input$select_quant_dt)) %>%
dplyr::arrange(State, dplyr::desc(.data[[input$select_filt]]))
} else {
data <- data_full %>%
dplyr::filter(.data[[input$select_filt]] >= quantile(.data[[input$select_filt]], .env$input$select_quant_dt)) %>%
dplyr::arrange(dplyr::desc(.data[[input$select_filt]]))
}
counts <- data %>%
dplyr::count(State) %>%
dplyr::pull(n)
names(counts) <- sort(unique(data$State))
filt_states <- names(which(!(org_counts - counts == 0)))
data %>%
dplyr::filter(State %in% filt_states)
})
output$table_ui <- renderUI({
if(nrow(tab()) == 0)
return("All SA2 above cutoff - select different value")
DT::DTOutput("table")
})
output$table <- DT::renderDT({tab()},
filter = "top",
extensions = "Buttons",
options = list(
dom = 'l<"sep">Bfrtip',
buttons = list(
'csv',
'excel',
list(extend = "pdf",
pageSize = "A4",
orientation = "landscape")
),
pageLength = 20,
lengthMenu = c(10,20,50,100)
), server = FALSE)
}
shinyApp(ui, server)