Git Product home page Git Product logo

deckgl's Introduction

r-deckgl: An R Interface to deck.gl

CRAN_Status_Badge github_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

The r-deckgl package makes the open-source JavaScript library deck.gl available within R via the htmlwidgets package.

vancouver-blocks

Installation

install.packages("deckgl")

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

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

Quickstart

library(deckgl)

Create a deckgl instance:

deckgl()

Add a basemap:

deckgl() %>%
  add_basemap()

Add any kind of layers:

# Grid layer example
data("sf_bike_parking")

props <- list(
  extruded = TRUE,
  cellSize = 200,
  elevationScale = 4,
  getPosition = ~lng + lat,
  tooltip = "Count: {{count}}"
)

deckgl(zoom = 11, pitch = 45) %>%
  add_basemap() %>%
  add_grid_layer(
    data = sf_bike_parking,
    properties = props
  )

The deckgl Instance

The deckgl function creates the widget / renderer to which you add layers and other configuration parameters:

rdeck <- deckgl(
  latitude = 37.8,
  longitude = -122.45,
  zoom = 12
) %>%
  add_grid_layer(
    data = data,
    properties = props
  )

Layers

Due to the generic function add_layer any kind of layer defined in the deck.gl Layer Catalog is supported. The layer type 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 shortcuts instead:

# Generic function
deckgl() %>%
  add_layer("ArcLayer", id, data, properties)

# Shortcut function
deckgl() %>%
  add_arc_layer(id, data, properties)

Data

The data parameter can either be an url to fetch data from or a data object. In most cases you will pass an object of type data.frame to the layers. Use the formula syntax to define data accessors that deck.gl uses to access the properties of the data object:

props <- list(
  getPosition = ~lng + lat
  # ...
)

sf Data Objects

An object of class sf is a data.frame with a geometry list-column. Set the layer prop that fetches the geometry to the geometry list-column of your sf object:

# Example: PolygonLayer

props <- list(
  getPolygon = ~geometry
  # ...
)

Sources

With add_source you can add a source to the widget that can be used accross layers:

data("bart_stations")

deckgl() %>%
  add_source("bart-stations", bart_stations) %>%
  add_scatterplot_layer(
    source = "bart-stations",
    getPosition = ~lng + lat,
    # ...
  ) %>%
  add_text_layer(
    source = "bart-stations",
    getPosition = ~lng + lat,
    # ...
  ) %>%
  add_basemap()

Please note that you use the parameter source instead of data.

Layer Props

Layer properties are passed to the add_*_layer functions either as named list by the properties argument or as named parameters / keyword arguments via the ... parameter. The names correspond to the properties of the deck.gl counterparts. Therefore, please see the deck.gl Layer Catalog to determine the available parameters for the used layer. You can also pass a props list and keyword arguments together. Identical properties are overwritten by the latter ones.

Grid Layer example:

// JavaScript code

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

# using named arguments
deck <- deckgl() %>%
  add_grid_layer(
    id = "grid-layer",
    data = data,
    extruded = TRUE,
    cellSize = 200,
    elevationScale = 4,
    getPosition = ~lng + lat
  )

# ... using a named props list
props <- list(
  cellSize = 200,
  extruded = TRUE,
  # ...
)

deckgl() %>%
  add_grid_layer(
    data = data,
    properties = props
  )

Camels or Snakes

According to the style conventions in R, camelCased parameters in deck.gl can also be passed as snake_cased parameters in R. For example, getPosition can be passed to deck.gl as get_position:

deckgl() %>%
  add_grid_layer(
    get_position = ~lng + lat,
    cell_size = 200,
    # ...

  )

Data Accessors

Use the formula syntax to define data accessors:

props <- list(
  getPosition = ~lng + lat # js: d => [d.lng, d.lat]
  getFillColor = ~color # js: d => d.color
  # ...
)

The example above assumes that your data contains the columns lng, lat and color.

It is also possible to pass JavaScript code by using the JS function in R:

props <- list(
  getColor = JS("d => d.capital ? [140, 10, 10] : [60, 10, 10]")
  # ...
)

Colors

In deck.gl colors are represented by [r, g, b, a] arrays. In R you can pass hex color codes or color names to all color props of the add_*_layer functions. They are automatically converted to the required format:

