Learn R Programming

toro (version 0.1.0)

set_source_data: Set data for a source on the map

Description

Set data for a source on the map

Usage

set_source_data(proxy, source_id, data, type = "geojson")

Value

The map proxy object for chaining.

Arguments

proxy

The map proxy object created by mapProxy().

source_id

The ID of the source to update.

data

The data for the source, typically in GeoJSON format.

type

The type of the source. Default is "geojson". Other options include "vector" or "raster".

Examples

Run this code
if(interactive()){
library(shiny)
library(sf)
library(toro)

data(quakes)
quakes_data <- sf::st_as_sf(quakes, coords = c("long", "lat"), crs = 4326)

ui <- fluidPage(
 tagList(
   mapOutput("map"),
   actionButton("btn", "Update Source Data")
 )
)
server <- function(input, output, session) {
 get_random_points <- function(data, n = 10) {
   random_rows <- runif(n, min = 1, max = nrow(data))
   return(data[random_rows, ])
 }

 output$map <- renderMap({
   map() |>
     add_symbol_layer(
       id = "quakes",
       source = get_random_points(quakes_data)
     )
   })
 observe({
   mapProxy("map") |>
     set_source_data(
       source_id = "source-quakes",
       data = get_random_points(quakes_data)
     )
 }) |>
 bindEvent(input$btn)
}
}

Run the code above in your browser using DataLab