diff --git a/h2/app.r b/h2/app.r index 8d684b0..d0678cb 100644 --- a/h2/app.r +++ b/h2/app.r @@ -1,3 +1,4 @@ +source("./global.r") ui <- fluidPage( includeCSS("./components/layout/style copy.css"), @@ -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 diff --git a/h2/components/modules/mod_graph.R b/h2/components/modules/mod_graph.R new file mode 100644 index 0000000..d6a3972 --- /dev/null +++ b/h2/components/modules/mod_graph.R @@ -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) + }) +} + + diff --git a/h2/components/modules/mod_run.R b/h2/components/modules/mod_run.R new file mode 100644 index 0000000..6e6ff11 --- /dev/null +++ b/h2/components/modules/mod_run.R @@ -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 + }) +} \ No newline at end of file diff --git a/h2/components/modules/mod_summary.r b/h2/components/modules/mod_summary.r new file mode 100644 index 0000000..cb64216 --- /dev/null +++ b/h2/components/modules/mod_summary.r @@ -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 + + }) +} \ No newline at end of file diff --git a/h2/components/modules/module_test.r b/h2/components/modules/module_test.r deleted file mode 100644 index e709a35..0000000 --- a/h2/components/modules/module_test.r +++ /dev/null @@ -1,19 +0,0 @@ -# test module - -plot_ui <- function(x) { - verbatimTextOutput((NS(x, "summary"))) -} - -plot_server <- function(x) { - moduleServer(x, function(input, output, session) { - ini_file <- system.file("input/hector_ssp245.ini", package = "hector") - - core <- newcore(ini_file) - - run(core) - - output$summary <- renderPrint({ - fetchvars(core, 2000:2300) - }) - }) -} diff --git a/h2/global.r b/h2/global.r index df4d92b..a113ad1 100644 --- a/h2/global.r +++ b/h2/global.r @@ -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 + } + ) +)