R/mod_creel_site.R

Defines functions mod_creel_site_server mod_creel_site_ui

#' creel_site UI Function
#'
#' @description Module for the site select input
#'
#' @param id, input, output, session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList uiOutput br req reactive
#' @importFrom dplyr mutate select arrange distinct pull
#' @importFrom shinyWidgets pickerInput pickerOptions
mod_creel_site_ui <- function(id) {
  tagList(
    uiOutput(NS(id, "site_select")),
    br()
  )
}

#' creel_site Server Function. Get input values from first module as a reactive list object.
#'
#' @noRd 
mod_creel_site_server <- function(id, yrmn) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    # Get creel sites given date range and catch area
    creel_sites = reactive({
      req(yrmn()$yr)
      req(yrmn()$mn)
      req(yrmn()$cr)
      chosen_years = paste0(yrmn()$yr, collapse = ", ")
      chosen_months = paste0(match(yrmn()$mn, month.name), collapse = ", ")
      chosen_catch_areas = paste0(paste0("'", yrmn()$cr, "'"), collapse = ", ")
      sites = get_creel_sites(chosen_years, chosen_months, chosen_catch_areas) %>%
        mutate(site_label = paste0(site_code, ": ", site_name)) %>%
        select(location_id, site_code, site_name, site_label, catch_area,
               latitude, longitude)
      return(sites)
    })

    # Pull out creel_site_list from creel_sites
    creel_site_list = reactive({
      creel_site_data = creel_sites() %>%
        select(site_label) %>%
        arrange(site_label) %>%
        distinct() %>%
        pull(site_label)
      return(creel_site_data)
    })
    
    # Creel site select
    output$site_select = renderUI({
      pickerInput(ns("site_select"),
                  label = "Select the creel site(s)",
                  multiple = TRUE,
                  choices = creel_site_list(),
                  selected = creel_site_list()[1],
                  width = "100%",
                  options = pickerOptions(actionsBox = TRUE))
    })
    
    # Output reactive list
    reactive({
      list(
        ss = input$site_select )
    })
  })
}
    
## To be copied in the UI
# mod_creel_site_ui("creel_site_ui")
    
## To be copied in the server
# callModule(mod_creel_site_server, "creel_site_ui")
 
arestrom/BaselineData documentation built on Sept. 28, 2020, 9:38 a.m.