Learn R Programming

toro (version 0.1.0)

get_clicked_feature: Get the sf data frame of a clicked feature from the map widget

Description

The click input is a list containing the layerId, properties, geometry, and time. Turn this into an sf object.

Usage

get_clicked_feature(clicked_feature_input)

Value

A sf object representing the clicked feature, or NULL.

Arguments

clicked_feature_input

A list representing the clicked feature.

Details

To get the clicked feature, use input$map_feature_click in a Shiny app, where map is the ID of your map output. This input will contain a list representing the clicked feature whenever a feature is clicked on the map. Pass this input to get_clicked_feature() to convert it into an sf object for easier manipulation in R.

Examples

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

nz_data <- spData::nz_height |>
  sf::st_transform(4326)

ui <- fluidPage(
 tagList(
   mapOutput("map")
 )
)
server <- function(input, output, session) {
 output$map <- renderMap({
   map() |>
     set_bounds(bounds = nz_data) |>
     add_circle_layer(
       id = "nz_elevation",
       source = nz_data
     )
 })

 # Print the clicked feature as an sf object
 observe({
   req(input$map_loaded, input$map_feature_click)
   print(get_clicked_feature(input$map_feature_click))
 }) |>
   bindEvent(input$map_feature_click)
}
}

Run the code above in your browser using DataLab