Learn R Programming

⚠️There's a newer version (0.3.0) of this package.Take me there.

An R Interface to deck.gl

Deckgl for R makes the open-source JavaScript library deck.gl available within R via the htmlwidgets package.

Notes

  • It is a known issue that the deckgl widget might not be visible in the viewer pane of RStudio. Just open it in your browser by clicking Show in new window and everything will be fine.
  • The documentation is work in progress. Please check also the examples as a starting point.
  • You do not need a mapbox api key to use this package. It is always optional to add a base map from mapbox to your widget.

Installation

install.packages("deckgl")

You can install the latest version of deckgl from github with:

# install.packages("remotes")
remotes::install_github("crazycapivara/deckgl")

Quickstart

Just check if everything is fine:

library(deckgl)

does_it_work()
#> You should see a text layer telling you that it works.

# Or in case you do have an api token from mapbox ...
does_it_work("yourSuperSecretApiToken")
#> Output should be the same as above but rendered on top of a base map from mapbox.

Usage

Create a deckgl widget:

deckgl()

Add a base map from mapbox (optional):

Sys.setenv(MAPBOX_API_TOKEN = "yourSuperSecretApiToken")

deckgl() %>%
  add_mapbox_basemap(style = "mapbox://styles/mapbox/dark-v9")

Add any kind of layers:

# Grid layer example
data("sf_bike_parking")

properties <- list(
  pickable = TRUE,
  extruded = TRUE,
  cellSize = 200,
  elevationScale = 4,
  getPosition = JS("data => [data.lng, data.lat]"),
  getTooltip = JS("object => object.count")
)

deckgl(zoom = 11, pitch = 45) %>%
  add_grid_layer(data = sf_bike_parking, properties = properties) %>%
  add_mapbox_basemap() # optional
  • The data parameter can either be an url to fetch data from or a data object. Furthermore, you can use get_data in conjunction with add_data to use a data file.
  • The properties parameter is a named list with names corresponding to the properties defined in the deckgl-api-reference for the given layer class. For the example above see the grid-layer-api-reference. In case the property is a data accessor (usually a function in the form of a lambda expression) just use the JS function in R to pass it to the layer object. In the example above this is done for the properties getPosition and getTooltip. It is also possible to pass properties as named arguments via the ... parameter. They are appended to the properties list. Same properties will be overwritten.

Layers

Due to the generic function add_layer any kind of layer defined in the deckgl-api-reference is supported. The type of layer is chosen via the class_name parameter, e. g. ScatterplotLayer or GeoJsonLayer. Usually you will not use the generic function but one of the add_*_layer shortcut functions instead:

# Generic function
deckgl() %>%
  add_layer("ArcLayer", "arc-layer", data, properties)

# Shortcut function
deckgl() %>%
  add_arc_layer("arc-layer", data, properties)

Run examples

You can run the api-examples from the add_*_layer functions with example(add_*_layer). For the IconLayer it looks like this:

# Sys.setenv(MAPBOX_API_TOKEN = "yourSuperSecretApiToken")
example(add_icon_layer)

Concept

Deckgl for R stays as close as possible to the JavaScript API so that usually all parameters of its JavaScript pendants are supported. Therefore, you need to check the deckgl-api-reference of the JavaScript framework to get information about the parameters you can pass to the R-functions mostly as named lists or named arguments (... parameter). Use the JS function if you need to pass any kind of JavaScript code, as it is the case for data accessors.

GridLayer API-example:

// JavaScript code

const layer = new GridLayer({
  id: "grid-layer",
  data: data,
  extruded: true,
  cellSize: 200,
  elevationScale: 4,
  getPosition: d => d.COORDINATES,
});
# Corresponding R code

deck <- deckgl() %>%
  add_grid_layer(
    id = "grid-layer",
    data = data,
    extruded = TRUE,
    cellSize = 200,
    elevationScale = 4,
    getPosition = JS("d => d.COORDINATES")
  )

In this example all properties are passed as named arguments.

You can test your R code like this:

properties <- list(
  extruded = TRUE,
  cellSize = 200,
  elevationScale = 4,
  getPosition = get_property("COORDINATES")
)

htmlwidgets:::toJSON2(properties, pretty = TRUE)
#> {
#>   "extruded": true,
#>   "cellSize": 200,
#>   "elevationScale": 4,
#>   "getPosition": "data => data.COORDINATES"
#> }

Documentation

Copy Link

Version

Install

install.packages('deckgl')

Monthly Downloads

285

Version

0.2.5

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Stefan Kuethe

Last Published

March 16th, 2020

Functions in deckgl (0.2.5)

add_arc_layer

Add an arc layer to the deckgl widget
add_geojson_layer

Add a geojson layer to the deckgl widget
add_grid_cell_layer

Add a grid cell layer to the deckgl widget
add_great_circle_layer

Add a great circle layer to the deckgl widget
add_basemap

Add a basemap to the deckgl widget
add_heatmap_layer

Add a heatmap layer to the deckgl widget
add_grid_layer

Add a grid layer to the deckgl widget
add_path_layer

Add a path layer to the deckgl widget
add_h3_cluster_layer

Add a h3 cluster layer to the deckgl widget
add_point_cloud_layer

Add a point cloud layer to the deckgl widget
add_hexagon_layer

Add a hexagon layer to the deckgl widget
add_text_layer

Add a text layer to the deckgl widget
add_bitmap_layer

Add a bitmap layer to the deckgl widget
get_last_element

Create a data accessor retrieving the last element of an array
get_first_element

Create a data accessor retrieving the first element of an array
add_scatterplot_layer

Add a scatterplot layer to the deckgl widget
add_screen_grid_layer

Add a screen grid layer to the deckgl widget
bart_segments

bart segments
add_column_layer

Add a column layer to the deckgl widget
reexports

Objects exported from other packages
add_h3_hexagon_layer

Add a h3 hexagon layer to the deckgl widget
update_deckgl

Send commands to a deckgl instance in a Shiny app
add_icon_layer

Add an icon layer to the deckgl widget
add_layer

Add any kind of layer to the deckgl widget
deckgl-shiny

Shiny bindings for deckgl
add_contour_layer

Add a contour layer to the deckgl widget
add_line_layer

Add a line layer to the deckgl widget
add_data

Add JavaScript data file
deckgl_proxy

Create a deckgl proxy object
deckgl

Create a deckgl widget
use_carto_style

Use a Carto style
get_color_to_rgb_array

Create a getColor data accessor
add_polygon_layer

Add a polygon layer to the deckgl widget
add_mapbox_basemap

Add a base map from mapbox to the deckgl widget
bart_stations

bart stations
does_it_work

Check if everything works fine
encode_icon_atlas

Encode atlas image to base64
use_icon_definition

Icon definition on an atlas image
sf_bike_parking

sf bike parking
use_contour_definition

Use contour definition
get_data

Get data
use_default_icon_properties

Use default icon properties
get_position

Create a getPosition data accessor
add_raster_tile_layer

Add a raster tile layer to the deckgl widget
get_property

Create a data accessor