Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crazycapivara/r2deck
An R Interface to deck.gl and Mapbox GL Visualizations
https://github.com/crazycapivara/r2deck
deck-gl htmlwidgets mapbox-gl maps r
Last synced: 3 months ago
JSON representation
An R Interface to deck.gl and Mapbox GL Visualizations
- Host: GitHub
- URL: https://github.com/crazycapivara/r2deck
- Owner: crazycapivara
- License: other
- Created: 2019-11-10T13:10:06.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T11:35:30.000Z (almost 2 years ago)
- Last Synced: 2024-05-14T12:31:18.330Z (6 months ago)
- Topics: deck-gl, htmlwidgets, mapbox-gl, maps, r
- Language: R
- Size: 1.06 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- awesome-shiny-extensions - r2deck - R interface to Deck.gl and Mapbox GL visualizations. (Visualization / Maps and Spatial Data)
README
---
output: github_document
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# _r2deck_ - An R Interface to deck.gl and Mapbox GL Visualizations[![Travis build status](https://travis-ci.org/crazycapivara/r2deck.svg?branch=master)](https://travis-ci.org/crazycapivara/r2deck)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![CRAN status](https://www.r-pkg.org/badges/version/r2deck)](https://cran.r-project.org/package=r2deck)The _r2deck_ package makes it possible to visualize your R data in [deck.gl](https://deck.gl/) and
[Mapbox GL](https://github.com/mapbox/mapbox-gl-js). It was inspired by the [r2d3](https://rstudio.github.io/r2d3/) package. In contrast to other existing packages, it is a low-level interface giving you a high degree of flexibility regarding the customization of your visualization.## Installation
You can install the latest version of _r2deck_ from [GiHub](https://github.com) with:
``` r
remotes::install_github("crazycapivara/r2deck")
```## Getting Started
To visualize your data you have to create a JavaScript file containing your visualization function called `r2deckViz`. The JavaScript visualization function takes 3 parameters:
* `map` - The map object on which the layers will be rendered, either of type `deck.DeckGL` or `mapboxgl.Map`.
This object is automatically created.
* `data` - The data that is passed from R to the vizualisation function.
* `options` - Additional options that can be passed from R to the vizualisation function.Furthermore, the libraries/variables `deck` and `mapboxgl` are available through the global context.
The JavaScript file is then passed to the `r2deck` or `r2mapbox` function in R together with the data and parameters to style the map object.
So let's take a look at a basic example.
First fetch some flight data:
```{r data}
data_url <- paste0(
"https://raw.githubusercontent.com/plotly/datasets/master/",
"2011_february_aa_flight_paths.csv"
)
flights <- data.table::fread(data_url)
head(flights)
```Then create your visualization function to render an arc layer like this:
``` javascript
// arc.js
function r2deckViz(map, data, options) {
// Create an arc layer
const arcLayer = new deck.ArcLayer({
id: "arc-layer",
data: data,
getSourcePosition: data => [data.start_lon, data.start_lat],
getTargetPosition: data => [data.end_lon, data.end_lat],
getSourceColor: data => [64, 255, 0],
getTargetColor: data => [0, 128, 200]
});// Add the layer to the map
map.setProps({
layers: [ arcLayer ]
});
}
```Finally, send the data to your visualization function:
``` r
library(r2deck)r2deck(
script = "arc.js",
data = flights,
# viewport parameters that are passed to the deck/map object
lng = -87.6500523,
lat = 41.850033,
zoom = 2,
pitch = 45
)```
For further examples take a look at the [example scripts](inst/examples) of the package.
## Mapbox Access Token
In order to use mapbox styles you need to put your access token in an environment variable called `MAPBOX_ACCESS_TOKEN`. If not set globally, you can run:
``` r
Sys.setenv(MAPBOX_ACCESS_TOKEN = "your-token")
```## _sf_ data objects
It is straight forward to pass [sf](https://github.com/r-spatial/sf) data objects to your visualization function. Just tell the data accessor to get the geometries from the _geometry_ column:
``` javascript
const polygonLayer = new PolygonLayer({
id: "nc",
data: data,
getPolygon: data => data.geometry,
// ...
}
```See also the [polygon layer example](inst/examples/polygon-layer.R).
## Documentation
The documentation is still work in progress as this package is in an early state.
As a good starting point check the [deck.gl api documentation](https://deck.gl/#/documentation/deckgl-api-reference/) where you have a lot of examples on how your JavaScript visualization function should look like.
Basically you just need to define one or more layers using your data object that is passed via `r2deck` to your function and then add it to the map:
``` javascript
function r2deckViz(map, data, options) {
const gridLayer = new deck.GridLayer({
id: "grid-layer",
data: data,
// ...
});
map.setProps({
layers: [ gridLayer ]
});
}// Columns from your data are accessed like this:
data.column_name// If you have the columns 'lat' and 'lng' in your data.frame
// a data accessor would be:
getPosition: data => [data.lng, data.lat]
```