Skip to content

Commit

Permalink
Merge branch 'remove-savetog' into custom_emissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ciara-donegan authored May 30, 2024
2 parents cad8edf + 31c68f7 commit 5c42863
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 124 deletions.
20 changes: 10 additions & 10 deletions h2/app.r
Original file line number Diff line number Diff line change
Expand Up @@ -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('<iframe width="560" height="315" src="https://www.youtube.com/embed/fBHXS7pjZcI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')

)
),
tabPanel(title = "Run Hector",
fluidRow(
run_ui("run_1")
Expand All @@ -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('<iframe width="560" height="315" src="https://www.youtube.com/embed/fBHXS7pjZcI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')

)
),
tabPanel(title = "About")
),
hr(),
Expand Down
20 changes: 2 additions & 18 deletions h2/components/functions/func_graph_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)

}
}

8 changes: 8 additions & 0 deletions h2/components/layout/style copy.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
165 changes: 73 additions & 92 deletions h2/components/modules/mod_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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")))
)
)
Expand All @@ -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")

}) %>%
Expand All @@ -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)

})
}
}
4 changes: 0 additions & 4 deletions h2/global.r
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,15 @@ 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",
package = "hector")) {
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"
Expand Down

0 comments on commit 5c42863

Please sign in to comment.