Skip to content

Commit

Permalink
Merge branch 'redesign' of https://github.com/JGCRI/hectorui into red…
Browse files Browse the repository at this point in the history
…esign
  • Loading branch information
stephpenn1 committed Aug 29, 2023
2 parents 2d15bbe + 60267ce commit e4067de
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 28 deletions.
35 changes: 28 additions & 7 deletions h2/app.r
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source("./global.r")

ui <- fluidPage(
includeCSS("./components/layout/style copy.css"),
Expand All @@ -8,19 +9,39 @@ ui <- fluidPage(
tabPanel(title = "Guides",
),
tabPanel(title = "Explore Hector",
sidebarPanel(
width = 4
),
h4("Summary"),
plot_ui("x")
# sidebarPanel(
# run_ui("run_1"), # buttons and sliders
# width = 4
# ),
# h4("Summary"),
# summary_ui("summary_1"), # print summary
# graph_ui("graph_1") # plot
fluidRow(
column(4,
wellPanel(
run_ui("run_1")
)
),
column(4,
summary_ui("summary_1")
),
column(4,
graph_ui("graph_1")
)
)
),
tabPanel(title = "About"
)
)
),
)

server <- function(input, output, session) {
plot_server("x")

r6 <- HectorInputs$new()

run_server("run_1", r6=r6)
summary_server("summary_1", r6=r6)
graph_server("graph_1", r6=r6)
}

# Run the application
Expand Down
29 changes: 29 additions & 0 deletions h2/components/modules/mod_graph.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Run Hector given SSP input, start and end years for model run
# Plot function

# Default values set for SSP, start/end years, and selected var for plot

graph_ui <- function(id) {
ns <- NS(id)
fluidRow(
actionButton(ns("plot"),"Plot"),
plotOutput(ns("graph"))
)
}

graph_server <- function(id,r6) {
moduleServer(id, function(input, output, session) {
observe({
#filtered_output <- filter(r6$output,variable=="RF_tot")
output$graph <- renderPlot({
ggplot(r6$output) +
aes(x = year, y = value) +
geom_line() +
facet_wrap(~variable, scales = "free_y")
})
}) %>%
bindEvent(input$plot)
})
}


45 changes: 45 additions & 0 deletions h2/components/modules/mod_run.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Run Hector using R6 module

run_ui <- function(id) {
ns <- NS(id)

tagList(
selectInput(ns("ssp_path"), label="Select SSP:",
choices = list("SSP 1-1.9"="input/hector_ssp119.ini",
"SSP 1-2.6"="input/hector_ssp126.ini",
"SSP 2-4.5"="input/hector_ssp245.ini",
"SSP 3-7.0"="input/hector_ssp370.ini",
"SSP 4-3.4"="input/hector_ssp434.ini",
"SSP 4-6.0"="input/hector_ssp460.ini",
"SSP 5-3.4OS"="input/hector_ssp534-over.ini",
"SSP 5-8.5"="input/hector_ssp585.ini"),
selected = "input/hector_ssp119.ini"),
sliderInput(ns("start"), label="Select start date:",
min = 1750, max = 2300, value = 2000, sep=""),
sliderInput(ns("end"), "Select end date:",
min = 1750, max = 2300, value = 2300, sep=""),
actionButton(ns("run"),"Run Model"),
verbatimTextOutput(ns("done"))
)
}

run_server <- function(id, r6) {
moduleServer(id, function(input, output, session) {
observe({
# store inputs in r6 class
r6$ini_file <- reactive({system.file(input$ssp_path,package="hector")})
r6$start <- reactive({input$start})
r6$end <- reactive({input$end})

# run hector using inputs
#output$done <- renderPrint({"Running..."}) # how to show this, then be replaced by "Done" ?
print("Running...") # in command line
core <- newcore(r6$ini_file())
run(core)
r6$output <- fetchvars(core,r6$start():r6$end())
output$done <- renderPrint({"Done"})
print("Done") # in command line
}) %>%
bindEvent(input$run) # triggers when "Run Model" is clicked
})
}
22 changes: 22 additions & 0 deletions h2/components/modules/mod_summary.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Run Hector given SSP input, start and end years for model run
# Print function

# Default values set for SSP, start/end years, and selected var for plot

summary_ui <- function(id) {
ns <- NS(id)
fluidRow(
actionButton(ns("print"),"Print"),
tableOutput(ns("summary"))
)
}

summary_server <- function(id,r6) {
moduleServer(id, function(input, output, session) {
observe({
output$summary <- renderTable({r6$output})
}) %>%
bindEvent(input$print) # run when Print button is clicked

})
}
19 changes: 0 additions & 19 deletions h2/components/modules/module_test.r

This file was deleted.

28 changes: 26 additions & 2 deletions h2/global.r
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@

library(R6)
library(shiny)
library(hector)
library(dplyr)
library(ggplot2)
library(shinycssloaders)

source("./components/modules/mod_graph.r")
source("./components/modules/mod_run.r")
source("./components/modules/mod_summary.r")

source("./components/modules/module_test.r")
# Define R6 class
HectorInputs <- R6Class(
classname = "HectorInputs",
public = list(
ini_file = NULL,
start = NA,
end = NA,
output = NULL,
initialize = function(ini_file=system.file("input/hector_ssp245.ini",
package="hector"),
start=2000,end=2300) {
self$ini_file <- ini_file
self$start <- start
self$end <- end
stopifnot(end>start) #gotta have the start year before the end year
}
)
)

0 comments on commit e4067de

Please sign in to comment.