From 197ff91fa04e486b2eff15e358e6b6c79dd06cef Mon Sep 17 00:00:00 2001 From: Nicoletta Farabullini <41536517+nfarabullini@users.noreply.github.com> Date: Mon, 6 Dec 2021 12:01:43 +0100 Subject: [PATCH] Feature/various issues (#263) * changes for issue #228 changes for issue #223 required edits implemented * added number of samples to teh analysis settings number of samples is retrieved from analysis settings in step 3 minor adjustments * fix to pop up tooltip * replaced "Model Values" table with set of tabs * small bug fix restored correct package versions * added functionality for single tab in model values in case no parameters_group is available * fixed list parameters rendering adjusted number of characters limit in tooltip box * adjusted list format * updates for locnumbers e.g. "1-" * fixes for exposure validation map * clean up for validation map * update to buildcustom * merging of data files for exposure validation map * Update analysis setting names (#265) Co-authored-by: sambles --- BFE_RShiny/oasisui/NAMESPACE | 3 + BFE_RShiny/oasisui/R/buildCustom_module.R | 346 +++++------ BFE_RShiny/oasisui/R/check_loc.R | 1 + BFE_RShiny/oasisui/R/datahub.R | 5 + .../oasisui/R/exposurevalidation_map_module.R | 74 ++- BFE_RShiny/oasisui/R/map.R | 21 +- BFE_RShiny/oasisui/R/modelParams_funs.R | 586 ++++++++++++++++-- BFE_RShiny/oasisui/R/output_config_module.R | 13 +- .../oasisui/R/step1_choosePortfolio_module.R | 16 +- .../oasisui/R/step2_chooseAnalysis_module.R | 51 +- BFE_RShiny/oasisui/man/Global_funs.Rd | 21 + 11 files changed, 815 insertions(+), 322 deletions(-) create mode 100644 BFE_RShiny/oasisui/man/Global_funs.Rd diff --git a/BFE_RShiny/oasisui/NAMESPACE b/BFE_RShiny/oasisui/NAMESPACE index edc58c6a..7816cbff 100644 --- a/BFE_RShiny/oasisui/NAMESPACE +++ b/BFE_RShiny/oasisui/NAMESPACE @@ -4,6 +4,7 @@ export("%>%") export("%notin%") export(APIgetenv) export(DataHub) +export(Global_funs) export(OASISUI_GUEST_ID) export(OasisAPI) export(Status) @@ -164,7 +165,9 @@ importFrom(DT,styleEqual) importFrom(R6,R6Class) importFrom(arrow,read_parquet) importFrom(arrow,write_parquet) +importFrom(bsplus,bs_embed_popover) importFrom(bsplus,bs_embed_tooltip) +importFrom(bsplus,use_bs_popover) importFrom(data.table,fread) importFrom(data.table,fwrite) importFrom(dplyr,"%>%") diff --git a/BFE_RShiny/oasisui/R/buildCustom_module.R b/BFE_RShiny/oasisui/R/buildCustom_module.R index 48464dd1..3d4240ba 100644 --- a/BFE_RShiny/oasisui/R/buildCustom_module.R +++ b/BFE_RShiny/oasisui/R/buildCustom_module.R @@ -34,8 +34,9 @@ buildCustomUI <- function(id) { id = ns("panel_build_Custom"), tabPanel( title = "Model Values", - numericInput(ns("inputnumsamples"), label = "Number of Samples:", value = 0), - DTOutput(ns("dt_model_values"))), + numericInput(ns("inputnumsamples"), label = "Number of Samples:", value = 9), + uiOutput(ns("tabs_BuildCustom"), inline = TRUE) + ), tabPanel( title = "File Uploads", fluidRow(uiOutput(ns("browsers_tables"))) @@ -65,15 +66,15 @@ buildCustomUI <- function(id) { #' #' @export buildCustom <- function(input, - output, - session, - portfolioID, - modelID, - supplierID, - versionID, - analysisID, - counter, - active = reactive(TRUE)) { + output, + session, + portfolioID, + modelID, + supplierID, + versionID, + analysisID, + counter, + active = reactive(TRUE)) { ns <- session$ns @@ -95,7 +96,8 @@ buildCustom <- function(input, # Output IDs tables outputID = NULL, # extrapolate file ids - file_ids = NULL + file_ids = list(), + list_files = list() ) # Initialize ----------------------------------------------------------------- @@ -104,12 +106,12 @@ buildCustom <- function(input, counter() }, ignoreInit = TRUE, { show("panel_build_custom_actions") - selectRows(proxy = dataTableProxy("dt_model_values"), selected = NULL) # initialize table selection to NULL every time panel is opened result$settings_df <- NULL result$tbl_modelsDetails <- NULL result$settings_tbl <- NULL .reloadtbl_modelsValue() + }) output$paneltitle_BuildCustom <- renderUI({ @@ -121,16 +123,41 @@ buildCustom <- function(input, logMessage("hiding panelBuildCustom") }) - output$dt_model_values <- renderDT(server = FALSE, { - datatable(result$settings_tbl, caption = "Double click on Default values to edit", - editable = list(target = 'cell', disable = list(columns = c(1,2))), selection = "none", rownames = FALSE) - }) + # render dynamically multiple tabs for Model Values section + output$tabs_BuildCustom <- renderUI({ + nTabs <- length(result$tbl_modelsDetails$model_settings$parameter_groups) + if (nTabs == 0) { + # in case parameter_group is not available, create only one tab + param_group <- as.list( + c( + names(result$tbl_modelsDetails$model_settings[1:2]), + unlist( + lapply(result$tbl_modelsDetails$model_settings[3:length(result$tbl_modelsDetails$model_settings)], function(x) { + sapply(x, function(y) { + y$name + }) + })) + ) + ) + names(param_group) <- NULL + myTabs = lapply(1, function (x) { + tabPanel( + title = "Model Parameters", + Global_funs(session, result$tbl_modelsDetails$model_settings, "generation", + param_group) + ) + }) + } else { + myTabs = lapply(seq_len(nTabs), function (x) { + tabPanel( + title = result$tbl_modelsDetails$model_settings$parameter_groups[[x]]$name, + Global_funs(session, result$tbl_modelsDetails$model_settings, "generation", + result$tbl_modelsDetails$model_settings$parameter_groups[[x]]$presentation_order) + ) + }) + } - # extrapolate changed cells - observeEvent(input$dt_model_values_cell_edit, { - inp_celledit <- input$dt_model_values_cell_edit - result$settings_df[inp_celledit$row, "value"] <- inp_celledit$value - result$settings_df[inp_celledit$row, "changed"] <- TRUE + do.call(tabsetPanel, myTabs) }) # dynamically create fileInputs and DTOutputs @@ -151,8 +178,10 @@ buildCustom <- function(input, ui_content <- list() for (i in seq_len(length(df_selectors))) { ui_content[[i * 2]] <- fluidRow(column(1), column(10, DTOutput(ns(paste0("dt_", result$outputID[i]))))) - ui_content[[i * 2 - 1]] <- fluidRow(column(1), column(10, fileInput(inputId = ns(result$inputID[i]), label = label_pre[[i]], - multiple = TRUE, accept = accept[i]))) + ui_content[[i * 2 - 1]] <- fluidRow(column(1), + column(10, fileInput(inputId = ns(result$inputID[i]), + label = label_pre[[i]], + multiple = TRUE, accept = accept[i]))) } tagList(ui_content) } else { @@ -199,101 +228,74 @@ buildCustom <- function(input, } }) }) - - lapply(seq_len(length(h)), function(i) { - input_dt <- paste0("dt_", h[i], "_rows_selected") - observeEvent(input[[input_dt]], { - tbl <- result$tbl_files %>% filter(file_description == h[i]) - tbl <- tbl[nrow(tbl):1,] - file_ids <- tbl[input[[input_dt]], "id"] - result$file_ids[[i]] <- file_ids - file_names <- tbl[input[[input_dt]], "filename"] - # because df is reversed, also order of row has to be reversed - result$list_files[[h[i]]] <- file_names - result$list_files[[h[i]]] <- result$list_files[[h[i]]][!is.na(result$list_files[[h[i]]])] - if (length(result$list_files[[h[i]]]) > 1) { - result$list_files[[h[i]]] <- as.list(result$list_files[[h[i]]]) - } - }) - }) + .reloadtbl_modelsFiles() }) - # output new analysis settings with changed values + # output new analysis settings with changed values through tabs observeEvent(input$abuttonselsettings, { - # update default choices for edited model settings - if (any(result$settings_df$changed)) { - new_settings <- result$settings_df %>% filter(changed) - res_mdlsettings <- result$tbl_modelsDetails$model_settings - - invisible(lapply(seq_len(length(names(res_mdlsettings))), function(x) { - if (is.null(res_mdlsettings[[x]]$name)) { - lapply(seq_len(length(res_mdlsettings[[x]])), function(y) { - findSetting <- new_settings$names %in% res_mdlsettings[[x]][[y]] - # (typically new_settings$names will match the name attribute, i.e. res_mdlsettings[[x]][[y]]$name) - if (any(findSetting)) { - if (is.list(res_mdlsettings[[x]][[y]]$default) && !is.null(names(res_mdlsettings[[x]][[y]]$default))) { - # dict parameters case - result$tbl_modelsDetails$model_settings[[x]][[y]]$default <- jsonlite::fromJSON(new_settings$value[which(findSetting)]) - } else { - if (is.list(res_mdlsettings[[x]][[y]]$default)) { - # list parameters case - result$tbl_modelsDetails$model_settings[[x]][[y]]$default <- as.list(strsplit(new_settings$value[which(findSetting)], split = ", ")[[1]]) - } else { - result$tbl_modelsDetails$model_settings[[x]][[y]]$default <- strsplit(new_settings$value[which(findSetting)], split = ", ")[[1]] - } - } - } - }) - } else { - # e.g. Event Occurrence - findSetting <- new_settings$names %in% res_mdlsettings[[x]] - if (any(findSetting)) { - result$tbl_modelsDetails$model_settings[[x]]$default <- strsplit(new_settings$value[which(findSetting)], split = ", ")[[1]] - } - } - })) - } - - # TODO: update result$tbl_modelsDetails$model_setting just once, do above and below in memory first (i.e. make a function as outlined above) - #result$tbl_modelsDetails$model_settings$event_occurrence_id <- result$tbl_modelsDetails$model_settings$event_occurrence_id$default - #result$tbl_modelsDetails$model_settings$event_set <- result$tbl_modelsDetails$model_settings$event_set$default - # replace all settings similarly with just default values fetch_model_settings <- function(model_settings) { - model_settings <- model_settings %>% unlist(recursive = FALSE) - string_input <- unlist(lapply(grep("string_parameters", names(model_settings)), function(x) { - if (!is.null(model_settings[[x]]$default)) { - model_settings[[x]]$default - } - })) - dict_input <- lapply(grep("dictionary_parameters", names(model_settings)), function(x) { - if (!is.null(model_settings[[x]]$default)) { - model_settings[[x]]$default - } + n_string <- grep("string_parameters", names(model_settings)) + string_input <- lapply(seq_len(length(model_settings[n_string]$string_parameters)), function(x) { + sapply(seq_len(length(model_settings[n_string]$string_parameters[[x]]$default)), function(z) { + if (!is.null(input[[paste0("string_parameters", x, z)]])) { + result$tbl_modelsDetails$model_settings$string_parameters[[x]]$default[z] <- + input[[paste0("string_parameters", x, z)]] + } + result$tbl_modelsDetails$model_settings$string_parameters[[x]]$default[z] + }) }) - dropdown_input <- lapply(grep("dropdown_parameters", names(model_settings)), function(x) { - if (!is.null(model_settings[[x]]$default)) { - model_settings[[x]]$default - } + n_dict <- grep("dictionary_parameters", names(model_settings)) + dict_input <- lapply(seq_len(length(model_settings[n_dict]$dictionary_parameters)), function(x) { + sapply(seq_len(length(model_settings[n_dict]$dictionary_parameters[[x]]$default)), function(z) { + if (!is.null(input[[paste0("dictionary_parameters", x, z)]])) { + result$tbl_modelsDetails$model_settings$dictionary_parameters[[x]]$default[z] <- + input[[paste0("dictionary_parameters", x, z)]] + } + result$tbl_modelsDetails$model_settings$dictionary_parameters[[x]]$default[z] + }) }) - # below is purposedly list() rather than NULL in case there are none (i.e. not doing unlist() on purpose)! - boolean_input <- lapply(grep("boolean_parameters", names(model_settings)), function(x) { - if (!is.null(model_settings[[x]]$default)) { - as.logical(model_settings[[x]]$default) - } + n_dropdown <- grep("dropdown_parameters", names(model_settings)) + dropdown_input <- lapply(seq_len(length(model_settings[n_dropdown]$dropdown_parameters)), function(x) { + sapply(seq_len(length(model_settings[n_dropdown]$dropdown_parameters[[x]]$default)), function(z) { + if (!is.null(input[[paste0("dropdown_parameters", x)]])) { + result$tbl_modelsDetails$model_settings$dropdown_parameters[[x]]$default[z] <- + input[[paste0("dropdown_parameters", x)]] + } + result$tbl_modelsDetails$model_settings$dropdown_parameters[[x]]$default[z] + }) }) - float_input <- lapply(grep("float_parameters", names(model_settings)), function(x) { - if (!is.null(model_settings[[x]]$default)) { - model_settings[[x]]$default - } + n_bool <- grep("boolean_parameters", names(model_settings)) + boolean_input <- lapply(seq_len(length(model_settings[n_bool]$boolean_parameters)), function(x) { + sapply(seq_len(length(model_settings[n_bool]$boolean_parameters[[x]]$default)), function(z) { + if (!is.null(input[[paste0("boolean_parameters", x)]])) { + result$tbl_modelsDetails$model_settings$boolean_parameters[[x]]$default[z] <- + input[[paste0("boolean_parameters", x)]] + } + unlist(result$tbl_modelsDetails$model_settings$boolean_parameters[[x]]$default[z]) + }) }) - list_input <- lapply(grep("list_parameters", names(model_settings)), function(x) { - if (!is.null(model_settings[[x]]$default)) { - model_settings[[x]]$default + n_float <- grep("float_parameters", names(model_settings)) + float_input <- lapply(seq_len(length(model_settings[n_float]$float_parameters)), function(x) { + sapply(seq_len(length(model_settings[n_float]$float_parameters[[x]]$default)), function(z) { + if (!is.null(input[[paste0("float_parameters", x)]])) { + result$tbl_modelsDetails$model_settings$float_parameters[[x]]$default[z] <- + input[[paste0("float_parameters", x)]] + } + result$tbl_modelsDetails$model_settings$float_parameters[[x]]$default[z] + }) + }) + + n_list <- grep("list_parameters", names(model_settings)) + list_input <- lapply(seq_len(length(model_settings[n_list]$list_parameters)), function(x) { + if (!is.null(input[[paste0("list_parameters", x)]])) { + result$tbl_modelsDetails$model_settings$list_parameters[[x]]$default <- + as.list(strsplit(input[[paste0("list_parameters", x)]], ",")[[1]]) + result$tbl_modelsDetails$model_settings$list_parameters[[x]]$default } }) @@ -313,19 +315,20 @@ buildCustom <- function(input, inputs_name <- list() for (param in seq_len(length(params_list))) { if (length(inputs_list[[param]]) > 0) { - param_name <- unlist(lapply(grep(params_list[[param]], names(model_settings)), function(i) { - model_settings[[i]][["name"]] + n_param <- grep(params_list[[param]], names(model_settings)) + param_name <- unlist(lapply(seq_len(length(model_settings[n_param][[1]])), function(i) { + # model_settings[i][[1]][[param]][["name"]] + model_settings[n_param][[1]][[i]][["name"]] })) inputs_name[[param]] <- param_name } # if a param is NULL and skipped, it will result in a NULL entry in the inputs_name list that will be removed by the unlist below } inputs_name <- unlist(inputs_name) - # find boolean parameters names if (length(boolean_input) > 0) { boolean_name <- lapply(seq_len(length(boolean_input)), function(i) { - model_match <- model_settings[grep("boolean_parameters", names(model_settings))][[i]] + model_match <- model_settings[grep("boolean_parameters", names(model_settings))]$boolean_parameters[[i]] model_match[["name"]] }) } else { @@ -333,8 +336,9 @@ buildCustom <- function(input, } # create model settings for analysis settings - model_settings_out <- c(model_settings$event_set.default, - model_settings$event_occurrence_id.default, + model_settings_out <- c(input$event_set_g, + input$event_occurrence_g, + input$inputnumsamples, # note that boolean_input is a list, making sure that the result of this c() is a flat list! boolean_input, string_input, @@ -343,10 +347,10 @@ buildCustom <- function(input, float_input, dropdown_input) # NULL or list() elements won't survive the c() above! - # create list/vector of names for model settings names_full_list <- c("event_set", "event_occurrence_id", + "number_of_samples", boolean_name, inputs_name) @@ -369,10 +373,29 @@ buildCustom <- function(input, "Model settings filtered by chosen entries.") gul_summaries <- summary_template + h <- unlist(result$inputID) + lapply(seq_len(length(h)), function(i) { + input_dt <- paste0("dt_", h[i], "_rows_selected") + if (!is.null(input[[input_dt]])) { + tbl <- result$tbl_files %>% filter(file_description == h[i]) + tbl <- tbl[nrow(tbl):1,] + file_ids <- tbl[input[[input_dt]], "id"] + result$file_ids[[i]] <- file_ids + file_names <- tbl[input[[input_dt]], "filename"] + # because df is reversed, also order of row has to be reversed + result$list_files[[h[i]]] <- file_names + result$list_files[[h[i]]] <- result$list_files[[h[i]]][!is.na(result$list_files[[h[i]]])] + if (length(result$list_files[[h[i]]]) > 1) { + result$list_files[[h[i]]] <- as.list(result$list_files[[h[i]]]) + } + result$list_files[[h[i]]] + } + }) + result$filtered_analysis_settings <- list(analysis_settings = c( list( - module_supplier_id = supplierID(), - model_version_id = versionID(), + model_supplier_id = supplierID(), + model_name_id = modelID(), number_of_samples = input$inputnumsamples, model_settings = c(filtered_settings, result$list_files), gul_output = FALSE, @@ -386,98 +409,6 @@ buildCustom <- function(input, query_path = paste("models", modelID(), "settings", sep = "/"), query_method = "GET" ) - result$tbl_files <- session$userData$data_hub$return_tbl_dataFiles(name = "") - result$list_files <- list() - result$file_ids <- list() - - # get entries for table: name, description and default value(s) - tbl_mdl_settings <- result$tbl_modelsDetails$model_settings - # drop "parameter_groups" (and potentially other unknown entries): - subset_settings <- names(tbl_mdl_settings) %in% c("event_set", - "event_occurrence_id", - "string_parameters", - "boolean_parameters", - "float_parameters", - "list_parameters", - "dictionary_parameters", - "dropdown_parameters") - tbl_mdl_settings <- tbl_mdl_settings[subset_settings] - order_list <- unlist(lapply(seq_len(length(result$tbl_modelsDetails$model_settings$parameter_groups)), function(x) { - unlist(lapply(seq_len(length(result$tbl_modelsDetails$model_settings$parameter_groups[[x]]$presentation_order)), function(y) { - result$tbl_modelsDetails$model_settings$parameter_groups[[x]]$presentation_order[[y]] - })) - })) - - settings_names <- unlist(lapply(seq_len(length(names(tbl_mdl_settings))), function(x) { - if (is.null(tbl_mdl_settings[[x]]$name)) { - lapply(seq_len(length(tbl_mdl_settings[[x]])), function(y) { - tbl_mdl_settings[[x]][[y]]$name - }) - } else { - tbl_mdl_settings[[x]]$name - } - })) - - intersect_n <- intersect(order_list, settings_names) - if (length(intersect_n) != length(settings_names)) { - extras <- unlist(lapply(settings_names, function(x) { - if (x %notin% intersect_n) { - grep(x, settings_names) - } - })) - } else { - extras <- NULL - } - reorder_nums <- unlist(lapply(intersect_n, function(x) {grep(x, settings_names)})) - reorder_names <- c(settings_names[extras], settings_names[reorder_nums]) - - settings_desc <- unlist(lapply(seq_len(length(names(tbl_mdl_settings))), function(x) { - if (is.null(tbl_mdl_settings[[x]]$desc)) { - lapply(seq_len(length(tbl_mdl_settings[[x]])), function(y) { - tbl_mdl_settings[[x]][[y]]$desc - }) - } else { - tbl_mdl_settings[[x]]$desc - } - })) - reorder_desc <- c(settings_desc[extras], settings_desc[reorder_nums]) - - settings_tooltip <- unlist(lapply(seq_len(length(names(tbl_mdl_settings))), function(x) { - if (is.null(tbl_mdl_settings[[x]]$tooltip)) { - lapply(seq_len(length(tbl_mdl_settings[[x]])), function(y) { - tbl_mdl_settings[[x]][[y]]$tooltip - }) - } else { - tbl_mdl_settings[[x]]$tooltip - } - })) - reorder_tooltip <- c(settings_tooltip[extras], settings_tooltip[reorder_nums]) - - settings_default <- unlist(lapply(seq_len(length(names(tbl_mdl_settings))), function(x) { - if (is.null(tbl_mdl_settings[[x]]$default)) { - lapply(seq_len(length(tbl_mdl_settings[[x]])), function(y) { - dflt <- tbl_mdl_settings[[x]][[y]]$default - if (is.list(dflt) && !is.null(names(dflt))) { - paste(jsonlite::toJSON(dflt)) - } else { - # below works on a list same as on a vector - paste(dflt, collapse = ", ") - } - }) - } else { - # e.g. event set and event occurrence - # below works on a list same as on a vector - paste(tbl_mdl_settings[[x]]$default, collapse = ", ") - } - })) - reorder_default <- c(settings_default[extras], settings_default[reorder_nums]) - - result$settings_df <- data.frame(names = reorder_names, descr = reorder_desc, tooltip = reorder_tooltip, value = reorder_default, - changed = rep(FALSE, times = length(settings_names)), stringsAsFactors = FALSE) - tmp_df <- result$settings_df[, c("names", "descr", "tooltip", "value")] - colnames(tmp_df) <- c("Setting Name", "Description", "Tooltip", "Default") - # output$dt_model_values depends (renders) on this one: - result$settings_tbl <- tmp_df result$settings_tbl } @@ -517,6 +448,7 @@ buildCustom <- function(input, .reloadtbl_modelsFiles() invisible() } + moduleOutput <- list( fullsettings = reactive({result$filtered_analysis_settings}), fileids = reactive({unlist(result$file_ids)}), diff --git a/BFE_RShiny/oasisui/R/check_loc.R b/BFE_RShiny/oasisui/R/check_loc.R index f315c2af..7ce9a9e4 100644 --- a/BFE_RShiny/oasisui/R/check_loc.R +++ b/BFE_RShiny/oasisui/R/check_loc.R @@ -16,6 +16,7 @@ #' #' @export check_loc <- function(analysisID, portfolioID, data_hub) { + logMessage(".check_loc called") uploaded_locs <- data_hub$get_pf_location_content(id = portfolioID) %>% diff --git a/BFE_RShiny/oasisui/R/datahub.R b/BFE_RShiny/oasisui/R/datahub.R index 0c7fd23d..fdd70b52 100644 --- a/BFE_RShiny/oasisui/R/datahub.R +++ b/BFE_RShiny/oasisui/R/datahub.R @@ -477,6 +477,11 @@ DataHub <- R6Class( query_response <- private$oasisapi$api_get_query(query_path = paste("analyses", id, "lookup_errors_file", sep = "/")) content(query_response$result) }, + # get lookup success file + get_ana_success_summary_content = function(id, ...) { + query_response <- private$oasisapi$api_get_query(query_path = paste("analyses", id, "lookup_success_file", sep = "/")) + content(query_response$result) + }, # invalidate analysis validation summary content invalidate_ana_validation_summary_content = function(id, dataset_identifier, type, ...) { self$invalidate_ana_dataset_header(id, dataset_identifier, type, ...) diff --git a/BFE_RShiny/oasisui/R/exposurevalidation_map_module.R b/BFE_RShiny/oasisui/R/exposurevalidation_map_module.R index 9156f7c2..6620e9b3 100644 --- a/BFE_RShiny/oasisui/R/exposurevalidation_map_module.R +++ b/BFE_RShiny/oasisui/R/exposurevalidation_map_module.R @@ -154,24 +154,33 @@ exposurevalidationmap <- function(input, observeEvent({input$chkgrp_perils result$uploaded_locs_check}, ignoreNULL = FALSE, { - if (!is.null(result$uploaded_locs_check) && nrow(result$uploaded_locs_check) > 0) { - if (is.null(input$chkgrp_perils)) { - result$uploaded_locs_check_peril <- result$uploaded_locs_check %>% - mutate(modeled = NA) - } else { - result$uploaded_locs_check_peril <- result$uploaded_locs_check %>% - # filter(peril_id %in% input$chkgrp_perils) %>% - mutate(modeled = case_when( - is.na(peril_id) ~ FALSE, - peril_id %in% input$chkgrp_perils ~ TRUE, - TRUE ~ NA) - ) %>% - filter(!is.na(modeled)) %>% - select(-peril_id) %>% - distinct() + if (!is.null(result$uploaded_locs_check) && nrow(result$uploaded_locs_check) > 0) { + + if (is.null(input$chkgrp_perils)) { + result$uploaded_locs_check_peril <- result$uploaded_locs_check %>% + mutate(modeled = NA) + if (grepl(".x", names(result$uploaded_locs_check_peril))) { + names(result$uploaded_locs_check_peril) <- gsub(".x", "", names(result$uploaded_locs_check_peril)) + } + names(result$uploaded_locs_check_peril) <- tolower(names(result$uploaded_locs_check_peril)) + } else { + result$uploaded_locs_check_peril <- result$uploaded_locs_check %>% + # filter(peril_id %in% input$chkgrp_perils) %>% + mutate(modeled = case_when( + is.na(peril_id) ~ FALSE, + peril_id %in% input$chkgrp_perils ~ TRUE, + TRUE ~ NA) + ) %>% + filter(!is.na(modeled)) %>% + select(-peril_id) %>% + distinct() + if (grepl(".x", names(result$uploaded_locs_check_peril))) { + names(result$uploaded_locs_check_peril) <- gsub(".x", "", names(result$uploaded_locs_check_peril)) + } + names(result$uploaded_locs_check_peril) <- tolower(names(result$uploaded_locs_check_peril)) + } } - } - }) + }) # Show/Hide table button ----------------------------------------------------- observeEvent(result$uploaded_locs_check_peril, ignoreNULL = FALSE, { @@ -272,6 +281,7 @@ exposurevalidationmap <- function(input, observeEvent(input$exposure_map_click, { if (input$tot_tiv_param == "Circles") { + hide("exposure_table") circles_features <- input$exposure_map_draw_all_features$features @@ -387,8 +397,8 @@ exposurevalidationmap <- function(input, } # check for pins within country borders - match_lat <- grep(TRUE, between(result$uploaded_locs_check_peril$Latitude, min(unlist(lati)), max(unlist(lati)))) - match_long <- grep(TRUE, between(result$uploaded_locs_check_peril$Longitude, min(unlist(long)), max(unlist(long)))) + match_lat <- grep(TRUE, between(result$uploaded_locs_check_peril$latitude, min(unlist(lati)), max(unlist(lati)))) + match_long <- grep(TRUE, between(result$uploaded_locs_check_peril$longitude, min(unlist(long)), max(unlist(long)))) # adjust TIV wrt damage ratio if (is.null(input$damage_ratio)) { @@ -432,8 +442,8 @@ exposurevalidationmap <- function(input, long <- regions@polygons[[entry]]@Polygons[[set]]@coords[,1] # check for pins within country borders - match_lat <- grep(TRUE, between(result$uploaded_locs_check_peril$Latitude, min(lati), max(lati))) - match_long <- grep(TRUE, between(result$uploaded_locs_check_peril$Longitude, min(long), max(long))) + match_lat <- grep(TRUE, between(result$uploaded_locs_check_peril$latitude, min(lati), max(lati))) + match_long <- grep(TRUE, between(result$uploaded_locs_check_peril$longitude, min(long), max(long))) # adjust TIV wrt damage ratio if (is.null(input$damage_ratio)) { @@ -517,12 +527,16 @@ exposurevalidationmap <- function(input, # Exposure validation map .createExposureValMap <- function(df) { marker_colors <- c('green', 'red') + # if ("Latitude" %in% colnames(df)) { + # colnames(df) <- tolower(colnames(df)) + # } + if (is.null(input$chkgrp_perils)) { icon_map <- NULL df <- df leaflet(df) %>% addTiles() %>% - leaflet::setView(mean(df$Longitude), mean(df$Latitude), zoom = 20) + leaflet::setView(mean(df$longitude), mean(df$latitude), zoom = 20) } else { df <- df %>% mutate(modeled = case_when( @@ -638,11 +652,11 @@ exposurevalidationmap <- function(input, # Drawn circles infos, outputs and radius .DrawnCircles <- function(radius, lat_click, long_click, ratio) { # calculate LocID and TIV for pins inside areas - df_info <- data.frame("LocNumber" = format(result$uploaded_locs_check_peril$LocNumber, big.mark = "", + df_info <- data.frame("LocNumber" = format(result$uploaded_locs_check_peril$locnumber, big.mark = "", scientific = FALSE), - "TIV" = result$uploaded_locs_check_peril$BuildingTIV * (ratio/100)) - if(!is.null(result$uploaded_locs_check_peril$StreetAddress)) { - df_info <- cbind(df_info, "Address" = result$uploaded_locs_check_peril$StreetAddress) + "TIV" = result$uploaded_locs_check_peril$buildingtiv * (ratio/100)) + if(!is.null(result$uploaded_locs_check_peril$streetaddress)) { + df_info <- cbind(df_info, "Address" = result$uploaded_locs_check_peril$streetaddress) } info <- .is_within_bounds(df_info, radius, lat_click, long_click, ratio) @@ -668,8 +682,8 @@ exposurevalidationmap <- function(input, .is_within_bounds <- function(uploaded_locs_input, radius, lat_click, long_click, ratio) { # get pins coordinates - long <- result$uploaded_locs_check_peril$Longitude - lat <- result$uploaded_locs_check_peril$Latitude + long <- result$uploaded_locs_check_peril$longitude + lat <- result$uploaded_locs_check_peril$latitude coord_df <- cbind(long_click, lat_click) @@ -686,7 +700,7 @@ exposurevalidationmap <- function(input, degrees_dist_4 <- degrees_dist_2 + 22.5 circle_bounds_4 <- destPoint(coord_df, degrees_dist_4, radius) - lapply(seq_len(length(result$uploaded_locs_check_peril$Longitude)), function(x) { + lapply(seq_len(length(result$uploaded_locs_check_peril$longitude)), function(x) { if ((.between_min_max(circle_bounds_1[, 1], long[x]) && .between_min_max(circle_bounds_1[, 2], lat[x])) || (.between_min_max(circle_bounds_2[, 1], long[x]) && @@ -740,7 +754,7 @@ exposurevalidationmap <- function(input, .showCountryInfo <- function(code, match_long, match_lat, ratio, country_num, part) { if(length(match_long) > 0 && length(match_lat) > 0) { result$tot_country_tiv <- add_commas(sum(unlist(lapply(seq_len(length(match_long)), function (x) { - result$uploaded_locs_check_peril$BuildingTIV[x] + result$uploaded_locs_check_peril$buildingtiv[x] }))) * (ratio/100)) } else { result$tot_country_tiv <- "No locations in this country" diff --git a/BFE_RShiny/oasisui/R/map.R b/BFE_RShiny/oasisui/R/map.R index a8950a0f..60bb2104 100644 --- a/BFE_RShiny/oasisui/R/map.R +++ b/BFE_RShiny/oasisui/R/map.R @@ -112,12 +112,23 @@ build_marker_data <- function(data, session, paramID, step = NULL) { # Extract error messages .keys_errors_msg <- function(data, session, paramID) { + keys_success <- session$userData$data_hub$get_ana_success_summary_content(paramID) keys_errors <- session$userData$data_hub$get_ana_errors_summary_content(id = paramID) - if (!is.null(keys_errors) && is.null(keys_errors$detail)) { - message <- group_by(keys_errors, LocID) %>% - summarize(message = paste(paste(PerilID, ":", Message), collapse = " / ")) - if (length(as.numeric(keys_errors$LocID)) > 0) { - errmessage <- left_join(data, message, by = c("locnumber" = "LocID"))$message + # unify names cases for merging + names(keys_success) <- tolower(names(keys_success)) + names(keys_errors) <- tolower(names(keys_errors)) + + if (!is.null(keys_errors) && is.null(keys_errors$detail) && !is.null(keys_success)) { + if (length(as.numeric(keys_errors$locid)) > 0) { + key_chain <- left_join(keys_success, keys_errors, by = c("loc_id" = "locid")) + message <- group_by(key_chain, loc_id) %>% + summarize(message = paste(paste(perilid, ":", message), collapse = " / ")) + if (grepl("locnumber.x", names(data))) { + names(data) <- gsub(".x", "", names(data)) + } + data$locnumber <- as.character(data$locnumber) + message$loc_id <- as.character(message$loc_id) + errmessage <- left_join(data, message, by = c("locnumber" = "loc_id"))$message } else { errmessage <- rep_len(NA, nrow(data)) } diff --git a/BFE_RShiny/oasisui/R/modelParams_funs.R b/BFE_RShiny/oasisui/R/modelParams_funs.R index df703044..6fdec6c1 100644 --- a/BFE_RShiny/oasisui/R/modelParams_funs.R +++ b/BFE_RShiny/oasisui/R/modelParams_funs.R @@ -12,45 +12,50 @@ basicConfig_funs <- function(session, model_settings) { ns <- session$ns # Event set .event_set_fun <- function(model_settings) { - selectChoices <- lapply(model_settings$event_set.options, function(x) { - x$id - }) - names(selectChoices) <- sapply(model_settings$event_set.options, function(x) { - x$desc - }) - doHide <- length(model_settings$event_set.options) < 2 - style <- if (doHide) "display: none" else "" - selectInput( - inputId = ns("event_set"), - label = "Event Set:", - choices = selectChoices, - selected = model_settings$event_set.default, - multiple = FALSE - ) %>% tagAppendAttributes(id = ns("eventset_ctnr"), style = style) + if (is.null(model_settings$event_set.used_for) || model_settings$event_set.used_for == "losses") { + selectChoices <- lapply(model_settings$event_set.options, function(x) { + x$id + }) + names(selectChoices) <- sapply(model_settings$event_set.options, function(x) { + x$desc + }) + doHide <- length(model_settings$event_set.options) < 2 + style <- if (doHide) "display: none" else "" + selectInput( + inputId = ns("event_set"), + label = "Event Set:", + choices = selectChoices, + selected = model_settings$event_set.default, + multiple = FALSE + ) %>% tagAppendAttributes(id = ns("eventset_ctnr")) + } } # Event occurrence .event_occurrence_fun <- function(model_settings) { - selectChoices <- lapply(model_settings$event_occurrence_id.options, function(x) { - x$id - }) - names(selectChoices) <- sapply(model_settings$event_occurrence_id.options, function(x) { - x$desc - }) - doHide <- length(model_settings$event_occurrence_id.options) < 2 - style <- if (doHide) "display: none" else "" - selectInput( - inputId = ns("event_occurrence"), - label = "Event Occurrence:", - choices = selectChoices, - selected = model_settings$event_occurrence_id.default, - multiple = FALSE - ) %>% tagAppendAttributes(id = ns("eventoccurrence_ctnr"), style = style) + if (is.null(model_settings$event_occurrence_id.used_for) || model_settings$event_occurrence_id.used_for == "losses") { + selectChoices <- lapply(model_settings$event_occurrence_id.options, function(x) { + x$id + }) + names(selectChoices) <- sapply(model_settings$event_occurrence_id.options, function(x) { + x$desc + }) + doHide <- length(model_settings$event_occurrence_id.options) < 2 + style <- if (doHide) "display: none" else "" + selectInput( + inputId = ns("event_occurrence"), + label = "Event Occurrence:", + choices = selectChoices, + selected = model_settings$event_occurrence_id.default, + multiple = FALSE + ) %>% tagAppendAttributes(id = ns("eventoccurrence_ctnr")) + } } tagList(.event_set_fun(model_settings), .event_occurrence_fun(model_settings) ) + } #' Advanced Output Configuration functions @@ -60,21 +65,44 @@ basicConfig_funs <- function(session, model_settings) { #' @param session Current session. #' @param model_settings Model settings retrieved from the API. #' +#' @importFrom bsplus bs_embed_popover +#' @importFrom bsplus use_bs_popover +#' #' @return List of UI elements (input/selector widgets). #' #' @export advancedConfig_funs <- function(session, model_settings) { ns <- session$ns - # string parameters .string_fun <- function(model_settings) { if (length(grep("string_parameters", names(model_settings))) > 0) { lapply(grep("string_parameters", names(model_settings)), function(x) { + if (!is.null(model_settings[[x]]$tooltip)) { + tooltip_tex <- model_settings[[x]]$tooltip + } else { + tooltip_tex <- model_settings[[x]]$name + } + if (!is.null(model_settings[[x]]$desc)) { + label_widget <- model_settings[[x]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]]$name) + } if (is.null(model_settings[[x]]$used_for) || model_settings[[x]]$used_for == "losses") { - textInput( - inputId = ns(paste0("string_parameters", x)), - label = paste0(gsub("_", " ", model_settings[[x]]$name), ":") %>% capitalize_first_letter(), - value = model_settings[[x]]$default + fluidRow(use_bs_popover(), + column(10, + textInput( + inputId = ns(paste0("string_parameters", x)), + label = div(paste0(label_widget, ":") %>% capitalize_first_letter()), + value = model_settings[[x]]$default + )), + column(1, + actionButton(ns(paste0("tooltip_string_", x)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) ) } }) @@ -85,11 +113,32 @@ advancedConfig_funs <- function(session, model_settings) { .list_fun <- function(model_settings) { if (length(grep("list_parameters", names(model_settings))) > 0) { lapply(grep("list_parameters", names(model_settings)), function(x) { + if (!is.null(model_settings[[x]]$tooltip)) { + tooltip_tex <- model_settings[[x]]$tooltip + } else { + tooltip_tex <- model_settings[[x]]$name + } + if (!is.null(model_settings[[x]]$desc)) { + label_widget <- model_settings[[x]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]]$name) + } if (is.null(model_settings[[x]]$used_for) || model_settings[[x]]$used_for == "losses") { - textInput( - inputId = ns(paste0("list_parameters", x)), - label = paste0(gsub("_", " ", model_settings[[x]]$name), ":") %>% capitalize_first_letter(), - value = paste(unlist(model_settings[[x]]$default), collapse = ", ") + fluidRow(use_bs_popover(), + column(10, + textInput( + inputId = ns(paste0("list_parameters", x)), + label = paste0(label_widget, ":") %>% capitalize_first_letter(), + value = paste(unlist(model_settings[[x]]$default), collapse = ", ") + )), + column(1, + actionButton(ns(paste0("tooltip_list_", x)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) ) } }) @@ -101,11 +150,27 @@ advancedConfig_funs <- function(session, model_settings) { if (length(grep("dictionary_parameters", names(model_settings))) > 0) { lapply(grep("dictionary_parameters", names(model_settings)), function(x) { if (is.null(model_settings[[x]]$used_for) || model_settings[[x]]$used_for == "losses") { + if (!is.null(model_settings[[x]]$tooltip)) { + tooltip_tex <- model_settings[[x]]$tooltip + } else { + tooltip_tex <- model_settings[[x]]$name + } lapply(seq_len(length(model_settings[[x]]$default)), function(y) { - textInput( - inputId = ns(paste0("dictionary_parameters", x, y)), - label = paste(model_settings[[x]]$name, names(model_settings[[x]]$default[y]), sep = ": "), - value = model_settings[[x]]$default[[y]] + fluidRow(use_bs_popover(), + column(10, + textInput( + inputId = ns(paste0("dictionary_parameters", x, y)), + label = paste(model_settings[[x]]$name, names(model_settings[[x]]$default[y]), sep = ": "), + value = model_settings[[x]]$default[[y]] + )), + column(1, + actionButton(ns(paste0("tooltip_dict_", x)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) ) }) } @@ -117,11 +182,32 @@ advancedConfig_funs <- function(session, model_settings) { .boolean_params_fun <- function(model_settings) { if (length(grep("boolean_parameters", names(model_settings))) > 0) { lapply(grep("boolean_parameters", names(model_settings)), function(x) { + if (!is.null(model_settings[[x]]$tooltip)) { + tooltip_tex <- model_settings[[x]]$tooltip + } else { + tooltip_tex <- model_settings[[x]]$name + } + if (!is.null(model_settings[[x]]$desc)) { + label_widget <- model_settings[[x]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]]$name) + } if (is.null(model_settings[[x]]$used_for) || model_settings[[x]]$used_for == "losses") { - checkboxInput( - inputId = ns(paste0("boolean_parameters", x)), - label = gsub("_", " ", model_settings[[x]]$name) %>% capitalize_first_letter(), - value = model_settings[[x]]$default + fluidRow(use_bs_popover(), + column(10, + checkboxInput( + inputId = ns(paste0("boolean_parameters", x)), + label = label_widget %>% capitalize_first_letter(), + value = model_settings[[x]]$default + )), + column(1, + actionButton(ns(paste0("tooltip_bool_", x)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) ) } }) @@ -132,13 +218,35 @@ advancedConfig_funs <- function(session, model_settings) { .float_fun <- function(model_settings) { if (length(grep("float_parameters", names(model_settings))) > 0) { lapply(grep("float_parameters", names(model_settings)), function(x) { + if (!is.null(model_settings[[x]]$tooltip)) { + tooltip_tex <- model_settings[[x]]$tooltip + } else { + tooltip_tex <- model_settings[[x]]$name + } + if (!is.null(model_settings[[x]]$desc)) { + label_widget <- model_settings[[x]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]]$name) + } if (is.null(model_settings[[x]]$used_for) || model_settings[[x]]$used_for == "losses") { - sliderInput( - inputId = ns(paste0("float_parameters", x)), - label = paste0(gsub("_", " ", model_settings[[x]]$name), ":") %>% capitalize_first_letter(), - min = model_settings[[x]]$min, - max = model_settings[[x]]$max, - value = model_settings[[x]]$default + fluidRow(use_bs_popover(), + column(10, + sliderInput( + inputId = ns(paste0("float_parameters", x)), + label = paste0(label_widget, ":") %>% capitalize_first_letter(), + min = model_settings[[x]]$min, + max = model_settings[[x]]$max, + value = model_settings[[x]]$default + ) + ), + column(1, + actionButton(ns(paste0("tooltip_float_", x)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) ) } }) @@ -149,11 +257,37 @@ advancedConfig_funs <- function(session, model_settings) { .dropdown_fun <- function(model_settings) { if (length(grep("dropdown_parameters", names(model_settings))) > 0) { lapply(grep("dropdown_parameters", names(model_settings)), function(x) { + if (!is.null(model_settings[[x]]$tooltip)) { + tooltip_tex <- model_settings[[x]]$tooltip + } else { + tooltip_tex <- model_settings[[x]]$name + } + if (!is.null(model_settings[[x]]$desc)) { + label_widget <- model_settings[[x]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]]$name) + } + list_values <- lapply(seq_len(length(model_settings[[x]]$options)), function (y) { + model_settings[[x]]$options[[y]]$id + }) if (is.null(model_settings[[x]]$used_for) || model_settings[[x]]$used_for == "losses") { - textInput( - inputId = ns(paste0("dropdown_parameters", x)), - label = paste(model_settings[[x]]$name, names(model_settings[[x]]$default), sep = ": "), - value = model_settings[[x]]$default + fluidRow(use_bs_popover(), + column(10, + selectInput( + inputId = ns(paste0("dropdown_parameters", x)), + label = paste0(label_widget, sep = ": "), + choices = list_values, + selected = model_settings[[x]]$default, + multiple = TRUE + )), + column(1, + actionButton(ns(paste0("tooltip_dropdown_", x)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) ) } }) @@ -182,3 +316,341 @@ advancedConfig_funs <- function(session, model_settings) { .float_fun(model_settings), .dropdown_fun(model_settings)) } + + +#' Global Model Settings parameters function +#' +#' @description Functions for the Build Custom UI. +#' +#' @param session Current session. +#' @param model_settings Model settings retrieved from the API. +#' @param ui_step Step 2. +#' @param ls_param_group Parameter groups. +#' +#' @importFrom bsplus bs_embed_popover +#' @importFrom bsplus use_bs_popover +#' +#' @return List of UI elements (input/selector widgets). +#' +#' @export +Global_funs <- function(session, model_settings, ui_step, ls_param_group) { + ns <- session$ns + + # account for event_set and event_occurrence_id which are outside of the other parameters + .event_set_fun_g <- function(model_settings) { + if (length(grep("event_set", ls_param_group)) > 0) { + if (is.null(model_settings$event_set$used_for) || model_settings$event_set$used_for == ui_step) { + selectChoices <- lapply(model_settings$event_set$options, function(x) { + x$id + }) + names(selectChoices) <- sapply(model_settings$event_set$options, function(x) { + x$desc + }) + doHide <- length(model_settings$event_set$options) < 2 + style <- if (doHide) "display: none" else "" + selectInput( + inputId = ns("event_set_g"), + label = "Event Set:", + choices = selectChoices, + selected = model_settings$event_set$default, + multiple = FALSE + ) %>% tagAppendAttributes(id = ns("eventset_ctnr"), style = style) + } + } + } + .event_occurrence_fun_g <- function(model_settings) { + if (length(grep("event_occurrence_id", ls_param_group)) > 0) { + if (is.null(model_settings$event_occurrence_id$used_for) || model_settings$event_occurrence_id$used_for == ui_step) { + selectChoices <- lapply(model_settings$event_occurrence_id$options, function(x) { + x$id + }) + names(selectChoices) <- sapply(model_settings$event_occurrence_id$options, function(x) { + x$desc + }) + doHide <- length(model_settings$event_occurrence_id$options) < 2 + style <- if (doHide) "display: none" else "" + selectInput( + inputId = ns("event_occurrence_g"), + label = "Event Occurrence:", + choices = selectChoices, + selected = model_settings$event_occurrence_id$default, + multiple = FALSE + ) %>% tagAppendAttributes(id = ns("eventoccurrence_ctnr"), style = style) + } + } + } + + # string parameters + .string_fun <- function(model_settings) { + if (length(grep("string_parameters", names(model_settings))) > 0) { + x <- grep("string_parameters", names(model_settings)) + lapply(seq_len(length(model_settings[[x]])), function(y) { + if (length(grep(model_settings[[x]][[y]]$name, ls_param_group)) > 0) { + if (!is.null(model_settings[[x]][[y]]$tooltip)) { + tooltip_tex <- model_settings[[x]][[y]]$tooltip + # limit number of characters in tooltip box + if (nchar(tooltip_tex) > 100) { + tooltip_tex <- paste0(substr(tooltip_tex, start = 1, stop = 100), "...") + } + } else { + tooltip_tex <- model_settings[[x]][[y]]$name + } + if (!is.null(model_settings[[x]][[y]]$desc)) { + label_widget <- model_settings[[x]][[y]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]][[y]]$name) + } + if (is.null(model_settings[[x]][[y]]$used_for) || model_settings[[x]][[y]]$used_for == ui_step) { + fluidRow(use_bs_popover(), + column(4, + textInput( + inputId = ns(paste0("string_parameters", y)), + label = div(paste0(label_widget, ":") %>% capitalize_first_letter()), + value = model_settings[[x]][[y]]$default + )), + column(1, + actionButton(ns(paste0("tooltip_string_", y)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) + ) + } + } + }) + } + } + + # list parameters + .list_fun <- function(model_settings) { + if (length(grep("list_parameters", names(model_settings))) > 0) { + x <- grep("list_parameters", names(model_settings)) + lapply(seq_len(length(model_settings[[x]])), function(y) { + if (length(grep(model_settings[[x]][[y]]$name, ls_param_group)) > 0) { + if (!is.null(model_settings[[x]][[y]]$tooltip)) { + tooltip_tex <- model_settings[[x]][[y]]$tooltip + # limit number of characters in tooltip box + if (nchar(tooltip_tex) > 100) { + tooltip_tex <- paste0(substr(tooltip_tex, start = 1, stop = 100), "...") + } + } else { + tooltip_tex <- model_settings[[x]][[y]]$name + } + if (!is.null(model_settings[[x]][[y]]$desc)) { + label_widget <- model_settings[[x]][[y]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]][[y]]$name) + } + if (is.null(model_settings[[x]][[y]]$used_for) || model_settings[[x]][[y]]$used_for == ui_step) { + fluidRow(use_bs_popover(), + column(4, + textInput( + inputId = ns(paste0("list_parameters", y)), + label = paste0(label_widget, ":") %>% capitalize_first_letter(), + value = paste(unlist(model_settings[[x]][[y]]$default), collapse = " , ") + )), + column(1, + actionButton(ns(paste0("tooltip_list_", y)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) + ) + } + } + }) + } + } + + # dictionary parameters + .dictionary_fun <- function(model_settings) { + if (length(grep("dictionary_parameters", names(model_settings))) > 0) { + x <- grep("dictionary_parameters", names(model_settings)) + lapply(seq_len(length(model_settings[[x]])), function(y) { + if (length(grep(model_settings[[x]][[y]]$name, ls_param_group)) > 0) { + if (is.null(model_settings[[x]][[y]]$used_for) || model_settings[[x]][[y]]$used_for == ui_step) { + if (!is.null(model_settings[[x]][[y]]$tooltip)) { + tooltip_tex <- model_settings[[x]][[y]]$tooltip + # limit number of characters in tooltip box + if (nchar(tooltip_tex) > 100) { + tooltip_tex <- paste0(substr(tooltip_tex, start = 1, stop = 100), "...") + } + } else { + tooltip_tex <- model_settings[[x]][[y]]$name + } + lapply(seq_len(length(model_settings[[x]][[y]]$default)), function(z) { + fluidRow(use_bs_popover(), + column(4, + textInput( + inputId = ns(paste0("dictionary_parameters", y, z)), + label = paste(model_settings[[x]][[y]]$desc, names(model_settings[[x]][[y]]$default[z]), + sep = ": "), + value = model_settings[[x]][[y]]$default[[z]] + )), + column(1, + actionButton(ns(paste0("tooltip_dict_", y)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) + ) + }) + } + } + }) + } + } + + # boolean parameters + .boolean_params_fun <- function(model_settings) { + if (length(grep("boolean_parameters", names(model_settings))) > 0) { + x <- grep("boolean_parameters", names(model_settings)) + lapply(seq_len(length(model_settings[[x]])), function(y) { + if (length(grep(model_settings[[x]][[y]]$name, ls_param_group)) > 0) { + if (!is.null(model_settings[[x]][[y]]$tooltip)) { + tooltip_tex <- model_settings[[x]][[y]]$tooltip + # limit number of characters in tooltip box + if (nchar(tooltip_tex) > 100) { + tooltip_tex <- paste0(substr(tooltip_tex, start = 1, stop = 100), "...") + } + } else { + tooltip_tex <- model_settings[[x]][[y]]$name + } + if (!is.null(model_settings[[x]][[y]]$desc)) { + label_widget <- model_settings[[x]][[y]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]][[y]]$name) + } + if (is.null(model_settings[[x]][[y]]$used_for) || model_settings[[x]][[y]]$used_for == ui_step) { + fluidRow(use_bs_popover(), + column(4, + checkboxInput( + inputId = ns(paste0("boolean_parameters", y)), + label = label_widget %>% capitalize_first_letter(), + value = model_settings[[x]]$default + )), + column(1, + actionButton(ns(paste0("tooltip_bool_", y)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) + ) + } + } + }) + } + } + + # float parameters + .float_fun <- function(model_settings) { + if (length(grep("float_parameters", names(model_settings))) > 0) { + x <- grep("float_parameters", names(model_settings)) + lapply(seq_len(length(model_settings[[x]])), function(y) { + if (length(grep(model_settings[[x]][[y]]$name, ls_param_group)) > 0) { + if (!is.null(model_settings[[x]][[y]]$tooltip)) { + tooltip_tex <- model_settings[[x]][[y]]$tooltip + # limit number of characters in tooltip box + if (nchar(tooltip_tex) > 100) { + tooltip_tex <- paste0(substr(tooltip_tex, start = 1, stop = 100), "...") + } + } else { + tooltip_tex <- model_settings[[x]][[y]]$name + } + if (!is.null(model_settings[[x]][[y]]$desc)) { + label_widget <- model_settings[[x]][[y]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]][[y]]$name) + } + if (is.null(model_settings[[x]][[y]]$used_for) || model_settings[[x]][[y]]$used_for == ui_step) { + fluidRow(use_bs_popover(), + column(4, + sliderInput( + inputId = ns(paste0("float_parameters", y)), + label = paste0(label_widget, ":") %>% capitalize_first_letter(), + min = model_settings[[x]][[y]]$min, + max = model_settings[[x]][[y]]$max, + value = model_settings[[x]][[y]]$default + ) + ), + column(1, + actionButton(ns(paste0("tooltip_float_", y)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) + ) + } + } + }) + } + } + + # drop-down parameters + .dropdown_fun <- function(model_settings) { + if (length(grep("dropdown_parameters", names(model_settings))) > 0) { + x <- grep("dropdown_parameters", names(model_settings)) + lapply(seq_len(length(model_settings[[x]])), function(y) { + if (length(grep(model_settings[[x]][[y]]$name, ls_param_group)) > 0) { + if (!is.null(model_settings[[x]][[y]]$tooltip)) { + tooltip_tex <- model_settings[[x]][[y]]$tooltip + # limit number of characters in tooltip box + if (nchar(tooltip_tex) > 100) { + tooltip_tex <- paste0(substr(tooltip_tex, start = 1, stop = 100), "...") + } + } else { + tooltip_tex <- model_settings[[x]][[y]]$name + } + if (!is.null(model_settings[[x]][[y]]$desc)) { + label_widget <- model_settings[[x]][[y]]$desc + } else { + label_widget <- gsub("_", " ", model_settings[[x]][[y]]$name) + } + list_values <- lapply(seq_len(length(model_settings[[x]][[y]]$options)), function (z) { + model_settings[[x]][[y]]$options[[z]]$id + }) + if (is.null(model_settings[[x]][[y]]$used_for) || model_settings[[x]][[y]]$used_for == ui_step) { + fluidRow(use_bs_popover(), + column(4, + selectInput( + inputId = ns(paste0("dropdown_parameters", y)), + label = paste0(label_widget, sep = ": "), + choices = list_values, + selected = model_settings[[x]][[y]]$default, + multiple = TRUE + )), + column(1, + actionButton(ns(paste0("tooltip_dropdown_", y)), "", icon = icon("info"), + style='padding:4px; font-size:80%') %>% + bs_embed_popover( + title = NULL, + content = tooltip_tex + ) + ) + ) + } + } + }) + } + } + + tagList(.event_set_fun_g(model_settings), + .event_occurrence_fun_g(model_settings), + .string_fun(model_settings), + .list_fun(model_settings), + .dictionary_fun(model_settings), + .boolean_params_fun(model_settings), + .float_fun(model_settings), + .dropdown_fun(model_settings)) +} + diff --git a/BFE_RShiny/oasisui/R/output_config_module.R b/BFE_RShiny/oasisui/R/output_config_module.R index 6806b7f2..c41a1354 100644 --- a/BFE_RShiny/oasisui/R/output_config_module.R +++ b/BFE_RShiny/oasisui/R/output_config_module.R @@ -76,7 +76,7 @@ panelModelParams <- function(id) { div( id = ns("configureAnaParamsAdvanced"), align = "left", - numericInput(ns("tinputnoofsample"), label = "Number of Samples:", value = 10), + numericInput(ns("tinputnoofsample"), label = "Number of Samples:", value = 9), numericInput(ns("tinputthreshold"), label = "Loss Threshold:", value = 0), uiOutput(ns("advanced_model_param")), oasisuiButton(inputId = ns("abuttonbasic"), label = "Basic") @@ -721,6 +721,7 @@ def_out_config <- function(input, if (!is.null(tbl_modelsDetails)) { subset_settings <- names(tbl_modelsDetails$model_settings) %in% c("event_set", "event_occurrence_id", + "number_of_samples", "string_parameters", "boolean_parameters", "float_parameters", @@ -746,10 +747,10 @@ def_out_config <- function(input, output$basic_model_param <- renderUI({ basicConfig_funs(session, model_settings) }) - # Advanced model params + updateNumericInput(session, "tinputnoofsample", value = tbl_ana_settings$number_of_samples) output$advanced_model_param <- renderUI({ - advancedConfig_funs(session, model_settings) + advancedConfig_funs(session, model_settings) }) } } @@ -890,6 +891,7 @@ def_out_config <- function(input, # create model settings for analysis settings model_settings <- c(input$event_set, input$event_occurrence, + input$tinputnoofsample, # note that boolean_input is a list, making sure that the result of this c() is a flat list! boolean_input, string_input, @@ -902,6 +904,7 @@ def_out_config <- function(input, # create list/vector of names for model settings names_full_list <- c("event_set", "event_occurrence_id", + "number_of_samples", boolean_name, inputs_name) # remove all NA elements @@ -932,8 +935,8 @@ def_out_config <- function(input, "ui_config_tag" = input$sintag, # potential new tag analysis_id "gul_threshold" = as.integer(input$tinputthreshold), - "model_version_id" = modelData[[tbl_modelsDataNames$version_id]], - "module_supplier_id" = modelData[[tbl_modelsDataNames$supplier_id]], + "model_name_id" = modelData[[tbl_modelsDataNames$model_id]], + "model_supplier_id" = modelData[[tbl_modelsDataNames$supplier_id]], "number_of_samples" = as.integer(input$tinputnoofsample), # potential new tag portfolio_id "prog_id" = as.integer(session$userData$oasisapi$api_return_query_res(query_path = paste("analyses", analysisID(), sep = "/"), diff --git a/BFE_RShiny/oasisui/R/step1_choosePortfolio_module.R b/BFE_RShiny/oasisui/R/step1_choosePortfolio_module.R index 7f1acb8f..0faf54d9 100644 --- a/BFE_RShiny/oasisui/R/step1_choosePortfolio_module.R +++ b/BFE_RShiny/oasisui/R/step1_choosePortfolio_module.R @@ -118,15 +118,23 @@ panelDefinePortfolio <- function(id) { actionButton(inputId = ns("abuttonhidedefpfpanel"), label = NULL, icon = icon("times"), style = "float: right;") ), fluidRow( - column(4, - textInput(inputId = ns("tinputpfName"), label = "Portfolio Name")), - br(), - column(2, + column(12, + textInput(inputId = ns("tinputpfName"), label = "Portfolio Name"), oasisuiButton(ns("abuttonpfsubmit"), "Submit") %>% bs_embed_tooltip(title = defineSingleAna_tooltips$abuttonpfsubmit, placement = "right"), style = "float:right" ) ) ) + # fluidRow( + # column(4, + # textInput(inputId = ns("tinputpfName"), label = "Portfolio Name")), + # br(), + # column(2, + # oasisuiButton(ns("abuttonpfsubmit"), "Submit") %>% + # bs_embed_tooltip(title = defineSingleAna_tooltips$abuttonpfsubmit, placement = "right"), style = "float:right" + # ) + # ) + # ) } diff --git a/BFE_RShiny/oasisui/R/step2_chooseAnalysis_module.R b/BFE_RShiny/oasisui/R/step2_chooseAnalysis_module.R index 1756c09f..be0e21a9 100644 --- a/BFE_RShiny/oasisui/R/step2_chooseAnalysis_module.R +++ b/BFE_RShiny/oasisui/R/step2_chooseAnalysis_module.R @@ -569,13 +569,30 @@ step2_chooseAnalysis <- function(input, output, session, # Show/hide Model Details Panel observeEvent(input$abuttonmodeldetails, { - hide("panelAnalysisDetails") - hide("panelAnalysisLog") - hide("panelAnalysisGenInputs") - hide("panelBuildCustom") - logMessage("showing panelModelDetails") - show("panelModelDetails") - logMessage("showing panelModelDetails") + model_settings <- session$userData$oasisapi$api_return_query_res( + query_path = paste("models", result$modelID, "settings", sep = "/"), + query_method = "GET" + ) + if (length(model_settings$detail) > 0) { + if (model_settings$detail == "Not found.") { + disable("anaName") + showModal(modalDialog( + "No model settings file present for this model", + easyClose = TRUE, + footer = tagList( + modalButton("Ok") + ) + )) + } + } else { + hide("panelAnalysisDetails") + hide("panelAnalysisLog") + hide("panelAnalysisGenInputs") + hide("panelBuildCustom") + logMessage("showing panelModelDetails") + show("panelModelDetails") + logMessage("showing panelModelDetails") + } }) observeEvent(input$abuttonbuildcustom, { @@ -587,7 +604,6 @@ step2_chooseAnalysis <- function(input, output, session, hide("panelModelDetails") disable("anaName") disable("abuttonsubmit") - logMessage("showing panelBuildCustom") }) # Hide panel if model id changes @@ -637,6 +653,7 @@ step2_chooseAnalysis <- function(input, output, session, # Create new Analysis -------------------------------------------------------- observeEvent(input$abuttonsubmit, { if (input$anaName != "") { + post_portfolios_create_analysis <- session$userData$oasisapi$api_body_query(query_path = paste("analyses", sep = "/"), query_body = list(name = input$anaName, portfolio = portfolioID(), @@ -657,7 +674,7 @@ step2_chooseAnalysis <- function(input, output, session, result$analysisNAME <- content(post_portfolios_create_analysis$result)$name logMessage(paste0("Calling api_post_analyses_generate_inputs with id", result$analysisID)) - + # browser() if (length(model_settings) > 0 && !is.null(model_settings$model_configurable) && model_settings$model_configurable && !is.null(sub_modules$buildCustom$fullsettings())) { post_analysis_settings <- session$userData$oasisapi$api_body_query( @@ -669,9 +686,9 @@ step2_chooseAnalysis <- function(input, output, session, ana_settings_step_2 <- list(analysis_settings = c( list( - module_supplier_id = result$supplierID, - model_version_id = result$versionID, - number_of_samples = 0, + model_supplier_id = result$supplierID, + model_name_id = result$modelID, + number_of_samples = 10, model_settings = NULL, gul_output = FALSE, gul_summaries = list(gul_summaries)) @@ -682,8 +699,11 @@ step2_chooseAnalysis <- function(input, output, session, query_body = ana_settings_step_2 ) } - if (post_portfolios_create_analysis$status == "Success" && post_analysis_settings$status == "Success") { + + if (post_portfolios_create_analysis$status == "Success" && post_analysis_settings$status == "Success" && !is.null(model_settings$model_configurable) && model_settings$model_configurable) { + fileids <- as.list(sub_modules$buildCustom$fileids()) + patch_analyses <- session$userData$oasisapi$api_body_query(query_path = paste("analyses", result$analysisID, sep = "/"), query_body = list(complex_model_data_files = fileids), query_method = "PATCH") @@ -693,7 +713,7 @@ step2_chooseAnalysis <- function(input, output, session, query_path = paste("analyses", result$analysisID, "generate_inputs", sep = "/") ) - if (input_generation$status == "Success" && patch_analyses$status == "Success") { + if (input_generation$status == "Success") { oasisuiNotification(type = "message", paste0("Analysis ", input$anaName, " created.")) .reloadAnaData() @@ -744,6 +764,9 @@ step2_chooseAnalysis <- function(input, output, session, query_path = paste("models", result$modelID, "settings", sep = "/"), query_method = "GET" ) + if (!is.null(model_settings$detail) && model_settings$detail == "Not found.") { + disable("anaName") + } if (length(model_settings) > 0 && !is.null(model_settings$model_configurable)) { if (model_settings$model_configurable) { enable("abuttonbuildcustom") diff --git a/BFE_RShiny/oasisui/man/Global_funs.Rd b/BFE_RShiny/oasisui/man/Global_funs.Rd new file mode 100644 index 00000000..47804c3c --- /dev/null +++ b/BFE_RShiny/oasisui/man/Global_funs.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/modelParams_funs.R +\name{Global_funs} +\alias{Global_funs} +\title{Global Model Settings parameters function} +\usage{ +Global_funs(session, model_settings, ui_step) +} +\arguments{ +\item{session}{Current session.} + +\item{model_settings}{Model settings retrieved from the API.} + +\item{ui_step}{Step 2} +} +\value{ +List of UI elements (input/selector widgets). +} +\description{ +Functions for the Build Custom UI. +}