Learn R Programming

echarty (version 1.4.7)

ec.util: Utility functions

Description

tabset, table layout, support for GIS shapefiles thru library sf

Usage

ec.util(..., cmd = "sf.series")

Arguments

...

Optional parameters for the command
for sf.series - see points, polylines, polygons(itemStyle).
for tabset parameters should be in format name1=chart1, name2=chart2, see example

cmd

Utility command

  • sf.series returns a list of chart series
    required parameter df - value from st_read
    optional parameter nid - column name for name-id used in tooltips
    optional coordinate system cs - leaflet(default) or geo

  • sf.bbox returns JavaScript code to position a map inside a bounding box from st_bbox, for leaflet only.

  • sf.unzip unzips a remote file and returns local file name of the unzipped .shp file
    required parameter url - URL of remote zipped shapefile
    optional parameter shp - name of .shp file inside ZIP file if multiple exist. Do not add file extension.

  • tabset returns a tagList of tabs, each tab may contain a chart.

  • layout returns a container div in rmarkdown, otherwise browsable.

Details

cmd='sf.series'
Goal is to build leaflet or geo map series from shapefiles.
Supported types: POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON
Limitations:
polygons can have only their name in tooltip,
assumes Geodetic CRS is WGS 84, use st_transform with crs=4326 to convert.
optional parameter:
verbose= Print shapefile item names in console
cmd='layout'
multiple charts in table-like rows/columns format, ...= List of charts
optional parameters are:
title= Title for the set, rows= Number of rows, cols= Number of columns,
width= Width of columns (one of xs, md, lg)
For 3-4 charts one would use multiple series within a grid.
For greater number of charts ec.util(cmd='layout') comes in handy
cmd='tabset'
...= a list of charts OR tab-name/chart pairs like n1=chart1, n2=chart2
optional parameters are:
width= Width of tabs in pixels, height= Height of tabs in pixels
tabStyle= tab style string, see default tabStyle variable in the code

Examples

Run this code
if (interactive()) {  # comm.out: Fedora errors about some 'browser'
  library(sf)
  fname <- system.file("shape/nc.shp", package="sf")
  nc <- as.data.frame(st_read(fname))
  ec.init(load= c('leaflet', 'custom'),  # load custom for polygons
     js= ec.util(cmd= 'sf.bbox', bbox= st_bbox(nc$geometry)),
     series= ec.util(df= nc, nid= 'NAME', itemStyle= list(opacity= 0.3)),
     tooltip= list(formatter= '{a}')
  )

  htmltools::browsable(
    lapply(iris |> dplyr::group_by(Species) |> dplyr::group_split(), 
           function(x) {
     x |> ec.init(ctype= 'scatter', title= list(text= unique(x$Species)))
           }) |> 
    ec.util(cmd= 'tabset')
  )

  p1 <- cars |> ec.init(grid= list(top= 20))
  p2 <- mtcars |> ec.init()
  htmltools::browsable(
    ec.util(cmd= 'tabset', cars= p1, mtcars= p2, width= 200, height= 200)
  )

  lapply(list('dark','macarons','gray','jazz','dark-mushroom'),
                function(x) cars |> ec.init() |> ec.theme(x) ) |>
  ec.util(cmd='layout', cols= 2, title= 'my layout')
}

Run the code above in your browser using DataLab