Skip to contents

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

Usage

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

Arguments

cmd

Utility command name, see Details.

...

Optional parameters for each command.

js

Optional JavaScript function, default is NULL.

event

Optional event name for cmd='morph', default is 'click'.

Details

cmd = 'sf.series'
  Build leaflet or geo map series from shapefiles.
  Supported types: POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON
  Coordinate system could be leaflet(default), geo, cartesian2D or cartesian3D(for POINT(xyz))
  Limitations:
   polygons can have only their name in tooltip, need load='custom' for rendering
   assumes Geodetic CRS is WGS 84, for conversion use st_transform with crs=4326.
  Parameters:
   df - value from st_read
   nid - optional column name used in tooltip formatter
   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.
  Returns full name of unzipped .shp file, or error string starting with 'ERROR'

cmd = 'geojson'
  Custom series list from geoJson objects
   geojson - object from fromJSON
   cs - optional coordinateSystem value, default 'leaflet'
   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 custom series attributes like itemStyle
  Can display also geoJson feature properties: color; lwidth, ldash (lines); ppfill, radius (points)

cmd = 'layout'
  Multiple charts in table-like rows/columns format
   ... - List of charts
   title - optional title for the entire set
   rows - optional number of rows
   cols - optional 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 of name/chart pairs like n1=chart1, n2=chart2, each tab may contain a chart, see example
   tabStyle - tab style string, see default strTabStyle variable in the code
   width - optional width size for the tabset, in CSS format, default is 100%
  Returns A) browsable when '...' params are provided
  Returns B) tagList of tabs when in a pipe (no '...' params)
  Please note that a maximum of five(5) tabs are supported by current tabStyle.

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 option lists
   event - name of event for switching charts. Default is click.
  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

library(dplyr)
if (interactive()) {  # comm.out: Cran 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(cmd= 'sf.series', df= nc, nid= 'NAME', itemStyle= list(opacity=0.3)),
     tooltip= list(formatter= '{a}')
  )
}

if (interactive()) {
 p1 <- cars |> ec.init(grid= list(top=26), height=333)  # move chart up
 p2 <- mtcars |> arrange(mpg) |> ec.init(height=333, ctype='line')
 ec.util(cmd= 'tabset', cars= p1, mtcars= p2)

 lapply(list('dark','macarons','gray','dark-mushroom'),
   function(x) cars |> ec.init(grid= list(bottom=5, top=10)) |> ec.theme(x) ) |>
 ec.util(cmd='layout', cols= 2, title= 'Layout')
}

cars |> ec.init(
  graphic = list(
    ec.util(cmd='button', text='see type', right='center', top=20,
      js="function(a) {op=ec_option(echwid); alert(op.series[0].type);}")
  )
)
colors <- c("blue","red","green") cyls <- as.character(sort(unique(mtcars$cyl))) sers <- lapply(mtcars |> group_by(cyl) |> group_split(), \(x) { cyl <- as.character(unique(x$cyl)) list(type='scatter', id=cyl, dataGroupId=cyl, data= ec.data(x |> select(mpg,hp)), universalTransition= TRUE) }) oscatter <- list( title= list(text='Morph', left='center', subtext='click points to morph'), color= colors, tooltip= list(show=TRUE), xAxis= list(scale=TRUE, name='mpg'), yAxis= list(scale=TRUE, name='hp'), series= sers ) opie <- list( title= list(text= 'Average hp'), color= colors, tooltip= list(show=TRUE), series= list(list( type= 'pie', label= list(show=TRUE), colorBy= 'data', data= ec.data(mtcars |> group_by(cyl) |> summarize(value= mean(hp)) |> mutate(groupId= as.character(cyl), name= as.character(cyl)),'names'), universalTransition= list(enabled=TRUE, seriesKey= cyls) )) ) ec.util(cmd='morph', oscatter, opie)