R/eq_create_label.R

#' Create labels for eq_map
#'
#' Creates a label for data mapped data.
#'
#' @param data A \code{data.frame} output from \code{\link{eq_clean_data}} or substantially similar
#' data. This is required as the label defaults are named as they would be in the resultant column.
#' Must have columns named "LOCATION_NAME", "DEATHS", "EQ_PRIMARY" (respectively). These are defaults.
#'
#' @param labelcols A character list of column names. Defaults to assignment requirement.
#'
#' @param labellabels A character list of the names for the items set in \code{labelcols}. This governs
#' what appears to the user. Setting to NULL will use the the character string in l\code{labelcols}.
#'
#' @return A string with LOCATION_NAME, EQ_PRIMARY and TOTAL_DEATHS.
#'
#' @export
#'
#' @examples
#'\dontrun{
#' #Run with just cleaned data will produce the assignment defaults
#' eq_create_label(EQdata)
#'
#' #Pass any columns names and associated labels
#' eq_create_label(EQdata,
#'                 labelcols=c("DATE"),
#'                 labellabels=c("EQ Estimated Date"))
#'
#' #Use labellabels = NULL to preserve column names of data source as the map annotation (if its clean)
#' eq_create_label(EQdata,
#'                          labelcols="LATITUDE",
#'                          labellabels=NULL)}
#'
eq_create_label <- function(data,labelcols=c("LOCATION_NAME","EQ_PRIMARY","TOTAL_DEATHS"),
                            labellabels = c("Location","Magnitude", "Deaths"))

{
  #NULL check for labels
  if(is.null(labellabels)) labellabels<-labelcols
#browser()
  #subset columns
  data<-data[,labelcols]

  #for appearance purposes, we will overwrite any NA cells with ""
  data[is.na(data)]<-""

  #first weave together the label to the column
  # Using a loop to be able to easily and reference column order
  for (i in 1:length(labelcols)){
    data[,i]<-paste(labellabels[i],": ",data[[i]],"<br>",sep=)

  }

  return(Reduce(f=paste,x=data))



}
JJNewkirk/NOAAEQ documentation built on May 27, 2019, 1:12 p.m.