R/maps.R

Defines functions get_json em_bank em_map

Documented in em_bank em_map get_json

#' Maps
#' 
#' Add map dependency to \link[echarts4r]{e_charts} chart.
#' 
#' @param e An object of class \code{echarts4r}.
#' @param map Map name, a country.
#' 
#' @details essentially a wrapper to \link[echarts4r]{e_map_register}.
#' 
#' @examples 
#' maps <- em_bank() # list of all maps
#' 
#' \dontrun{
#' library(echarts4r)
#' 
#' df <- data.frame(x = "", y = "")
#' 
#' df %>% 
#'   e_charts(x) %>%
#'   em_map("France") %>% 
#'   e_map(y, map = "france") 
#' }
#' 
#' @name maps
#' @export
em_map <- function(e, map){
  
  if(missing(e) || missing(map))
    stop("missing e or map", call. = FALSE)
  
  file <- paste0(map, ".json")
  
  json <- jsonlite::read_json(
    system.file(file, package = "echarts4r.maps")
  )
  
  e <- echarts4r::e_map_register(e, map, json)
  return(e)
}

#' @rdname maps
#' @export
em_bank <- function(){
  files <- list.files(
    system.file("", package = "echarts4r.maps")
  )
  
  files <- files[grepl(".json", files)]
  
  gsub(".json", "", files)
}

#' @rdname maps
#' @export
get_json <- function(map){
  file <- paste0(map, ".json")
  
  jsonlite::read_json(
    system.file(file, package = "echarts4r.maps")
  )
}
JohnCoene/echarts4r.maps documentation built on Jan. 12, 2021, 12:45 a.m.