Skip to content

Commit

Permalink
add legends
Browse files Browse the repository at this point in the history
  • Loading branch information
RWParsons committed Jun 5, 2024
1 parent be8bdd8 commit f91cdc6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/app/logic/scales_and_palettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ ra_text_choices <- ra_scale_to_text(0:4)
#' @export
pal_mins <- function(x) withr$with_package("dplyr", palette_list$palNum(x))

#' @export
bins_mins <- palette_list$bins_mins

#' @export
pal_hours <- function(x) withr$with_package("dplyr", palette_list$palNum_hours(x))

Expand Down
43 changes: 43 additions & 0 deletions app/app/mapping/update_legend.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
box::use(
leaflegend,
leaflet,
shiny,
stringr,
)

box::use(
app / logic / scales_and_palettes,
)


#' @export
update_legend <- function(proxy_map, selected_layer, layer_type) {
proxy_map |> leaflet$clearControls()

if (stringr$str_detect(selected_layer, "index")) {
proxy_map |> add_itraqi_legend()
} else if (layer_type != "none") {
proxy_map |> add_continuous_legend()
}
}

add_itraqi_legend <- \(map) {
leaflegend$addLegendFactor(
map = map,
opacity = 1,
position = "topright",
pal = scales_and_palettes$pal_index,
values = scales_and_palettes$iTRAQI_levels,
title = "iTRAQI index"
)
}

add_continuous_legend <- \(map) {
leaflegend$addLegendNumeric(
map = map,
position = "topright",
pal = scales_and_palettes$pal_hours,
values = scales_and_palettes$bins_mins / 60,
title = "Time to care (hours)"
)
}
13 changes: 10 additions & 3 deletions app/app/mapping/update_map_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ box::use(
app / logic / load_shapes,
app / logic / scales_and_palettes,
app / logic / utils,
app / mapping / update_legend,
)

#' @export
update_map_content <- function(proxy_map, d_selection, selected_layer, r_layers) {
layer_type <- utils$get_layer_type(selected_layer)
selected_layer <- utils$get_standard_layer_name(selected_layer)

if(layer_type == "none") {
if (layer_type == "none") {
show_nothing(proxy_map = proxy_map, r_layers = r_layers)
} else if (layer_type == "polygon") {
show_polygon(proxy_map = proxy_map, d_selection = d_selection, r_layers = r_layers)
} else if (layer_type == "raster") {
show_raster(proxy_map = proxy_map, selected_layer = selected_layer, r_layers = r_layers)
}

update_legend$update_legend(
proxy_map = proxy_map,
selected_layer = selected_layer,
layer_type = layer_type
)
}

show_nothing <- function(proxy_map, r_layers) {
Expand Down Expand Up @@ -107,11 +114,11 @@ show_polygon <- function(proxy_map, d_selection, r_layers) {
}

remove_or_hide_grp <- function(proxy_map, grp) {
if(is_polygon_grp(grp)) {
if (is_polygon_grp(grp)) {
proxy_map |>
leaflet$clearGroup(grp)
}
if(is_raster_grp(grp)) {
if (is_raster_grp(grp)) {
proxy_map |>
leaflet$hideGroup(grp)
}
Expand Down
1 change: 1 addition & 0 deletions app/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library(bslib)
library(dplyr)
library(here)
library(leafgl)
library(leaflegend)
library(leaflet)
library(purrr)
library(rhino)
Expand Down
15 changes: 15 additions & 0 deletions app/renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,21 @@
],
"Hash": "d6b521a3382490f766de5fedf95fc1af"
},
"leaflegend": {
"Package": "leaflegend",
"Version": "1.2.1",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
"R",
"base64enc",
"htmltools",
"htmlwidgets",
"leaflet",
"stats"
],
"Hash": "b6ae2af92974c56c504b889287c6a9d7"
},
"leaflet": {
"Package": "leaflet",
"Version": "2.2.2",
Expand Down

0 comments on commit f91cdc6

Please sign in to comment.