Git Product home page Git Product logo

deckgl's Introduction

An R Interface to deck.gl

CRAN_Status_Badge Travis-CI Build Status Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. deck.gl Version

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

deckgl's People

Contributors

crazycapivara avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.