ec.util: Utility functions

View source: R/util.R

ec.utilR Documentation

Utility functions

Description

tabset, table layout, support for GIS shapefiles through library 'sf'

Usage

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

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, see Details

js

optional JavaScript function, default is NULL.

Details

cmd = 'sf.series'
⁠ ⁠Build leaflet or geo map series from shapefiles.
⁠ ⁠Supported types: POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON
⁠ ⁠Coordinate system is leaflet(default), geo or cartesian3D (for POINT(xyz))
⁠ ⁠Limitations:
⁠ ⁠polygons can have only their name in tooltip,
⁠ ⁠assumes Geodetic CRS is WGS 84, for conversion use st_transform with crs=4326.
⁠ ⁠Parameters:
⁠ ⁠df - value from st_read
⁠ ⁠cs - optional coordinateSystem value
⁠ ⁠nid - optional column name for name-id used in tooltips
⁠ ⁠verbose - optional, print shapefile item names in console
⁠ ⁠Returns a list of chart series
cmd = 'sf.bbox'
⁠ ⁠Returns JavaScript code to position a map inside a bounding box from st_bbox, for leaflet only.
cmd = 'sf.unzip'
⁠ ⁠unzips a remote file and returns local file name of the unzipped .shp file
⁠ ⁠url - URL of remote zipped shapefile
⁠ ⁠shp - optional name of .shp file inside ZIP file if multiple exist. Do not add file extension.
cmd = 'geojson'
⁠ ⁠custom series list with geoJson objects
⁠ ⁠geojson - object from fromJSON
⁠ ⁠... - optional custom series attributes like itemStyle
⁠ ⁠ppfill - optional fill color like '#F00', OR NULL for no-fill, for all Points and Polygons
⁠ ⁠nid - optional feature property for item name used in tooltips
⁠ ⁠optional geoJSON feature properties: color, ppfill, lwidth, ldash, radius(for points)
cmd = 'layout'
⁠ ⁠multiple charts in table-like rows/columns format
⁠ ⁠... - List of charts
⁠ ⁠title - optional title for the set, rows= Number of rows, cols= Number of columns
⁠ ⁠Returns a container div in rmarkdown, otherwise browsable.
⁠ ⁠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 name/chart pairs like n1=chart1, n2=chart2, each tab may contain a chart.
⁠ ⁠width - optional width of tabs in pixels
⁠ ⁠height - optional height of tabs in pixels
⁠ ⁠tabStyle - tab style string, see default tabStyle variable in the code
⁠ ⁠Returns A) tagList of tabs when in a pipe without '...' params, see example
⁠ ⁠Returns B) browsable when '...' params are provided by user
cmd = 'button'
⁠ ⁠UI button to execute a JS function,
⁠ ⁠text - the button label
⁠ ⁠js - the JS function string
⁠ ⁠... - optional parameters for the rect element
⁠ ⁠Returns a graphic.elements-rect element.
cmd = 'morph'
⁠ ⁠... - a list of charts or chart options
⁠ ⁠js - optional JS function for switching charts. Default function is on mouseover. Disable with FALSE.
⁠ ⁠Returns a chart with ability to morph into other charts
cmd = 'fullscreen'
⁠ ⁠a toolbox feature to toggle fullscreen on/off. Works in a browser, not in RStudio.
cmd = 'rescale'
⁠ ⁠v - input vector of numeric values to rescale
⁠ ⁠t - target range c(min,max), numeric vector of two
cmd = 'level'
⁠ ⁠calculate vertical levels for timeline line charts, returns a numeric vector
⁠ ⁠df - data.frame with from and to columns
⁠ ⁠from - name of 'from' column
⁠ ⁠to - name of 'to' column

Examples

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))  # move chart up
  p2 <- mtcars |> ec.init()
  ec.util(cmd= 'tabset', cars= p1, mtcars= p2, width= 333, height= 333)

  lapply(list('dark','macarons','gray','jazz','dark-mushroom'),
                \(x) cars |> ec.init() |> ec.theme(x) ) |>
  ec.util(cmd='layout', cols= 2, title= 'my layout')
  
  setd <- \(type) {
	   mtcars |> group_by(cyl) |> 
	 ec.init(ctype= type,
		  title= list(subtext= 'mouseover points to morph'),
		  xAxis= list(scale= TRUE))
  }
  oscatter <- setd('scatter')
  obar <- setd('bar')
  ec.util(cmd='morph', oscatter, obar)
}

echarty documentation built on Oct. 16, 2023, 1:06 a.m.