Skip to content

Instantly share code, notes, and snippets.

@MilesMcBain
Created July 8, 2022 01:31
Show Gist options
  • Save MilesMcBain/11fae340151847a19457eef7c64172ad to your computer and use it in GitHub Desktop.
Save MilesMcBain/11fae340151847a19457eef7c64172ad to your computer and use it in GitHub Desktop.
Demo of the {rdeck} feature editor with {shiny}
library(shiny)
library(rdeck)
library(sf)
sf::sf_use_s2(FALSE)
library(spData)
library(dplyr)
library(ggplot2)
bikes <-
spData::cycle_hire
ui <- fluidPage(
rdeckOutput(
outputId = "map"
),
plotOutput(
outputId = "plot"
)
)
server <- function(input, output, session) {
selected_bikes <- reactive({
st_intersection(bikes, get_edited_features("map"))
})
output$map <- rdeck::renderRdeck({
rdeck(
editor = TRUE,
initial_bounds = st_bbox(bikes)
) |>
add_scatterplot_layer(
data = bikes,
get_position = geometry,
get_radius = 2,
radius_units = "pixels",
get_fill_color = scale_color_linear(nbikes)
)
})
output$plot <- renderPlot({
plot_data <- selected_bikes()
plot_data |>
ggplot(aes(x = nbikes)) +
geom_histogram() +
theme_minimal()
})
observe({
rdeck_proxy(
id = "map"
) |>
add_scatterplot_layer(
id = "selected_bikes",
data = selected_bikes(),
get_position = geometry,
get_radius = 2,
radius_units = "pixels",
get_fill_color = "#0199cc"
)
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment