diff --git a/h2/app.r b/h2/app.r index 22bf342..a5f85ed 100644 --- a/h2/app.r +++ b/h2/app.r @@ -32,16 +32,6 @@ ui <- fluidPage(theme = shinythemes::shinytheme("readable"), )) ) ), - tabPanel(title = "Guides", - mainPanel( - style="vertical-align: middle;", - h3("Ready to get started?", - tags$a("View the Guide/Tutorial", href="https://jgcri.github.io/hectorui/articles/Tutorial.html", target="blank")), - br(), - HTML('') - - ) - ), tabPanel(title = "Run Hector", fluidRow( run_ui("run_1") @@ -57,6 +47,16 @@ ui <- fluidPage(theme = shinythemes::shinytheme("readable"), tracking_ui("tracking_1") ) ), + tabPanel(title = "Guides", + mainPanel( + style="vertical-align: middle;", + h3("Ready to get started?", + tags$a("View the Guide/Tutorial", href="https://jgcri.github.io/hectorui/articles/Tutorial.html", target="blank")), + br(), + HTML('') + + ) + ), tabPanel(title = "About") ), hr(), diff --git a/h2/components/functions/func_graph_plots.R b/h2/components/functions/func_graph_plots.R index 90f4855..40bd1f5 100644 --- a/h2/components/functions/func_graph_plots.R +++ b/h2/components/functions/func_graph_plots.R @@ -2,29 +2,13 @@ graph_plots <- function(r6) { - if (r6$save == TRUE) { - #browser() - {ggplot(last(r6$output)) + + {ggplot(r6$output) + geom_line(aes(x = year, y = value, color = Scenario)) + labs(x = "Year", y = last(r6$output)$variable[1], - title = paste0("Run Name: ", last(r6$output)$run[1], "\n", "Variable: ", last(r6$output)$variable[1],"\nPermafrost: ",r6$permafrost)) + + title = paste0("Variable: ", last(r6$output)$variable[1],"\nPermafrost: ",r6$permafrost, "\n")) + theme(legend.position = "bottom")} %>% plotly::ggplotly() - } else if(r6$save == FALSE) { - - {ggplot(r6$no_save_output) + - geom_line(aes(x = year, y = value, color = Scenario)) + - labs(x = "Year", y = r6$no_save_output$variable[1], - title = paste0("Run Name: Unsaved Run\n", "Variable: ", r6$no_save_output$variable[1],"\nPermafrost: ",r6$permafrost))} %>% - plotly::ggplotly() %>% - layout( - legend = list( - orientation = 'h', x = 0 - ) - ) - - } } diff --git a/h2/components/layout/style copy.css b/h2/components/layout/style copy.css index f4d24df..c694571 100644 --- a/h2/components/layout/style copy.css +++ b/h2/components/layout/style copy.css @@ -88,6 +88,14 @@ body font-size: 14px; } + + .bootstrap-switch .bootstrap-switch-handle-off.bootstrap-switch-danger, + .bootstrap-switch .bootstrap-switch-handle-on.bootstrap-switch-danger { + background: #00699d; + color: white; +} + + .navbar-nav { float:none; diff --git a/h2/components/modules/mod_run.R b/h2/components/modules/mod_run.R index 38b40a3..38c18ee 100644 --- a/h2/components/modules/mod_run.R +++ b/h2/components/modules/mod_run.R @@ -28,12 +28,11 @@ run_ui <- function(id) { min = 0, max = 1, value = 1, width = "90%") ) ) - ), - mainPanel(width = 8, + mainPanel( fluidRow( - column(4, - selectInput(ns("variable"), "Choose Output Variable:", + column(8, + selectInput(ns("variable"), "Output Variable:", list("Carbon Cycle" = list("Atmospheric CO2" = CONCENTRATIONS_CO2(), "FFI Emissions" = FFI_EMISSIONS(), "LUC Emissions" = LUC_EMISSIONS()), @@ -49,32 +48,15 @@ run_ui <- function(id) { "RF - Total SO2" = RF_SO2(), "RF - Volcanic Activity" = RF_VOL(), "RF - CH4" = RF_CH4())), - selected = "Atmospheric CO2", multiple = FALSE), - ), - column(3, - materialSwitch(ns("savetoggle"), "Save Run", status = "success") - ), - column(5, - conditionalPanel( - condition = "input.savetoggle == true", - ns = ns, - textInput(ns("run_name"), label = "Run Name", placeholder = "Run 1") - ) - ), - column(2, - dropdownButton(inputId = ns("dropdown"), - icon = icon("gear"), - circle = TRUE, - status = "primary", - dataTableOutput(ns("savetable")), - actionButton(ns("deleteRuns"), "Delete Selected") - ) + selected = "Atmospheric CO2", multiple = FALSE, width = '100%'), ) ), fluidRow( - actionBttn(ns("run"),"Run", color = "primary"), + actionButton(ns("run"), label="Load Graphs", width = '200px', style = "background: #0B3F8F; color: white;"), + downloadButton(ns("downloadData"), label="Download Raw Data", style = "background: #0B3F8F; color: white;") ), fluidRow( + br(), withSpinner(plotlyOutput(ns("graph"))) ) ) @@ -87,51 +69,40 @@ run_server <- function(id, r6) { observe({ - if (input$savetoggle == TRUE) { - r6$save <- TRUE - } else { - r6$save <- FALSE - } - - r6$selected_var <- reactive({input$variable}) - r6$run_name <- reactive({input$run_name}) - r6$ini_file <- reactive({system.file(input$ssp_path,package="hector")}) - r6$time <- reactive({input$time}) - - print("Running...") # in command line - core <- reactive({newcore(r6$ini_file())}) # create core - - # Set parameters using inputs (function to only call setvar once in final version) - if (input$permafrost == TRUE) { - setvar(core(),0,PERMAFROST_C(),865,"Pg C") - r6$permafrost <- "On" - } else if (input$permafrost == FALSE) { - r6$permafrost <- "Off" - } - 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)") - - reset(core()) - run(core()) - - if (r6$save == TRUE) { - - r6$output[[r6$run_name()]] <- fetchvars(core(), r6$time()[1]:r6$time()[2], vars = list(r6$selected_var())) %>% - mutate(run = r6$run_name(), Scenario = names(which(scenarios == input$ssp_path, arr.ind = FALSE))) - - updateSwitchInput(session = session, "savetoggle", value = FALSE) - - } else if (r6$save == FALSE) { - - r6$no_save_output <- fetchvars(core(), r6$time()[1]:r6$time()[2], vars = list(r6$selected_var())) %>% - mutate(Scenario = names(which(scenarios == input$ssp_path, arr.ind = FALSE))) - + runs <- list() + + for(i in 1:length(input$ssp_path)) { + + r6$selected_var <- reactive({input$variable}) + r6$run_name <- reactive({input$run_name}) + r6$ini_file <- reactive({system.file(input$ssp_path[i],package="hector")}) + r6$time <- reactive({input$time}) + + print("Running...") # in command line + core <- reactive({newcore(r6$ini_file())}) # create core + + # Set parameters using inputs (function to only call setvar once in final version) + if (input$permafrost == TRUE) { + setvar(core(),0,PERMAFROST_C(),865,"Pg C") + r6$permafrost <- "On" + } else if (input$permafrost == FALSE) { + r6$permafrost <- "Off" + } + 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)") + + reset(core()) + run(core()) + + runs[[i]] <- fetchvars(core(), r6$time()[1]:r6$time()[2], vars = list(r6$selected_var())) %>% + mutate(Scenario = names(which(scenarios == input$ssp_path[i], arr.ind = FALSE))) } + r6$output <- bind_rows(runs) print("Done") }) %>% @@ -145,31 +116,41 @@ run_server <- function(id, r6) { }) %>% bindEvent(input$run, ignoreNULL = TRUE, ignoreInit = FALSE) - # Clear text input for run save after toggle switch is off - observe({ - updateTextInput(session = session, "run_name", value = NA) - }) %>% - bindEvent(input$savetoggle == FALSE) - - # Create a table to show saved runs in session - observe({ - - # this double allows it to be accessible outside this observe - savetable <<- reactive(tibble("Run Name" = names(r6$output))) - output$savetable <- renderDataTable({savetable()}) - - }) %>% bindEvent(input$run) - - # Create delete entry in saved output list based on user-selected row - observe({ - - delete <- savetable()$`Run Name`[input$savetable_rows_selected] - - r6$output[[delete]] <- NULL - #this works, but the table needs to be updated + #observe({ + + # Download handler for downloading the raw data output from a Hector run. This is activated upon button click. + output$downloadData <- downloadHandler( + filename = function() + { + paste0('HectorUI_Output_', Sys.Date(), '.csv') + }, + + content = function(file) + { + browser() + if(!is.null(r6$output)) + { + + header_text <- paste("File created with Hector UI - https://github.com/JGCRI/hector-ui\n" , + "Model Parameters: " , input$input_paramToggle , "\n", + "Alpha:,", input$alpha, ",Beta:,", input$beta, ",Diff:,", input$diff, + ",ECS:,", input$S, ",Q10:,", input$q10_rh, ",Volc:,", input$volscl, + "\n") + + cat(header_text, file = file) + + } + else + { + shinyalert::shinyalert("No active Hector cores", "Please set at least one of the SSP scenarios to active or upload a custom emissions scenario before downloading.", type = "warning") + } + } + ) - }) %>% bindEvent(input$deleteRuns) + outputOptions(output, "downloadData", suspendWhenHidden = FALSE) + #}) %>% + # bindEvent(input$downloadData, ignoreNULL = TRUE, ignoreInit = FALSE) }) -} \ No newline at end of file +} diff --git a/h2/global.r b/h2/global.r index 15c6809..0d4d02c 100644 --- a/h2/global.r +++ b/h2/global.r @@ -33,11 +33,8 @@ HectorInputs <- R6Class( ini_file = NULL, time = NA, output = NULL, - no_save_output = NULL, - no_save = NULL, run_name = NA, permafrost = NULL, - save = NULL, inputs = NULL, selected_var = NULL, initialize = function(ini_file = system.file("input/hector_ssp245.ini", @@ -45,7 +42,6 @@ HectorInputs <- R6Class( self$ini_file <- ini_file self$time <- time self$output <- list() - self$no_save <- NULL self$run_name <- 1 self$inputs <- list() self$selected_var <- "CO2_concentration"