R/module3.R

Defines functions eq_create_label eq_map

Documented in eq_create_label eq_map

#Andrew Spewak
#Mastering Software Development in R Capstone




#' eq_map function
#'
#' function that creates a leaflet map of earthquakes
#'
#' @param mapping
#' @param data
#' @param stat
#' @param position
#' @param na.rm
#' @param show.legend
#' @param inherit.aes
#'
#' @importFrom leaflet leaflet
#' @importFrom leaflet addTiles
#' @importFrom leaflet addCircleMarkers
#'
#'
#' @return a function that will build a layer based on geom_timeline geom.
#'
#' @examples
#' \dontrun{readr::read_delim("earthquakes.tsv.gz", delim = "\t") %>%
#'  eq_clean_data() %>%
#'  dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>%
#'  eq_map(annot_col = "DATE")}
#' @export
eq_map <- function(eq_clean=NULL, annot_col="date") {

  leaflet::leaflet() %>%
    leaflet::addTiles() %>%
    leaflet::addCircleMarkers(data = eq_clean, lng = ~ LONGITUDE, lat = ~ LATITUDE, radius = ~ EQ_MAG_ML, fillOpacity = 0.5,
                              popup =~ paste0(get(annot_col)))
}

#' eq_create_label function
#'
#' function to generate pop-up markers on map
#'
#' @param eq_map_clean the cleaned earthquake data file for mapping
#' @return a character vector that is used as pop-up text on the leaflet map from the eq_map function
#'
#' @examples
#' \dontrun{readr::read_delim("earthquakes.tsv.gz", delim = "\t") %>%
#'  eq_clean_data() %>%
#'  dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>%
#'  dplyr::mutate(popup_text = eq_create_label(.)) %>%
#'  eq_map(annot_col = "popup_text")}
#'
#' @export
eq_create_label <- function(eq_map_clean=NULL) {
  eq_map_clean %>%
    dplyr::mutate(location = ifelse(is.na(LOCATION_NAME), '', paste("<b>Location: </b>", LOCATION_NAME, "<br>")),
                  magnitude = ifelse(is.na(EQ_PRIMARY), '', paste("<b>Magnitude: </b>", EQ_PRIMARY, "<br>")),
                  deaths = ifelse(is.na(DEATHS), '', paste("<b>Total deaths: </b>", DEATHS))) %>%
    dplyr::rowwise() %>%
    do(popup_text = paste(.$location, .$magnitude, .$death)) %>%
    unlist(recursive = FALSE) %>%
    as.character
}
AESpe/R_capstone_final documentation built on Oct. 16, 2020, 12:37 a.m.