From 51b3b880be6be181ffe0ce3c345b485014e959ed Mon Sep 17 00:00:00 2001 From: Ciara Donegan <82416895+ciara-donegan@users.noreply.github.com> Date: Tue, 28 Nov 2023 11:27:33 -0500 Subject: [PATCH] More variables added for plotting Additional variables available to select, by pulling all of them with fetchvars in mod_run --- h2/app.r | 10 ++++---- h2/components/modules/mod_graph.R | 37 +++++++++++++++++++++++------ h2/components/modules/mod_run.R | 14 +++++++---- h2/components/modules/mod_summary.r | 4 ++-- h2/global.r | 6 +++-- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/h2/app.r b/h2/app.r index d81748f..3d73a49 100644 --- a/h2/app.r +++ b/h2/app.r @@ -16,8 +16,8 @@ ui <- fluidPage( run_ui("run_1"), download_ui("download_1") )), - # column(4, - # summary_ui("summary_1")), + column(4, + summary_ui("summary_1")), column(4, graph_ui("graph_1")) )), @@ -28,9 +28,9 @@ ui <- fluidPage( server <- function(input, output, session) { r6 <- HectorInputs$new() # r6 class - run_server("run_1", r6 = r6, i = i) - # summary_server("summary_1", r6 = r6, i = i) - graph_server("graph_1", r6 = r6, i = i) + run_server("run_1", r6 = r6) + summary_server("summary_1", r6 = r6) + graph_server("graph_1", r6 = r6) download_server("download_1", r6 = r6) } diff --git a/h2/components/modules/mod_graph.R b/h2/components/modules/mod_graph.R index f0e2080..2b46c34 100644 --- a/h2/components/modules/mod_graph.R +++ b/h2/components/modules/mod_graph.R @@ -6,13 +6,34 @@ graph_ui <- function(id) { ns <- NS(id) fluidRow(selectInput(ns("variable"), "Select a variable to plot:", - c("Global Temperature at Surface" = "global_tas", - "CO2 Concentration" = "CO2_concentration")), + list("Carbon Cycle" = list("Atmospheric CO2" = CONCENTRATIONS_CO2(), + "FFI Emissions" = FFI_EMISSIONS(), + "LUC Emissions" = LUC_EMISSIONS()), + "Concentrations" = list("N2O Concentration" = CONCENTRATIONS_N2O()), + "Emissions" = list("Black Carbon Emissions" = EMISSIONS_BC(), + "Organic Carbon Emissions" = EMISSIONS_OC()), + "Forcings" = list("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()))), + # 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"), + # other variables can be found from the fetchvars help page actionButton(ns("plot"), "Plot"), plotlyOutput(ns("graph"))) } -graph_server <- function(id, r6, i) { +graph_server <- function(id, r6) { moduleServer(id, function(input, output, session) { observe({ if (r6$save == TRUE) { @@ -40,9 +61,11 @@ graph_server <- function(id, r6, i) { }) } if (r6$save == FALSE) { - filtered_output <- - filter(r6$no_save, variable == input$variable) + r6$selected_var <- reactive({input$variable}) + filtered_output <- + filter(r6$no_save, variable == r6$selected_var()) + #browser() output$graph <- renderPlotly({ plot_ly( filtered_output, @@ -58,12 +81,12 @@ graph_server <- function(id, r6, i) { ) %>% layout( xaxis = list(title = "Year"), - yaxis = list(title = input$variable), + yaxis = list(title = r6$selected_var()), title = input$variable ) }) } }) %>% - bindEvent(input$plot, ignoreNULL = FALSE, ignoreInit = FALSE) + bindEvent(input$plot, ignoreNULL = TRUE, ignoreInit = FALSE) }) } \ No newline at end of file diff --git a/h2/components/modules/mod_run.R b/h2/components/modules/mod_run.R index 62d77a1..d408478 100644 --- a/h2/components/modules/mod_run.R +++ b/h2/components/modules/mod_run.R @@ -19,8 +19,6 @@ run_ui <- function(id) { shape = "square", width = "80%"), sliderInput(ns("start"), label="Select dates:", min = 1750, max = 2300, value = c(1900,2100), sep="", width = "90%", step=5), - # sliderInput(ns("end"), "Select end date:", - # min = 1750, max = 2300, value = 2300, sep="", width = "90%"), br(), h5("Model Parameters"), sliderInput(ns("alpha"), label="Aerosol forcing scaling factor", # AERO_SCALE() @@ -40,7 +38,7 @@ run_ui <- function(id) { ) } -run_server <- function(id, r6, i) { +run_server <- function(id, r6) { moduleServer(id, function(input, output, session) { observe({ @@ -97,9 +95,9 @@ run_server <- function(id, r6, i) { # Run core reset(core()) run(core()) - + #browser() # Output results - r6$no_save <- fetchvars(core(),r6$start():r6$end()) + 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 } @@ -111,3 +109,9 @@ run_server <- function(id, r6, i) { 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 diff --git a/h2/components/modules/mod_summary.r b/h2/components/modules/mod_summary.r index efd5ac6..2b981bc 100644 --- a/h2/components/modules/mod_summary.r +++ b/h2/components/modules/mod_summary.r @@ -9,7 +9,7 @@ summary_ui <- function(id) { DTOutput(ns("summary"))) } -summary_server <- function(id, r6, i) { +summary_server <- function(id, r6) { moduleServer(id, function(input, output, session) { observe({ if (r6$save == TRUE) { @@ -21,7 +21,7 @@ summary_server <- function(id, r6, i) { output$summary <- renderDT({datatable(hectoroutput)}) } }) %>% - bindEvent(input$print) # run when Print button is clicked + bindEvent(input$print, ignoreNULL = TRUE, ignoreInit = FALSE) # run when Print button is clicked }) } \ No newline at end of file diff --git a/h2/global.r b/h2/global.r index 59ce430..e52f791 100644 --- a/h2/global.r +++ b/h2/global.r @@ -28,10 +28,12 @@ HectorInputs <- R6Class( i = NA, save = NULL, inputs = NULL, + selected_var = NULL, initialize = function(ini_file = system.file("input/hector_ssp245.ini", package = "hector"), start = 2000, - end = 2300) { + end = 2300, + selected_var = "global_tas") { self$ini_file <- ini_file self$start <- start self$end <- end @@ -39,7 +41,7 @@ HectorInputs <- R6Class( #self$no_save <- NULL self$i <- 1 self$inputs <- list() - #self$savetoggle <- FALSE + self$selected_var <- selected_var stopifnot(end > start) #gotta have the start year before the end year } )