Skip to content

Commit

Permalink
More variables added for plotting
Browse files Browse the repository at this point in the history
Additional variables available to select, by pulling all of them with fetchvars in mod_run
  • Loading branch information
ciara-donegan committed Nov 28, 2023
1 parent 799a309 commit 51b3b88
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
10 changes: 5 additions & 5 deletions h2/app.r
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)),
Expand All @@ -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)
}

Expand Down
37 changes: 30 additions & 7 deletions h2/components/modules/mod_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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)
})
}
14 changes: 9 additions & 5 deletions h2/components/modules/mod_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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({

Expand Down Expand Up @@ -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

}
Expand All @@ -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()))
4 changes: 2 additions & 2 deletions h2/components/modules/mod_summary.r
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

})
}
6 changes: 4 additions & 2 deletions h2/global.r
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ 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
self$output <- list()
#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
}
)
Expand Down

0 comments on commit 51b3b88

Please sign in to comment.