Skip to content

Commit

Permalink
add 2021 IDN fish data and include year selector
Browse files Browse the repository at this point in the history
  • Loading branch information
ibidyouadu committed Nov 4, 2021
1 parent fb6539e commit d812799
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 49 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ importFrom(shiny,tagList)
importFrom(shinyWidgets,dropdown)
importFrom(shinyWidgets,multiInput)
importFrom(shinyWidgets,pickerInput)
importFrom(shinyWidgets,radioGroupButtons)
importFrom(shinyWidgets,switchInput)
importFrom(shinyWidgets,updatePickerInput)
importFrom(shinyjs,extendShinyjs)
Expand Down
2 changes: 1 addition & 1 deletion R/mod_main.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mainUI <- function(id){
tabsetPanel(
id = ns('tabs'),
tabPanel("Start", startUI("startUI")),
tabPanel('Coral Reefs', plotUI('plotUI')),
tabPanel('Fish', plotUI('plotUI')),
tabPanel('Mangrove Forests', mangroveUI('mangroveUI')),
tabPanel('Map', mapUI('mapUI'))
# tabPanel('Report', reportUI('reportUI'))
Expand Down
10 changes: 5 additions & 5 deletions R/mod_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ plotServer <- function(id, rv){
sel_maa <- rv$sel_maa
y_scale <- rv$sel_yscale
sel_geom <- rv$sel_geom
sel_dates <- rv$sel_dates
sel_year <- rv$sel_year

if (is.null(sel_maa)) {
div(class="warning_message", "No managed access area selected.")
} else {
p <- switch(rv$sel_metric,
"Fish Biomass" = plot_biomass(data_filtered, sel_family, sel_geom),
"Fish Density" = plot_density(data_filtered, sel_family, sel_geom),
"Fish Diversity" = plot_diversity(data_filtered, sel_family, sel_geom),
"Fish Size" = plot_size(data_filtered, sel_family, sel_geom)
"Fish Biomass" = plot_biomass(data_filtered, sel_year, sel_family, sel_geom),
"Fish Density" = plot_density(data_filtered, sel_year, sel_family, sel_geom),
"Fish Diversity" = plot_diversity(data_filtered, sel_year, sel_family, sel_geom),
"Fish Size" = plot_size(data_filtered, sel_year, sel_family, sel_geom)
)
p$facet$params$free$y <- y_scale
rv$current_plot <- p
Expand Down
7 changes: 4 additions & 3 deletions R/mod_plot_utils_fish_biomass.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plot_biomass <- function(data_filtered, sel_family, sel_geom) {
plot_biomass <- function(data_filtered, sel_year, sel_family, sel_geom) {
data_aggreg <- data_filtered %>%
dplyr::filter(family %in% sel_family) %>%
aggregate_data(., 'biomass_kg_ha')
dplyr::filter(family %in% sel_family) %>%
dplyr::filter(year == sel_year) %>%
aggregate_data(., 'biomass_kg_ha')

if (sel_geom == 'Bar plots') {
data_summary <- summarySE(data_aggreg, 'biomass_kg_ha')
Expand Down
7 changes: 4 additions & 3 deletions R/mod_plot_utils_fish_density.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plot_density <- function(data_filtered, sel_family, sel_geom) {
plot_density <- function(data_filtered, sel_year, sel_family, sel_geom) {
data_aggreg <- data_filtered %>%
dplyr::filter(family %in% sel_family) %>%
aggregate_data(., 'density_ind_ha')
dplyr::filter(family %in% sel_family) %>%
dplyr::filter(year == sel_year) %>%
aggregate_data(., 'density_ind_ha')
if (sel_geom == 'Bar plots') {
data_summary <- summarySE(data_aggreg, 'density_ind_ha')

Expand Down
3 changes: 2 additions & 1 deletion R/mod_plot_utils_fish_diversity.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plot_diversity <- function(data_filtered, sel_family, sel_geom) {
plot_diversity <- function(data_filtered, sel_year, sel_family, sel_geom) {
data_aggreg <- data_filtered %>%
dplyr::filter(family %in% sel_family) %>%
dplyr::filter(year == sel_year) %>%
aggregate_data(., 'species')
if (sel_geom == 'Bar plots') {
data_summary <- summarySE(data_aggreg, 'species')
Expand Down
9 changes: 5 additions & 4 deletions R/mod_plot_utils_fish_size.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
plot_size <- function(data_filtered, sel_family, sel_geom) {
plot_size <- function(data_filtered, sel_year, sel_family, sel_geom) {

data_aggreg <- data_filtered %>%
dplyr::filter(family %in% sel_family) %>%
aggregate_data(., 'sizeclass')
dplyr::filter(year == sel_year) %>%
aggregate_data(., 'size_class')

if (sel_geom == "Bar plots") {
data_summary <- summarySE(data_aggreg, 'density_ind_ha', for.size=TRUE)
ggplot2::ggplot(data=data_summary,
aes(sizeclass, density_ind_ha),
aes(size_class, density_ind_ha),
na.rm = TRUE) +
facet_wrap('ma_name') +
geom_bar(aes(fill = location_status), position=position_dodge(),
Expand All @@ -16,7 +17,7 @@ plot_size <- function(data_filtered, sel_family, sel_geom) {
} else {
data_local <- get_local_data(data_aggreg, 'density_ind_ha', for.size=TRUE)
ggplot2::ggplot(data=data_local,
aes(sizeclass, group=location_status),
aes(size_class, group=location_status),
na.rm = TRUE) +
facet_wrap('ma_name') +
geom_density(aes(fill=location_status), size=0.3, alpha=0.5) +
Expand Down
18 changes: 9 additions & 9 deletions R/mod_plot_utils_selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ aggregate_data <- function(data_filtered, metric) {
data = .,
FUN = mean)

} else if (metric == 'sizeclass') {
} else if (metric == 'size_class') {
# idea: for each size class, you can expect to see `density_ind_ha` many
# fish per hectare.
aggregate(density_ind_ha ~ country + ma_name + location_status + location_name +
transect_no + sizeclass, data=data_filtered, FUN=sum)
transect_no + size_class, data=data_filtered, FUN=sum)
}
}

Expand All @@ -66,27 +66,27 @@ get_local_data <- function(data_aggreg, metric, for.size=FALSE) {
if (metric %in% c("biomass_kg_ha", "density_ind_ha", "dbh_cm", "sapling_tree_density_ind_m2")) {
groupvars <- c('country', 'ma_name', 'location_status', 'location_name')
if (for.size) {
groupvars <- append(groupvars, 'sizeclass')
groupvars <- append(groupvars, 'size_class')
}
formula <- paste(metric, paste(groupvars, collapse=" + "), sep=" ~ ") %>%
as.formula()
aggregate(formula, data=data_aggreg, FUN=mean)
} else { # sizeclass
} else { # size_class
aggregate(density_ind_ha ~ country + ma_name + location_status +
location_name + sizeclass, data=data_aggreg, FUN=mean)
location_name + size_class, data=data_aggreg, FUN=mean)
}
}
summarySE <- function(data_aggreg, metric, for.size=FALSE, for.tree=FALSE) {

groupvars1 <- c('country', 'ma_name', 'location_status', 'location_name')
groupvars2 <- groupvars1[-length(groupvars1)]
if (for.size) {
groupvars1 <- append(groupvars1, 'sizeclass')
groupvars2 <- append(groupvars2, 'sizeclass')
groupvars1 <- append(groupvars1, 'size_class')
groupvars2 <- append(groupvars2, 'size_class')
# changing metric to 'count' to use in the following aggregates w/o changing
# the code much. maybe clunky to do so and it may be better to just set metric
# to 'count' from the beginning, but from a readability/comprehension standpoint,
# I think it makes sense to keep metric as 'sizeclass' in the beginning,
# I think it makes sense to keep metric as 'size_class' in the beginning,
# as it makes it very clear that the surrounding code is in regard to fish size
}

Expand All @@ -108,7 +108,7 @@ summarySE <- function(data_aggreg, metric, for.size=FALSE, for.tree=FALSE) {
# take mean across locations
data_summary <- aggregate(formula2, data=data_loc, FUN=mean)

if (!for.size) { # ie not sizeclass
if (!for.size) { # ie not size_class
data_summary$N <- aggregate(formula2, data=data_loc, FUN=length) %>%
dplyr::pull(metric)
data_summary$SD <- aggregate(formula2, data=data_loc, FUN=sd) %>%
Expand Down
33 changes: 17 additions & 16 deletions R/mod_side_display.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @noRd
#'
#' @importFrom shiny NS tagList
#' @importFrom shinyWidgets pickerInput multiInput switchInput
#' @importFrom shinyWidgets pickerInput multiInput switchInput radioGroupButtons
#' @importFrom leaflet providers
sidebarDisplayUI <- function(id){
ns <- NS(id)
Expand All @@ -24,7 +24,7 @@ sidebarDisplayServer <- function(id, rv){
ui <- NULL #list()
current_tab <- rv$current_tab

if (current_tab == 'Coral Reefs') {
if (current_tab == 'Fish') {
ui <- tagList(
div(class="sidetitle", "Plotting"),
selectInput(ns('sel_metric'),
Expand Down Expand Up @@ -54,20 +54,8 @@ sidebarDisplayServer <- function(id, rv){
`selected-text-format` = "count > 3"
),
multiple = TRUE
),
# switchInput(ns("sel_date"),
# "Plot dates")
# don't need this right now
# pickerInput(ns('sel_species'),
# 'Fish species',
# choices = LETTERS,
# options = list(
# `actions-box` = TRUE,
# `selected-text-format` = "count > 2"
# ),
# multiple = TRUE
# )
) # tagList
)
)
} else if(current_tab == "Mangrove Forests") {
ui <- tagList(
div(class = "sidetitle", "Plotting"),
Expand All @@ -87,6 +75,14 @@ sidebarDisplayServer <- function(id, rv){
)
)
}
ui <- tagList(ui,
radioGroupButtons(
ns("sel_year"),
"Year",
choices = get_year_choices(rv$sel_country, rv$data_full$fish),
selected = max(get_year_choices(rv$sel_country, rv$data_full$fish))
)
)

if (current_tab == 'Map') {

Expand Down Expand Up @@ -148,6 +144,11 @@ sidebarDisplayServer <- function(id, rv){
# rv$data_map <- get_biomass_loc(rv$data_aggreg)
}, ignoreInit = TRUE
)

observeEvent(input$sel_year, {
rv$sel_year <- input$sel_year
}, ignoreInit = TRUE
)
})
}

Expand Down
8 changes: 8 additions & 0 deletions R/mod_side_display_utils_selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ get_display_choices <- function(sel_maa, data_full) {
unique() %>%
as.vector() %>%
sort()
}

get_year_choices <- function(sel_country, data_full) {
data_full %>%
dplyr::filter(country == sel_country) %>%
dplyr::pull(year) %>%
unique() %>%
sort()
}
12 changes: 6 additions & 6 deletions R/mod_side_geography.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sidebarGeoServer <- function(id, rv){
output$geo <- renderUI({
current_tab <- rv$current_tab

if (current_tab %in% c("Start", "Coral Reefs", "Map")) {
if (current_tab %in% c("Start", "Fish", "Map")) {
init_country_choices <- INIT$COUNTRY_CHOICES$FISH
init_subnational_choices <- INIT$SUBNATIONAL_CHOICES$FISH
init_local_choices <- INIT$LOCAL_CHOICES$FISH
Expand Down Expand Up @@ -65,7 +65,7 @@ sidebarGeoServer <- function(id, rv){
),
pickerInput(
ns('sel_maa'),
'Managed access area',
'Community',
choices = init_maa_choices,
selected = NULL,
multiple = TRUE,
Expand Down Expand Up @@ -96,7 +96,7 @@ sidebarGeoServer <- function(id, rv){

observeEvent(input$sel_country, {
rv$sel_country <- input$sel_country
if (rv$current_tab %in% c("Start", "Coral Reefs", "Map")) {
if (rv$current_tab %in% c("Start", "Fish", "Map")) {
data_country <- rv$data_full$fish %>%
dplyr::filter(country == input$sel_country)
} else if (rv$current_tab == "Mangrove Forests") {
Expand All @@ -115,7 +115,7 @@ sidebarGeoServer <- function(id, rv){

observeEvent(input$sel_subnational, {
rv$sel_subnational <- input$sel_subnational
if (rv$current_tab %in% c("Start", "Coral Reefs", "Map")) {
if (rv$current_tab %in% c("Start", "Fish", "Map")) {
data_subnational <- rv$data_full$fish %>%
dplyr::filter(level1_name %in% input$sel_subnational)
} else if (rv$current_tab == "Mangrove Forests") {
Expand All @@ -134,7 +134,7 @@ sidebarGeoServer <- function(id, rv){

observeEvent(input$sel_local, {
rv$sel_local <- input$sel_local
if (rv$current_tab %in% c("Start", "Coral Reefs", "Map")) {
if (rv$current_tab %in% c("Start", "Fish", "Map")) {
data_local <- rv$data_full$fish %>%
dplyr::filter(level2_name %in% input$sel_local)
} else if (rv$current_tab == "Mangrove Forests") {
Expand All @@ -155,7 +155,7 @@ sidebarGeoServer <- function(id, rv){

observeEvent(input$sel_maa, {
rv$sel_maa <- input$sel_maa
if (rv$current_tab %in% c("Start", "Coral Reefs", "Map")) {
if (rv$current_tab %in% c("Start", "Fish", "Map")) {
rv$data_filtered$fish <- rv$data_full$fish %>%
dplyr::filter(ma_name %in% input$sel_maa)
} else if (rv$current_tab == "Mangrove Forests") {
Expand Down
6 changes: 5 additions & 1 deletion R/utils_init.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#'
#' @noRd
# INIT <- list()
# INIT$CURRENT_TAB <- 'Coral Reefs'
# INIT$CURRENT_TAB <- 'Start'
# INIT$SEL_METRIC <- 'Fish Biomass'
# INIT$SEL_GEOM <- "Bar plots"
# INIT$SEL_YSCALE <- TRUE
Expand All @@ -21,6 +21,10 @@
# FISH = "HND",
# MANGROVES = "Mozambique"
# )
# INIT$SEL_YEAR <- fish.surveys %>%
# filter(country == INIT$SEL_COUNTRY$FISH) %>%
# pull(year) %>%
# max()
# INIT$DATA_FULL <- list(
# FISH = fish.surveys %>% dplyr::filter(family != ""),
# MANGROVES = mangroves
Expand Down
Loading

0 comments on commit d812799

Please sign in to comment.