deckgl() %>%
  add_grid_layer(
    colorRange = RColorBrewer::brewer.pal(6, "Blues"),
    # ...
  )

Tooltips

The tooltip for a layer can be set via the tooltip parameter. You can either pass a single template string or a list with the following properties (see also use_tooltip):

  • html: A template string that will be set as the innerHTML of the tooltip.
  • style: A cssText string that will modefiy the default style of the tooltip.

Tooltip Template Syntax

The tooltip string is a so called “mustache” template in which variable names are identified by the double curly brackets that surround them. The variable names available to the template are given by deck.gl’s pickingInfo.object and vary by layer.

Arc Layer example:

data("bart_segments")

props <- list(
  getWidth = 12,
  getSourcePosition = ~from_lng + from_lat,
  getTargetPosition = ~to_lng + to_lat,
  getSourceColor = "yellow",
  getTargetColor = "orange",
  tooltip = use_tooltip(
    html = "{{from_name}} to {{to_name}}",
    style = "background: steelBlue; border-radius: 5px;"
  )
)

deckgl(zoom = 9.5, pitch = 35) %>%
  add_arc_layer(data = bart_segments, properties = props) %>%
  add_basemap()

See mustache.js for a complete syntax overwiew.

Controls

Controls are displayed as overlays on top of the map / deck. Usually you can set the position and the style of the control. The most basic control is a simple text box:

deckgl() %>%
  add_basemap() %>%
  add_control(
    html = "Plain Base Map",
    pos = "top-right",
    style = "background: steelblue; color: white"
  )

JSON Editor

You can add an instance of the ace editor in JSON mode to the map by using add_json_editor:

deckgl() %>%
  add_grid_layer(
    # ...
  ) %>%
  add_json_editor()

This allows you to change your layer props on the fly. You can toggle the visibility of the editor by pressing “e”.

Legends

With add_legend you can add a custom legend to your widget:

deckgl() %>%
  add_basemap() %>%
  add_legend(
    colors = c("yellow", "orange"),
    labels = c("Cake", "Icecream"),
    title = "Sweets"
  )

In most cases, you will create the legend automatically using a palette function:

data_column <- 1:10
pal <- scales::col_bin("Blues", data_column, bins = 5)
deckgl() %>%
  add_basemap() %>%
  add_legend_pal(pal, title = "Blues")

Basemaps

By default, add_basemap adds a carto basemap to the widget.

To use basemaps from mapbox it is recommended that you store your API access token in an environment variable called MAPBOX_API_TOKEN:

# If not set globally
#Sys.setenv(MAPBOX_API_TOKEN = "xyz")

deckgl() %>%
  add_mapbox_basemap("mapbox://styles/mapbox/light-v9")

Run Examples

You can run the API examples from the add_*_layer functions with example(add_*_layer):

example(add_grid_layer)

Shiny Integration

With the renderDeckgl and deckglOutput functions you can use r-deckgl in shiny applications:

library(shiny)
library(deckgl)

backend <- function(input, output) {
  output$rdeck <- renderDeckgl({
    deckgl() %>%
      add_grid_layer(
        data = sf_bike_parking,
        getPosition = ~lng + lat,
        cellSize = 400,
        pickable = TRUE
      ) %>%
    add_basemap()
  })
}

frontend <- fluidPage(
  deckglOutput("rdeck")
)

shinyApp(frontend, backend)

To update a deckgl instance use deckgl_proxy in combination with update_deckgl.

Furthermore, the onclick event sends deck.gl’s picking info object to your shiny application and updates the corresponding input in the form of input$widget_id_onclick. For example, if the widget id is rdeck, you can access the pickingInfo object with input$rdeck_onclick:

backend < -function(input, output) {
  # ...
  observeEvent(input$rdeck_onclick, {
    info <- input$rdeck_onclick
    print(info$object)
  })
}

Development

The JavaScript library of r-deckgl uses webpack as module bundler. Therefore, you need node.js to build the module. All JavaScript code is located in the javascript/src folder and test components go to javascript/src/test-components.

Install deps and build the library from inside the javascript folder with:

npm install

npm run build

To spin up the webpack-dev-server run:

npm run start

Documentation

Note

