diff --git a/h2/app.r b/h2/app.r index b0dc0d2..da636fb 100644 --- a/h2/app.r +++ b/h2/app.r @@ -12,7 +12,6 @@ ui <- fluidPage( tabPanel(title = "Explore Hector", fluidRow( run_ui("run_1"), - #download_ui("download_1"), ) ), tabPanel(title = "Carbon Tracking", @@ -26,7 +25,7 @@ ui <- fluidPage( server <- function(input, output, session) { r6 <- HectorInputs$new() # r6 class - r6_tracking <- HectorInputs$new() # r6 class for carbon tracking + r6_tracking <- HectorInputs$new() # separate r6 class for carbon tracking run_server("run_1", r6 = r6) summary_server("summary_1", r6 = r6) diff --git a/h2/components/modules/mod_tracking.R b/h2/components/modules/mod_tracking.R index b991517..ea94c48 100644 --- a/h2/components/modules/mod_tracking.R +++ b/h2/components/modules/mod_tracking.R @@ -31,40 +31,22 @@ tracking_ui <- function(id) { "Soil"="Soil"), selected="Atmosphere"), - # prettyRadioButtons(ns("plotSelect"), label="Select plot to view:", - # choices = list("Area Plot"=1, - # "Animated Bar Plot"=2), - # selected=1), - prettyRadioButtons(ns("ff"), label="Toggle fossil fuels:", choices = list("On"=1,"Off"=2)), bsPopover(ns("ff"), title="",content="Select whether you want fossil fuels to be included (On), or only non-anthropogenic sources of carbon (Off).", placement = "top", trigger = "hover", options = NULL), - -# conditionalPanel( -# condition = "input.plotSelect == 1", - prettyRadioButtons(ns("view"), label="View:", - choices = list("Carbon Amount"=1, - "Carbon Fraction"=2), - selected = 1), - bsPopover(ns("view"), title="",content="Select whether you want to view the total amounts of carbon in each pool (Carbon Amount), or the fraction each pool has of the total amount in the system (Carbon Fraction).", - placement = "top", trigger = "hover", options = NULL), -# ns = NS("tracking_1") -# ), + prettyRadioButtons(ns("view"), label="View:", + choices = list("Carbon Amount"=1, + "Carbon Fraction"=2), + selected = 1), + bsPopover(ns("view"), title="",content="Select whether you want to view the total amounts of carbon in each pool (Carbon Amount), or the fraction each pool has of the total amount in the system (Carbon Fraction).", + placement = "top", trigger = "hover", options = NULL), actionButton(ns("generate"),"Generate"), downloadButton(ns("download"),"Download Plots"), ), mainPanel( -# conditionalPanel( -# condition = "input.plotSelect == 1", - withSpinner(plotlyOutput(ns("fig"))), -# ns = NS("tracking_1") -# ), -# conditionalPanel( -# condition = "input.plotSelect == 2", - withSpinner(imageOutput(ns("gif"))), -# ns = NS("tracking_1") -# ) + withSpinner(plotOutput(ns("fig"))), + imageOutput(ns("gif")), ) ) } @@ -72,10 +54,6 @@ tracking_ui <- function(id) { tracking_server <- function(id) { moduleServer(id, function(input, output, session) { - # Get shinycssloaders to not appear until plots are generated - #output$fig <- renderPlotly(NULL) - #output$gif <- renderImage(NULL) - observe({ # Run Hector w/ carbon tracking ini_file <- reactive({system.file(input$ssp_path,package="hector")}) @@ -102,7 +80,7 @@ tracking_server <- function(id) { ## filter df to just selected pool selectedPool <- reactive({input$pool}) df <- filter(df,pool_name==selectedPool()) - # filter out fossil fuels option + # filter out fossil fuels if selected if (input$ff == 2) { df <- subset(df, source_name!="Fossil Fuels") } @@ -125,102 +103,25 @@ tracking_server <- function(id) { frac_lbl = paste0(format(round(source_fraction,2),nsmall=2))) %>% group_by(source_name) %>% ungroup() - - # Plotting -# plotSelect <- reactive({input$plotSelect}) # area or bar - #view <- reactive({input$view}) # fraction or amount - #view <- as.numeric(view()) - #browser() - - # Area plot - # if (plotSelect() == 1) { # Carbon amount if (input$view == 1) { area_plot <- ggplot(df, aes(x=year,y=source_amt,fill=source_name)) + geom_area(stat="identity") + - scale_fill_viridis_d() + + scale_fill_viridis_d(name="Source") + scale_color_viridis_d() + - ggtitle(paste0("Amount of Carbon in ", selectedPool(), " by Source")) + + ggtitle(paste0(selectedPool(), " Carbon Amount by Source")) + xlab("") + - ylab("Carbon Pool (Pg C)") + ylab("Carbon Pool (Pg C)") + + theme(plot.title = element_text(size=20,face="bold"), + legend.position="bottom", + plot.margin = margin(1,1,1,1,"cm")) # save as file - ggsave("outfile_area.png",plot=area_plot,device="png") + ggsave("outfile_area.jpeg",plot=area_plot,device="jpeg", + dpi=72,width=800,height=500,units="px") - # print("Generating plot...") - # area_plot <- - # plot_ly( - # filter(df, source_name == "HL Ocean"), - # x = ~ year, - # y = ~ source_amt, - # name = "HL Ocean", - # type = "scatter", - # mode = "none", - # stackgroup = "one", - # fillcolor = "#2C728EFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "LL Ocean"), - # y = ~ source_amt, - # name = "LL Ocean", - # fillcolor = "#3B528BFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Intermediate Ocean"), - # y = ~ source_amt, - # name = "Intermediate Ocean", - # fillcolor = "#472D7BFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Deep Ocean"), - # y = ~ source_amt, - # name = "Deep Ocean", - # fillcolor = "#440154FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Atmosphere"), - # y = ~ source_amt, - # name = "Atmosphere", - # fillcolor = "#21908CFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Vegetation"), - # y = ~ source_amt, - # name = "Vegetation", - # fillcolor = "#27AD81FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Soil"), - # y = ~ source_amt, - # name = "Soil", - # fillcolor = "#5DC863FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Detritus"), - # y = ~ source_amt, - # name = "Detritus", - # fillcolor = "#AADC32FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Fossil Fuels"), - # y = ~ source_amt, - # name = "Fossil Fuels", - # fillcolor = "#FDE725FF" - # ) - # area_plot <- - # area_plot %>% layout(title = paste0("Amount of Carbon in ", selectedPool(), " by Source"), - # xaxis = list(title=""), - # yaxis = list(title="Carbon Pool (Pg C)")) } # Carbon fraction @@ -232,94 +133,21 @@ tracking_server <- function(id) { geom_area(stat="identity") + scale_fill_viridis_d() + scale_color_viridis_d() + - ggtitle(paste0("Fraction of Carbon in ", selectedPool(), " by Source")) + + ggtitle(paste0(selectedPool(), " Carbon Fraction by Source")) + xlab("") + - ylab("Carbon Pool (Fraction)") + ylab("Carbon Pool (Fraction)") + + theme(plot.title = element_text(size=20,face="bold"), + legend.position="bottom") # save as file - ggsave("outfile_area.svg",plot=area_plot,device="svg", - width=500,height=500,units="px") + ggsave("outfile_area.jpeg",plot=area_plot,device="jpeg", + width=800,height=500,units="px") - # - # plot_ly( - # filter(df, source_name == "HL Ocean"), - # x = ~ year, - # y = ~ source_fraction, - # name = "HL Ocean", - # type = "scatter", - # mode = "none", - # stackgroup = "one", - # fillcolor = "#2C728EFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "LL Ocean"), - # y = ~ source_fraction, - # name = "LL Ocean", - # fillcolor = "#3B528BFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Intermediate Ocean"), - # y = ~ source_fraction, - # name = "Intermediate Ocean", - # fillcolor = "#472D7BFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Deep Ocean"), - # y = ~ source_fraction, - # name = "Deep Ocean", - # fillcolor = "#440154FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Atmosphere"), - # y = ~ source_fraction, - # name = "Atmosphere", - # fillcolor = "#21908CFF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Vegetation"), - # y = ~ source_fraction, - # name = "Vegetation", - # fillcolor = "#27AD81FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Soil"), - # y = ~ source_fraction, - # name = "Soil", - # fillcolor = "#5DC863FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Detritus"), - # y = ~ source_fraction, - # name = "Detritus", - # fillcolor = "#AADC32FF" - # ) - # area_plot <- - # area_plot %>% add_trace( - # data = filter(df, source_name == "Fossil Fuels"), - # y = ~ source_fraction, - # name = "Fossil Fuels", - # fillcolor = "#FDE725FF" - # ) - # area_plot <- - # area_plot %>% layout(title = paste0("Fraction of Carbon in ", selectedPool(), " by Source"), - # xaxis = list(title=""), - # yaxis = list(title="Carbon Pool (Fraction)")) } - output$fig <- renderPlotly(area_plot) - - # } else if (plotSelect() == 2) { + output$fig <- renderPlot(area_plot) output$gif <- renderImage({ - # Temp file to save output - #outfile <- tempfile(fileext='.gif') # Make animation p <- ggplot(df,aes(fill=source_name,color=source_name, @@ -359,10 +187,9 @@ tracking_server <- function(id) { list(src = 'outfile_bar.gif', contentType = 'image/gif' # width = 800, - # height = 300, + # height = 500, # alt = "An animation tracking the sources of carbon in a chosen pool" )}, deleteFile = FALSE) -# } }) %>% bindEvent(input$generate) @@ -371,7 +198,7 @@ tracking_server <- function(id) { output$download <- downloadHandler( filename="myplots.zip", content=function(file){ - zip(file,files=c('outfile_area.png','outfile_bar.gif')) + zip(file,files=c('outfile_area.jpeg','outfile_bar.gif')) } ) diff --git a/h2/global.r b/h2/global.r index e8dfb45..49a2a01 100644 --- a/h2/global.r +++ b/h2/global.r @@ -11,6 +11,7 @@ library(gganimate) library(tidyverse) library(shinyBS) library(zip) +library(svglite) setwd("~/GitHub/hectorui/h2") diff --git a/h2/outfile_area.jpeg b/h2/outfile_area.jpeg new file mode 100644 index 0000000..1c276c2 Binary files /dev/null and b/h2/outfile_area.jpeg differ diff --git a/h2/outfile_area.png b/h2/outfile_area.png index 0906c77..9c07aca 100644 Binary files a/h2/outfile_area.png and b/h2/outfile_area.png differ diff --git a/h2/outfile_area.svg b/h2/outfile_area.svg new file mode 100644 index 0000000..817a027 --- /dev/null +++ b/h2/outfile_area.svg @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +250 +500 +750 +1000 +1900 +2000 +2100 +2200 +2300 +Carbon Pool (Pg C) +source_name + + + + + + + + + +Atmosphere +Deep Ocean +Detritus +Fossil Fuels +HL Ocean +Intermediate Ocean +LL Ocean +Soil +Vegetation +Amount of Carbon in Atmosphere by Source + + diff --git a/h2/outfile_bar.gif b/h2/outfile_bar.gif index 2a154b9..e4798a6 100644 Binary files a/h2/outfile_bar.gif and b/h2/outfile_bar.gif differ