Skip to content

Commit

Permalink
fixes #105 and improves restore when some spatial components are miss…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
see24 committed Dec 22, 2023
1 parent f3d127e commit 8647253
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
18 changes: 13 additions & 5 deletions R/app_utils_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ update_call <- function(input, update_fun, value, arg_name, comment, session){
}
}

spat_vuln_hide <- function(id, check_exists, do_spat, restored){
spat_vuln_hide <- function(id, check_exists, do_spat, restored, spat_inc){
mis <- paste0("missing_", id)
mapid <- paste0("map_", id)
nmis <- paste0("not_missing_", id)
Expand All @@ -323,10 +323,18 @@ spat_vuln_hide <- function(id, check_exists, do_spat, restored){
shinyjs::show(mis)
}
} else if(isTruthy(restored)){
shinyjs::hide(mis)
shinyjs::hide(mapid)
shinyjs::show(nmis)
shinyjs::show(tblid)
if(isTruthy(spat_inc)){
shinyjs::hide(mis)
shinyjs::hide(mapid)
shinyjs::show(nmis)
shinyjs::show(tblid)
} else {
shinyjs::show(mis)
shinyjs::hide(mapid)
shinyjs::hide(nmis)
shinyjs::hide(tblid)
}

}
}

Expand Down
28 changes: 17 additions & 11 deletions R/ccvi_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,11 @@ ccvi_app <- function(testmode_in, ...){

}
})
# doing this rather than eventReactive so that it still has a value (NULL)
# if shinyalert is not called
hs_rast <- reactiveVal()

hs_rast <- eventReactive(input$shinyalert, {
observeEvent(input$shinyalert, {
if (isTRUE(getOption("shiny.testmode"))) {
pth <- system.file("extdata/rng_chg_45.tif",
package = "ccviR")
Expand All @@ -857,10 +860,9 @@ ccvi_app <- function(testmode_in, ...){
}else {
names(pth) <- fs::path_file(pth) %>% fs::path_ext_remove()

check_trim(terra::rast(pth))
out <- check_trim(terra::rast(pth))
hs_rast(out)
}


})

ptn_poly <- reactive({
Expand Down Expand Up @@ -1107,7 +1109,7 @@ ccvi_app <- function(testmode_in, ...){
})

# C2ai
observe({spat_vuln_hide("C2ai", clim_vars()$htn, doSpatial(), restored_df())})
observe({spat_vuln_hide("C2ai", clim_vars()$htn, doSpatial(), restored_df(), spat_res()$HTN_1)})

output$map_C2ai <- leaflet::renderLeaflet({
req(doSpatial())
Expand Down Expand Up @@ -1149,7 +1151,9 @@ ccvi_app <- function(testmode_in, ...){
outputOptions(output, "box_C2ai", suspendWhenHidden = FALSE)

# C2aii
observe({spat_vuln_hide("C2aii", ptn_poly(), doSpatial(), restored_df())})
observe({
spat_vuln_hide("C2aii", ptn_poly(), doSpatial(), restored_df(), spat_res()$PTN)
})

output$map_C2aii <- leaflet::renderLeaflet({
req(doSpatial())
Expand All @@ -1175,7 +1179,9 @@ ccvi_app <- function(testmode_in, ...){
outputOptions(output, "box_C2aii", suspendWhenHidden = FALSE)

# C2bi
observe({spat_vuln_hide("C2bi", clim_vars()$map, doSpatial(), restored_df())})
observe({
spat_vuln_hide("C2bi", clim_vars()$map, doSpatial(), restored_df(), spat_res()$MAP_max)
})

output$map_C2bi <- leaflet::renderLeaflet({
req(doSpatial())
Expand All @@ -1202,14 +1208,14 @@ ccvi_app <- function(testmode_in, ...){

# D2 and D3
observe({
spat_vuln_hide("D2_3", hs_rast(), doSpatial(), restored_df())
spat_vuln_hide("D2_3", hs_rast(), doSpatial(), restored_df(), spat_res()$range_change)
})

# reclassify raster
hs_rast2 <- reactive({
req(hs_rast())
rast <- terra::classify(hs_rast(),
rcl = hs_rcl_mat(), right = NA)
rcl = hs_rcl_mat(), right = NA)
})

output$map_D2_3 <- leaflet::renderLeaflet({
Expand Down Expand Up @@ -1239,7 +1245,7 @@ ccvi_app <- function(testmode_in, ...){
box_val <- spat_res2() %>%
pull(.data$D2)

if(nrow(spat_res2()) > 1 & isTruthy(hs_rast())){
if(nrow(spat_res2()) > 1 & isTruthy(spat_res()$range_change)){
valueNm <- valueNms[ 4- box_val]
div(strong("Calculated effect on vulnerability."),
HTML("<font color=\"#FF0000\"><b> Spatial results can not be edited when multiple scenarios are provided.</b></font>"),
Expand All @@ -1266,7 +1272,7 @@ ccvi_app <- function(testmode_in, ...){
box_val <- spat_res2() %>%
pull(.data$D3)

if(nrow(spat_res2()) > 1 & isTruthy(hs_rast())){
if(nrow(spat_res2()) > 1 & isTruthy(spat_res()$range_overlap)){
valueNm <- valueNms[4 - box_val]
div(strong("Calculated effect on vulnerability."),
HTML("<font color=\"#FF0000\"><b> Spatial results can not be edited when multiple scenarios are provided.</b></font>"),
Expand Down
Binary file modified inst/extdata/clim_files/processed/clim_poly.dbf
Binary file not shown.

0 comments on commit 8647253

Please sign in to comment.