R/map.R

#' Popup Labeling for Leaflet Map
#' 
#' This function create Popup window with HTML format for Leaflet MAP
#'
#' @param data  A data frame with NOAA Significant Earthquake Database
#' @return a vector with the HTML-formatted labels like row in data set
#' @export
#'
#' @examples
#' library(dplyr); library(lubridate)
#' NOAA_quakes%>%
#' eq_clean_data()%>%
#' dplyr::filter(COUNTRY == 'MEXICO') %>%
#' dplyr::filter(lubridate::year(DATE) >= 2000) %>%
#' dplyr::mutate(popup_text = eq_create_label(.))

eq_create_label<-function(data){
  label<-''
  lab=function(LC,EQ,TD){
    if(!is.na(LC)){
      label<-glue::glue("{label}<b>LOCATION: </b> {LC} <br>")
    }
    if(!is.na(EQ)){
      label<-glue::glue("{label}<b>Magnitude: </b> {EQ} <br>")
    }
    if(!is.na(TD)){
      label<-glue::glue("{label}<b>Total Death: </b> {TD} <br>")
    }
    return(label)
  }

  data <- data %>%
    dplyr::mutate_(popup_text = ~purrr::pmap_chr(
      list(LOCATION_NAME, EQ_PRIMARY,
           TOTAL_DEATHS),
      lab
    ))
  data$popup_text
}


#' Create Interactive Leaflet Map with NOAA data
#' 
#'  
#'    
#' This function create interactive leaflet map of the location of earthquakes
#' from the NOAA dataset. The size of the circles are
#' proportional to the magnitude of the earthquakes. Pop-up window shows when you click on a circle
#'
#' @param data  A data frame with NOAA Significant Earthquake Database
#' @param annot_col variable for popup display
#' @return map with Earthquakes
#' @import leaflet
#' @export
#'
#' @examples
#' library(dplyr); library(lubridate)
#' NOAA_quakes%>%
#'  eq_clean_data()%>%
#' dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>%
#' dplyr::mutate(popup_text = eq_create_label(.)) %>%
#' eq_map(annot_col = "popup_text")

eq_map<-function(data,annot_col="DATE"){
  data <- data%>%
    dplyr::mutate_(popup_col = as.name(annot_col))
  data %>%
    leaflet::leaflet() %>%
    leaflet::addTiles() %>%
    leaflet::addCircles(
      lng = ~LONGITUDE, lat = ~LATITUDE,
      radius =~as.numeric(EQ_PRIMARY)*7500,
      popup = ~paste(popup_col))
}
jyjek/jyjekNOAA documentation built on May 7, 2019, 10:52 p.m.