Skip to contents

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

Usage

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

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.

event

optional event name for cmd='morph'.

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
    nid - optional column name for name-id used in tooltips
    cs - optional coordinateSystem value, default 'leaflet'
    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 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 name/chart pairs like n1=chart1, n2=chart2, each tab may contain a chart.
    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 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: 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}')
  )

  htmltools::browsable(
    lapply(iris |> group_by(Species) |> 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=26), height=333)  # move chart up
  p2 <- mtcars |> arrange(mpg) |> ec.init(height=333, ctype='line')
  ec.util(cmd= 'tabset', cars= p1, mtcars= p2)
}
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
#> Reading layer `nc' from data source 
#>   `C:\Users\gugu\Documents\R\win-library\4.2\sf\shape\nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#> 
#>  series: 108 records: 2529 
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);}") ) )
lapply(list('dark','macarons','gray','jazz','dark-mushroom'), function(x) cars |> ec.init(grid= list(bottom=0)) |> ec.theme(x) ) |> ec.util(cmd='layout', cols= 2, title= 'my layout')

my layout




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(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)