From bc7f3f73e97965a189314d417e32a3c975e6c3a1 Mon Sep 17 00:00:00 2001 From: Ciara Donegan <82416895+ciara-donegan@users.noreply.github.com> Date: Tue, 5 Dec 2023 14:46:44 -0500 Subject: [PATCH] All vars added, save toggle somewhat working All vars from current version of HectorUI added. Save toggle somewhat works, but the unsaved run also is triggered each time even if save toggle is on --- h2/components/modules/mod_graph.R | 69 ++++++++-- h2/components/modules/mod_run.R | 206 +++++++++++++++++++--------- h2/components/modules/mod_summary.r | 3 + h2/global.r | 135 ++++++++++++++++++ 4 files changed, 334 insertions(+), 79 deletions(-) diff --git a/h2/components/modules/mod_graph.R b/h2/components/modules/mod_graph.R index 2b46c34..842ab03 100644 --- a/h2/components/modules/mod_graph.R +++ b/h2/components/modules/mod_graph.R @@ -7,6 +7,7 @@ graph_ui <- function(id) { ns <- NS(id) fluidRow(selectInput(ns("variable"), "Select a variable to plot:", list("Carbon Cycle" = list("Atmospheric CO2" = CONCENTRATIONS_CO2(), + "Atmospheric Carbon Pool" = ATMOSPHERIC_CO2(), # i think this is the right var? "FFI Emissions" = FFI_EMISSIONS(), "LUC Emissions" = LUC_EMISSIONS()), "Concentrations" = list("N2O Concentration" = CONCENTRATIONS_N2O()), @@ -20,14 +21,38 @@ graph_ui <- function(id) { "RF - Organic Carbon" = RF_OC(), "RF - Total SO2" = RF_SO2(), "RF - Volcanic Activity" = RF_VOL(), - "RF - CH4" = RF_CH4()))), - # selectInput(ns("variable"), "Select a variable to plot:", - # c("Global Mean Temperature" = "global_tas", - # "Atmospheric CO2" = "CO2_concentration", - # "RF - Total" = "RF_tot", - # "RF - CO2" = "RF_CO2", - # "Atmospheric N2O" = CONCENTRATIONS_N2O()), - # selected = "global_tas"), + "RF - CH4" = RF_CH4()), + "Halocarbon Forcings" = list("CF4 Forcing"="RF_CF4", #function doesn't give the correct output? + "C2F6 Forcing"="RF_C2F6", + "HFC-23 Forcing"="RF_HFC23", + "HFC-4310 Forcing"="RF_HFC4310", + "HFC-125 Forcing"="RF_HFC125", + "HFC-143a Forcing"="RF_HFC143a", + "HFC-245fa Forcing"="RF_HFC245fa", + "SF6 Forcing"="RF_SF6", + "CFC-11 Forcing"="RF_CFC11", + "CFC-12 Forcing"="RF_CFC12", + "CFC-113 Forcing"="RF_CFC113", + "CFC-114 Forcing"="RF_CFC114", + "CFC-115 Forcing"="RF_CFC115", + "CCl4 Forcing"="RF_CCl4", + "CH3CCl3 Forcing"="RF_CH3CCl3", + "Halon-1211 Forcing"="RF_halon1211", + "Halon-1301 Forcing"="RF_halon1301", + "Halon-2402 Forcing"="RF_halon2402", + "CH3Cl Forcing"="RF_CH3Cl", + "CH3Br Forcing"="RF_CH3Br"), + "Methane" = list("Atmospheric CH4" = CONCENTRATIONS_CH4(), + "CH4 Emissions" = EMISSIONS_CH4()), + "SO2" = list("Anthropogenic SO2"=EMISSIONS_SO2(), + "Volcanic SO2"=VOLCANIC_SO2()), + "Temperature" = list("Global Mean Temp" = GLOBAL_TAS(), + "Equilibrium Global Temp" = GMST(), # i think? + "Ocean Surface Temp" = SST(), + "Ocean Air Temp" = OCEAN_TAS(), + "Heat Flux - Mixed Layer Ocean" = FLUX_MIXED(), + "Heat Flux - Interior Layer Ocean" = FLUX_INTERIOR(), + "Total Heat Flux - Ocean" = HEAT_FLUX()))), # other variables can be found from the fetchvars help page actionButton(ns("plot"), "Plot"), plotlyOutput(ns("graph"))) @@ -37,6 +62,13 @@ graph_server <- function(id, r6) { moduleServer(id, function(input, output, session) { observe({ if (r6$save == TRUE) { + + # Get labels given input + key <- reactive({input$variable}) + title <- title[[key()]] + ylabel <- units[[key()]] + + # Filter data for selected variable filtered_output <- filter(r6$output[[r6$i()]], variable == input$variable) @@ -55,17 +87,24 @@ graph_server <- function(id, r6) { ) %>% layout( xaxis = list(title = "Year"), - yaxis = list(title = "Global Temperature (C)"), - title = "Global Temperature at Surface" + yaxis = list(title = ylabel), + title = title ) }) } + if (r6$save == FALSE) { r6$selected_var <- reactive({input$variable}) + # Get labels given input + key <- reactive({input$variable}) + title <- title[[key()]] + ylabel <- units[[key()]] + + # Filter data for selected variable filtered_output <- filter(r6$no_save, variable == r6$selected_var()) - #browser() + output$graph <- renderPlotly({ plot_ly( filtered_output, @@ -81,12 +120,14 @@ graph_server <- function(id, r6) { ) %>% layout( xaxis = list(title = "Year"), - yaxis = list(title = r6$selected_var()), - title = input$variable + yaxis = list(title = ylabel), + title = title ) }) } }) %>% bindEvent(input$plot, ignoreNULL = TRUE, ignoreInit = FALSE) }) -} \ No newline at end of file +} + +# add reset variables button \ No newline at end of file diff --git a/h2/components/modules/mod_run.R b/h2/components/modules/mod_run.R index d408478..69dca7e 100644 --- a/h2/components/modules/mod_run.R +++ b/h2/components/modules/mod_run.R @@ -40,78 +40,154 @@ run_ui <- function(id) { run_server <- function(id, r6) { moduleServer(id, function(input, output, session) { + ns <- NS(id) + observe({ + + toggle <- reactive({input$savetoggle}) - if (input$savetoggle == TRUE) { - shinyalert( - html = TRUE, - type = "input", - inputType = "text", - inputPlaceholder = "run name", - showConfirmButton = TRUE, - confirmButtonText = "OK", - showCancelButton = TRUE, - cancelButtonText = "Cancel", - closeOnClickOutside = TRUE - ) + if (toggle() == TRUE) { + # shinyalert( + # text = tagList( + # textInput("run_name,","Input run name:",placeholder="run name"), + # actionButton("OK","OK") + # ), + # html = TRUE, + # #type = "input", + # #inputType = "text", + # #inputPlaceholder = "run name", + # showConfirmButton = TRUE, + # confirmButtonText = "OK", + # showCancelButton = TRUE, + # cancelButtonText = "Cancel", + # closeOnClickOutside = FALSE + # ) - Sys.sleep(5) + observe({ + showModal(modalDialog( + textInput(ns("run_name"),"Enter run name:",value="run name"), + easyClose=TRUE, + footer=tagList( + actionButton(ns("ok"), "OK"), + modalButton("Cancel") + ) + )) + }) %>% + bindEvent(input$run, ignoreNULL = TRUE, ignoreInit = TRUE) - r6$i <- reactive({input$shinyalert}) + observe({ + # Close input modal + removeModal() + + #browser() + + r6$i <- reactive({input$run_name}) + + r6$ini_file <- reactive({system.file(input$ssp_path,package="hector")}) + r6$start <- reactive({input$start[1]}) + r6$end <- reactive({input$start[2]}) + + print("Running...") # in command line + + # Create core + core <- reactive({newcore(r6$ini_file(),name=input$core_name)}) + + # Set parameters using inputs (function to only call setvar once in final version) + setvar(core(),NA,AERO_SCALE(),input$alpha,"(unitless)") + setvar(core(),NA,BETA(),input$beta,"(unitless)") + setvar(core(),NA,DIFFUSIVITY(),input$diff,"cm2/s") + setvar(core(),NA,ECS(),input$S,"degC") + setvar(core(),NA,Q10_RH(),input$q10_rh,"(unitless)") + setvar(core(),NA,VOLCANIC_SCALE(),input$volscl,"(unitless)") + + # Run core + reset(core()) + run(core()) + + r6$output[[r6$i()]] <- fetchvars(core(),r6$start():r6$end(),vars= + list(CONCENTRATIONS_CO2(),FFI_EMISSIONS(), + LUC_EMISSIONS(),CONCENTRATIONS_N2O(), + EMISSIONS_BC(),EMISSIONS_OC(),RF_TOTAL(), + RF_ALBEDO(),RF_N2O(),RF_CO2(),RF_BC(), + RF_OC(),RF_SO2(),RF_CH4(),RF_VOL(), + RF_CF4(),RF_C2F6(),RF_HFC23(), + RF_HFC4310(),RF_HFC125(),RF_HFC143A(), + RF_HFC245FA(),RF_SF6(),RF_CFC11(), + RF_CFC12(),RF_CFC113(),RF_CFC114(), + RF_CFC115(),RF_CCL4(),RF_CH3CCL3(), + RF_HALON1211(),RF_HALON1301(), + RF_HALON2402(),RF_CH3CL(),RF_CH3BR(), + CONCENTRATIONS_CH4(),EMISSIONS_CH4(), + EMISSIONS_SO2(),VOLCANIC_SO2(), + GLOBAL_TAS(),SST(),OCEAN_TAS(), + FLUX_MIXED(),FLUX_INTERIOR(),HEAT_FLUX(), + ATMOSPHERIC_CO2(),GMST() + )) %>% + mutate(run=r6$i()) + + #output$done <- renderPrint({r6$i()}) + r6$save <- TRUE + + print("Done") # in console + + }) %>% + bindEvent(input$ok) - r6$ini_file <- reactive({system.file(input$ssp_path,package="hector")}) - r6$start <- reactive({input$start}) - r6$end <- reactive({input$end}) + } else if (toggle() == FALSE) { - print("Running...") # in command line - core <- reactive({newcore(r6$ini_file(),name=input$core_name)}) - run(core()) - - r6$output[[r6$i()]] <- fetchvars(core(),r6$start():r6$end()) %>% mutate(run=r6$i()) - output$done <- renderPrint({r6$i()}) #print run number - r6$save <- TRUE - - } else { - - r6$i <- reactive({input$shinyalert}) - - r6$ini_file <- reactive({system.file(input$ssp_path,package="hector")}) - r6$start <- reactive({input$start[1]}) - r6$end <- reactive({input$start[2]}) - - print("Running...") # in command line - - # Create core - core <- reactive({newcore(r6$ini_file(),name=input$core_name)}) - - # Set parameters using inputs (function to only call setvar once in final version) - setvar(core(),NA,AERO_SCALE(),input$alpha,"(unitless)") - setvar(core(),NA,BETA(),input$beta,"(unitless)") - setvar(core(),NA,DIFFUSIVITY(),input$diff,"cm2/s") - setvar(core(),NA,ECS(),input$S,"degC") - setvar(core(),NA,Q10_RH(),input$q10_rh,"(unitless)") - setvar(core(),NA,VOLCANIC_SCALE(),input$volscl,"(unitless)") - - # Run core - reset(core()) - run(core()) - #browser() - # Output results - r6$no_save <- fetchvars(core(),r6$start():r6$end(),vars=list(CONCENTRATIONS_CO2(),FFI_EMISSIONS(),LUC_EMISSIONS(),CONCENTRATIONS_N2O(),EMISSIONS_BC(),EMISSIONS_OC(),RF_TOTAL(),RF_ALBEDO(),RF_N2O(),RF_CO2(),RF_BC(),RF_OC(),RF_SO2(),RF_CH4(),RF_VOL())) - r6$save <- FALSE + observe({ + browser() + r6$i <- reactive({input$run_name}) + + r6$ini_file <- reactive({system.file(input$ssp_path,package="hector")}) + r6$start <- reactive({input$start[1]}) + r6$end <- reactive({input$start[2]}) + + print("Running...") # in console + + # Create core + core <- reactive({newcore(r6$ini_file(),name=input$core_name)}) + + # Set parameters using inputs (function to only call setvar once in final version) + setvar(core(),NA,AERO_SCALE(),input$alpha,"(unitless)") + setvar(core(),NA,BETA(),input$beta,"(unitless)") + setvar(core(),NA,DIFFUSIVITY(),input$diff,"cm2/s") + setvar(core(),NA,ECS(),input$S,"degC") + setvar(core(),NA,Q10_RH(),input$q10_rh,"(unitless)") + setvar(core(),NA,VOLCANIC_SCALE(),input$volscl,"(unitless)") + + # Run core + reset(core()) + run(core()) + + # Output results + r6$no_save <- fetchvars(core(),r6$start():r6$end(),vars= + list(CONCENTRATIONS_CO2(),FFI_EMISSIONS(), + LUC_EMISSIONS(),CONCENTRATIONS_N2O(), + EMISSIONS_BC(),EMISSIONS_OC(),RF_TOTAL(), + RF_ALBEDO(),RF_N2O(),RF_CO2(),RF_BC(), + RF_OC(),RF_SO2(),RF_CH4(),RF_VOL(), + RF_CF4(),RF_C2F6(),RF_HFC23(), + RF_HFC4310(),RF_HFC125(),RF_HFC143A(), + RF_HFC245FA(),RF_SF6(),RF_CFC11(), + RF_CFC12(),RF_CFC113(),RF_CFC114(), + RF_CFC115(),RF_CCL4(),RF_CH3CCL3(), + RF_HALON1211(),RF_HALON1301(), + RF_HALON2402(),RF_CH3CL(),RF_CH3BR(), + CONCENTRATIONS_CH4(),EMISSIONS_CH4(), + EMISSIONS_SO2(),VOLCANIC_SO2(), + GLOBAL_TAS(),SST(),OCEAN_TAS(), + FLUX_MIXED(),FLUX_INTERIOR(),HEAT_FLUX(), + ATMOSPHERIC_CO2(),GMST() + )) + r6$save <- FALSE + + print("Done") # in console + }) %>% + bindEvent(input$run, ignoreNULL = TRUE, ignoreInit = FALSE) } - - print("Done") - #updateMaterialSwitch(session = i, inputId = "savetoggle", value = FALSE) #not working right now - }) %>% - bindEvent(input$run, ignoreNULL = FALSE, ignoreInit = FALSE) # runs when app opens + }) }) -} - -# might be worth it to just run the core with all selectable variables. how much time would that add? -# issue seems to be that mod_run goes first, so input$variable just doesn't exist yet... maybe having -# that module containing all choices is a good idea - -# fetchvars(core,1745:2300,vars=list(CONCENTRATIONS_CO2(),FFI_EMISSIONS(),LUC_EMISSIONS(),CONCENTRATIONS_N2O,EMISSIONS_BC(),EMISSIONS_OC(),RF_TOTAL(),RF_ALBEDO(),RF_N2O(),RF_CO2(),RF_BC(),RF_OC(),RF_SO2(),RF_CH4(),RF_VOL())) \ No newline at end of file +} \ No newline at end of file diff --git a/h2/components/modules/mod_summary.r b/h2/components/modules/mod_summary.r index 2b981bc..6c477c5 100644 --- a/h2/components/modules/mod_summary.r +++ b/h2/components/modules/mod_summary.r @@ -18,6 +18,9 @@ summary_server <- function(id, r6) { } if (r6$save == FALSE) { hectoroutput <- r6$no_save + #filtered_output <- + # filter(r6$no_save, variable == r6$selected_var()) + output$summary <- renderDT({datatable(hectoroutput)}) } }) %>% diff --git a/h2/global.r b/h2/global.r index e52f791..4315b2b 100644 --- a/h2/global.r +++ b/h2/global.r @@ -45,4 +45,139 @@ HectorInputs <- R6Class( stopifnot(end > start) #gotta have the start year before the end year } ) +) + +# labels dictionary +# title <- list("Atmospheric CO2" = CONCENTRATIONS_CO2(), +# "FFI Emissions" = FFI_EMISSIONS(), +# "LUC Emissions" = LUC_EMISSIONS(), +# "N2O Concentration" = CONCENTRATIONS_N2O(), +# "Black Carbon Emissions" = EMISSIONS_BC(), +# "Organic Carbon Emissions" = EMISSIONS_OC(), +# "RF - Total" = RF_TOTAL(), +# "RF - Albedo" = RF_ALBEDO(), +# "RF - CO2" = RF_CO2(), +# "RF - N2O" = RF_N2O(), +# "RF - Black Carbon" = RF_BC(), +# "RF - Organic Carbon" = RF_OC(), +# "RF - Total SO2" = RF_SO2(), +# "RF - Volcanic Activity" = RF_VOL(), +# "RF - CH4" = RF_CH4(), +# "CF4 Forcing"="RF_CF4", #function doesn't give the correct output? +# "C2F6 Forcing"="RF_C2F6", +# "HFC-23 Forcing"="RF_HFC23", +# "HFC-4310 Forcing"="RF_HFC4310", +# "HFC-125 Forcing"="RF_HFC125", +# "HFC-143a Forcing"="RF_HFC143A", +# "HFC-245fa Forcing"="RF_HFC245FA", +# "SF6 Forcing"="RF_SF6", +# "CFC-11 Forcing"="RF_CFC11", +# "CFC-12 Forcing"="RF_CFC12", +# "CFC-113 Forcing"="RF_CFC113", +# "CFC-114 Forcing"="RF_CFC114", +# "CFC-115 Forcing"="RF_CFC115", +# "CCl4 Forcing"="RF_CCl4", +# "CH3CCl3 Forcing"="RF_CH3CCl3", +# "Halon-1211 Forcing"="RF_halon1211", +# "Halon-1301 Forcing"="RF_halon1301", +# "Halon-2402 Forcing"="RF_halon2402", +# "CH3Cl Forcing"="RF_CH3Cl", +# "CH3Br Forcing"="RF_CH3Br") + +title <- list("CO2_concentration" = "Atmospheric CO2", + "atmos_co2" = "Atmospheric Carbon Pool", + "ffi_emissions" = "FFI Emissions", + "luc_emissions" = "LUC Emissions", + "N2O_concentration" = "N2O Concentration", + "BC_emissions" = "Black Carbon Emissions", + "OC_emissions" = "Organic Carbon Emissions", + "RF_tot" = "RF - Total", + "RF_albedo" = "RF - Albedo", + "RF_CO2" = "RF - CO2", + "RF_N2O" = "RF - N2O", + "RF_BC" = "RF - Black Carbon", + "RF_OC" = "RF - Organic Carbon", + "RF_SO2" = "RF - Total SO2", + "RF_VOL" = "RF - Volcanic Activity", + "RF_CH4" = "RF - CH4", + "RF_CF4" = "CF4 Forcing", + "RF_C2F6" = "C2F6 Forcing", + "RF_HFC23" = "HFC-23 Forcing", + "RF_HFC4310" = "HFC-4310 Forcing", + "RF_HFC125" = "HFC-125 Forcing", + "RF_HFC143a" = "HFC-143a Forcing", + "RF_HFC245fa" = "HFC-245fa Forcing", + "RF_SF6" = "SF6 Forcing", + "RF_CFC11" = "CFC-11 Forcing", + "RF_CFC12" = "CFC-12 Forcing", + "RF_CFC113" = "CFC-113 Forcing", + "RF_CFC114" = "CFC-114 Forcing", + "RF_CFC115" = "CFC-115 Forcing", + "RF_CCl4" = "CCl4 Forcing", + "RF_CH3CCl3" = "CH3CCl3 Forcing", + "RF_halon1211" = "Halon-1211 Forcing", + "RF_halon1301" = "Halon-1301 Forcing", + "RF_halon2402" = "Halon-2402 Forcing", + "RF_CH3Cl" = "CH3Cl Forcing", + "RF_CH3Br" = "CH3Br Forcing", + "CH4_concentration" = "Atmospheric CH4", + "CH4_emissions" = "CH4 Emissions", + "SO2_emissions" = "Anthropogenic SO2", + "SV" = "Volcanic SO2", + "global_tas" = "Global Mean Temperature", + "gmst" = "Equilibrium Global Temperature", + "sst" = "Ocean Surface Temperature", + "ocean_tas" = "Ocean Air Temperature", + "heatflux_mixed" = "Heat Flux - Mixed Layer Ocean", + "heatflux_interior" = "Heat Flux - Interior Layer Ocean", + "heatflux" = "Total Heat Flux - Ocean" +) + +units <- list("CO2_concentration" = "DegC", + "atmos_co2" = "Pg C", + "ffi_emissions" = "Pg C/yr", + "luc_emissions" = "Pg C/yr", + "N2O_concentration" = "ppbv N2O", + "BC_emissions" = "Tg", + "OC_emissions" = "Tg", + "RF_tot" = "RF - Total", + "RF_albedo" = "W/m2", + "RF_CO2" = "W/m2", + "RF_N2O" = "W/m2", + "RF_BC" = "W/m2", + "RF_OC" = "W/m2", + "RF_SO2" = "W/m2", + "RF_VOL" = "W/m2", + "RF_CH4" = "W/m2", + "RF_CF4" = "W/m2", + "RF_C2F6" = "W/m2", + "RF_HFC23" = "W/m2", + "RF_HFC4310" = "W/m2", + "RF_HFC125" = "W/m2", + "RF_HFC143a" = "W/m2", + "RF_HFC245fa" = "W/m2", + "RF_SF6" = "W/m2", + "RF_CFC11" = "W/m2", + "RF_CFC12" = "W/m2", + "RF_CFC113" = "W/m2", + "RF_CFC114" = "W/m2", + "RF_CFC115" = "W/m2", + "RF_CCl4" = "W/m2", + "RF_CH3CCl3" = "W/m2", + "RF_halon1211" = "W/m2", + "RF_halon1301" = "W/m2", + "RF_halon2402" = "W/m2", + "RF_CH3Cl" = "W/m2", + "RF_CH3Br" = "W/m2", + "CH4_concentration" = "ppbv CH4", + "CH4_emissions" = "Tg CH4", + "SO2_emissions" = "Gg SO2", + "SV" = "W/m2", + "global_tas" = "DegC", + "gmst" = "DegC", + "sst" = "DegC", + "ocean_tas" = "DegC", + "heatflux_mixed" = "W/m2", + "heatflux_interior" = "W/m2", + "heatflux" = "W/m2" ) \ No newline at end of file