Skip to content

Commit

Permalink
Plot aesthetic and download button updates
Browse files Browse the repository at this point in the history
Area plot aesthetics updated to better match bar plot gif, and minor updates to zip file downloading
  • Loading branch information
ciara-donegan committed Feb 28, 2024
1 parent 836c2f0 commit cdbda33
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 201 deletions.
3 changes: 1 addition & 2 deletions h2/app.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ui <- fluidPage(
tabPanel(title = "Explore Hector",
fluidRow(
run_ui("run_1"),
#download_ui("download_1"),
)
),
tabPanel(title = "Carbon Tracking",
Expand All @@ -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)
Expand Down
225 changes: 26 additions & 199 deletions h2/components/modules/mod_tracking.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,29 @@ 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")),
)
)
}

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")})
Expand All @@ -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")
}
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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'))
}
)

Expand Down
1 change: 1 addition & 0 deletions h2/global.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ library(gganimate)
library(tidyverse)
library(shinyBS)
library(zip)
library(svglite)

setwd("~/GitHub/hectorui/h2")

Expand Down
Binary file added h2/outfile_area.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified h2/outfile_area.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cdbda33

Please sign in to comment.