If the deckgl widget is not visible in the viewer pane of RStudio, just open it in your browser by clicking “Show in new window” and everything will be fine.

deckgl's People

Contributors

crazycapivara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

deckgl's Issues

Can't display columns using add_layer and ColumnLayer

I was trying to use add_layer function to display ColumnLayer, but nothing shows in the Shiny app.

    properties <- list(
        extruded = TRUE,
        radius = 100,
        elevationScale = 4,
        getElevation = JS("d => d.total_lots"),
        getPosition = JS("d => d.geometry"),
        getTooltip = JS("object => `${object.centroid.join(', ')}<br/>Count: ${object.points.length}`"),
        fixedTooltip = TRUE
    )

    output$distPlot <- renderDeckgl({
        deckgl(latitude = 1.3521, longitude = 103.8198, pitch = 45) %>%
            add_layer(class_name = "ColumnLayer", id="column-layer", data = sf, properties = properties) %>%
            add_mapbox_basemap(style = "mapbox://styles/mapbox/dark-v10")
    })

Use default icon properties

Add default icon properties (iconAtlas, iconMapping and getColor) when the property iconAtlas is not set in add_icon_layer.

how create coordinates that include c(lng,lat) in a column?

@crazycapivara hi,

Something's bothering me

when I run the code:
url0 <- paste0(
"https://raw.githubusercontent.com/",
"uber-common/deck.gl-data/",
"master/website/bart-stations.json"
)
sample_data<-fromJSON(url0)

and I get a dataframe like this:

head(sample_data)
name code address
1 Lafayette (LAFY) LF 3601 Deer Hill Road, Lafayette CA 94549
2 12th St. Oakland City Center (12TH) 12 1245 Broadway, Oakland CA 94612
3 16th St. Mission (16TH) 16 2000 Mission Street, San Francisco CA 94110
4 19th St. Oakland (19TH) 19 1900 Broadway, Oakland CA 94612
5 24th St. Mission (24TH) 24 2800 Mission Street, San Francisco CA 94110
6 Ashby (ASHB) AS 3100 Adeline Street, Berkeley CA 94703
entries exits coordinates
1 3481 3616 -122.123801, 37.893394
2 13418 13547 -122.271604, 37.803664
3 12409 12351 -122.419694, 37.765062
4 13108 13090 -122.269029, 37.807870
5 12817 12529 -122.418466, 37.752254
6 5452 5341 -122.269780, 37.853024

the coordinates include lng and lat
How am I going to generate new coordinates like that ?

ps: raw data as below

head(dt[,.(vin,lng,lat)])
vin lng lat
1: L6T792423JN451621 121.2295550 30.33141889
2: L6T792423JN451621 121.2368408 30.34006500
3: L6T792423JN451621 121.6590389 29.73693583
4: L6T792423JN451621 121.6553528 29.73500694
5: L6T792423JN451621 121.2299058 30.33186083
6: L6T792423JN451621 121.2268678 30.32662194

or https://crazycapivara.github.io/deckgl/articles/icon-layer.html
when I creatte icon-layer, is data must be JSON?

Thanks a million
Thanks a million

Add tests

  • default icon / icon-layer
  • tooltips
  • deckglWidget

similar packages

Looks like we have similar packages, your deckgl and my mapdeck. Maybe there's a way we can combine efforts?

similar packages

Looks like we have similar packages, your deckgl and my mapdeck. Maybe there's a way we can combine efforts?

Use 'initialViewState'

Use initialViewState instead of deprecated latitude, longitude etc properties when creating new DeckGL object.

Add new layers

Add new layers supported in version 7 (see #83)

  • bitmap layer
  • h3 hexagon layer
  • h3 cluster layer
  • great circle layer
  • raster tile layer
  • column layer
  • vector tile layer

Detect sf column

Detect sf column, if data is supplied as sf object: attr(x, "sf_column")

Add debug mode

Add argument to deckgl widget to enable js debug mode (logs)

Update README

Add examples for different data types:

  • data url
  • data.frame
  • sf object

Add Dockerfile

  • Create an rstudio docker container with deckgl and example scripts
  • Maybe in conjunction with a graphhopper container for some of the examples

Add startup message

Add startup message with deckgl-js version number and link to documentation.

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.