R/mod_national_map.R

Defines functions mod_national_map_server mod_national_map_ui

#' national_map UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
#' @importFrom scales comma
#' @import plotly
mod_national_map_ui <- function(id){
  ns <- NS(id)
  tagList(
    box(
      plotlyOutput(ns("sightings_per_state_map")),
      title = "Total Number of UFO Sightings In The U.S",
      width = NULL,
      maximizable = TRUE,
      status = "primary"
    )
  )
}

#' national_map Server Function
#'
#' @import plotly
#' @import sf
#' @noRd
mod_national_map_server <- function(input, output, session){
  ns <- session$ns

  total_sightings_by_state <- ufosightings::total_sightings_by_state
  total_sightings_by_state_dor <- ufosightings::total_sightings_by_state_dor

  output$sightings_per_state_map <- renderPlotly({

    plot_ly(stroke = I("black"), span = I(1)) %>%
      add_sf(
        data = total_sightings_by_state,
        color = I("gray95"),
        hoverinfo = "none"
      ) %>%
      add_sf(
        data = total_sightings_by_state_dor,
        color = ~sightings,
        split = ~state,
        text = ~paste(
          paste0(state, ":"),
          scales::comma(sightings, accuracy = 1)
          ),
        hoverinfo = "text",
        hoveron = "fills"
      ) %>%
      layout(
        showlegend = FALSE,
        plot_bgcolor = "#e6edf2",
        paper_bgcolor = "#e6edf2"
      )
  })

}

## To be copied in the UI
# mod_national_map_ui("national_map_ui_1")

## To be copied in the server
# callModule(mod_national_map_server, "national_map_ui_1")
asbates/ufo.sightings documentation built on Jan. 20, 2021, 3:33 a.m.