R/select_vessel.R

Defines functions select_vessel_server select_vessel_ui

Documented in select_vessel_server select_vessel_ui

#' @title UI for the vessel dropdown buttons
#'
#' @description A shiny Module.
#'
#' @param id Internal parameters for {shiny}.
#' @param marine_stats The marine_stats dataset
#'
#' @export

select_vessel_ui <- function(id, marine_stats) {
  ns <- shiny::NS(id)

  vessel_types <- sort(unique(marine_stats$vessel_type))

  out <- shiny.semantic::grid(
    grid_template = shiny.semantic::grid_template(
      default = list(
        areas = rbind(c("btn1"), c("btn2")),
        cols_width = c("100%")
      )
    ),
    area_styles = list(btn1 = "margin: 5px;", btn2 = "margin: 5px;"),
    btn1 = shiny::div(
      shiny::h5(class = "label", "Vessel type"),
      shiny.semantic::dropdown_input(
        input_id = ns("vessel_type"),
        choices = vessel_types,
        value = vessel_types[1],
        default_text = "Select a vessel type"
      )
    ),
    btn2 = shiny::div(
      shiny::h5(class = "label", "Vessel name"),
      shiny::uiOutput(outputId = ns("vessel_name_btn"))
    )
  )

  return(out)
}

#' @title Server for the vessel dropdown buttons
#'
#' @description A shiny Module.
#'
#' @param id Internal parameters for {shiny}.
#' @param marine_stats The marine_stats dataset
#'
#' @importFrom dplyr %>%
#'
#' @export

select_vessel_server <- function(id, marine_stats) {
  shiny::moduleServer(id, function(input, output, session) {

    output$vessel_name_btn <- shiny::renderUI({
      # Vessel's type selected
      type <- input$vessel_type

      if(!type %in% marine_stats$vessel_type) return(NULL)

      if(!is.null(type)) {
        # Vessels
        choices <- marine_stats %>%
          dplyr::filter(vessel_type == type) %>%
          dplyr::pull(vessel_name) %>%
          unique() %>%
          sort()

        # Button
        shiny.semantic::dropdown_input(
          input_id = "vessel_name",
          choices = choices,
          value = choices[3] # Just for the first impression
        )
      }
    })
  })
}
DouglasMesquita/marineApp documentation built on Dec. 17, 2021, 5:29 p.m.