R/module3.R

Defines functions eq_create_label eq_map

Documented in eq_create_label eq_map

#' @title Leaflet Map Tools
#'
#' @description eq_map creates a leaflet map
#'     showing the location of the earthquake.
#'
#' @details The function maps the epicenters Longitude and
#'  Latitude and annotates each point in a popup window.
#'
#' @param data The earthquake clean data frame
#'
#' @param annot_col The name of the column to be used for annotation
#'
#' @return Returns a leaflet map
#'
#' @importFrom magrittr %>%
#'
#' @examples
#' \dontrun{
#' load_raw_data() %>%
#' eq_clean_data() %>%
#' eq_location_clean() %>%
#' dplyr::filter(Country == "MEXICO" & lubridate::year(Date) >= 2000)
#' eq_map(data = NOAA_data, annot_col = "Date")
#' }
#'
#' @export


eq_map <- function(data, annot_col = "Date") {
  # annot_col contains  the name of the column to display
  leaflet::leaflet() %>%
    leaflet::addTiles() %>%
    leaflet::addCircleMarkers(data = data,
                              radius = ~ Mag,
                              lng = ~ Longitude,
                              lat = ~ Latitude,
                              popup = ~ data[[annot_col]])
}



#' @title Leaflet Earth Quake Map with Customized Label
#'
#' @description eq_create_label creates a more informative popup using HTML tags
#'     to be used as labels in eq_map.
#'
#' @details This function put together a character string for each earthquake to be
#' used as a more informative label. The label contains the following items:
#' \itemize{
#'  \item Location
#'  \item Magnitude
#'  \item Total Deaths
#'  }
#'
#' @param data The earthquake clean data frame.
#'
#' @return A character vector using HTML tags to be used as label for
#'  labelcolumn variable.
#'
#' @importFrom magrittr %>%
#'
#' @examples
#' \dontrun{
#' load_raw_data() %>%
#' eq_clean_data() %>%
#' eq_location_clean() %>%
#'  dplyr::filter(Country %in% c("HONDURAS", "MEXICO") & lubridate::year(Date) >= 2000) %>%
#'  dplyr::mutate(popup_text = eq_create_label(.)) %>%#'
#'  eq_map( annot_col = "popup_text")
#' }
#'
#' @export

eq_create_label <- function(data) {
  labelled_data <- data %>%
    dplyr::mutate(popup_text = paste("<b>Location:</b>", Location, "<br />",
                                     "<b>Magnitude:</b>", Mag, "<br />",
                                     "<b>Total Deaths:</b>", Deaths, "<br />"),
                  popup_text = ifelse(is.na(Location) | is.na(Mag) | is.na(Deaths),
                                      paste("<b>No Data Available</b>"), popup_text))
  labelled_data$popup_text
}
BFavetto/EarthquakeAnalysis documentation built on Dec. 17, 2021, 9:50 a.m.