是否可以在 Shiny(传单之外)访问 R Leaflet 图层控件? [英] Is it possible to access R Leaflet layer controls in Shiny (outside of leaflet)?

查看:12
本文介绍了是否可以在 Shiny(传单之外)访问 R Leaflet 图层控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个类似于

您可以轻松地在传单地图对象内的蓝色和红色图层之间切换.但是,假设当我将地图图层从蓝色切换到红色时,我想要一个闪亮的表格来更新红色多边形图层的属性.我希望能够将此对象拉到传单之外并在闪亮的 observeEvent 中使用它.这可能吗/我该怎么做?

解决方案

您可以在 Shiny 服务器中为 {MAP_ID}_groups 输入定义一个观察者.

例子:

server <- function(input, output, session) {# ...output$my_map <- renderLeaflet({# ...})观察({selected_groups <- req(输入$my_map_groups)# 随便...})}

当用户在图层控件中选择一个组时,此输入会更新.

I'm working on creating a Shiny/Leaflet app similar to this one that's done in tableau. It shows world-level views of poverty for different years, allowing the user to filter the map by variable, region, and year.

The problem is that the global country-level shapefile (from NaturalEarthData) renders quite slowly. I'm working on different ways to simplify those polygons to decrease load time, but in the meantime, I'm working on other potential solutions.

Ideally, I would use Shiny controls to toggle the different map layers and use leafletProxy to update the map. But because each layer change draws the entire map again, this is also quite slow.

When I include the different layers inside Leaflet, the layers are rendered much, much faster. (I assume that this is because the addLayersControl option in Leaflet only changes the fillColor of the polygons rather than redrawing the entire global shapefile, as is done with leafletProxy). But is there any way to access these layers outside of Leaflet?

To illustrate, here's some dummy code:

#load required libraries 
library(shiny)
library(leaflet)
library(raster)

#begin shiny app
shinyApp(

  ui <- fluidPage(
    leafletOutput("map", width = "100%", height = 600) 
  ), #END UI

  server <- function(input, output, session){

    #load shapefile
    rwa <- getData("GADM", country = "RWA", level = 0)

    #render map
    output$map <- renderLeaflet({
      leaflet() %>% 
        addTiles() %>% 
        addPolygons(data = rwa, 
                    fillColor = "blue", 
                    group = "blue") %>% 
        addPolygons(data = rwa, 
                    fillColor = "red", 
                    group = "red") %>% 
        addLayersControl(baseGroups = c("blue", "red"), 
                         options = layersControlOptions(collapsed = F))
    }) #END RENDER LEAFLET 
  } #END SERVER
) #END SHINY APP

Which has the following output:

You can easily toggle between the blue and red layers within the leaflet map object. But let's say that I want a Shiny table to update with the attributes from the red polygon layer when I toggle the map layers from blue to red. I want to be able to pull this object outside of leaflet and utilize it in a Shiny observeEvent. Is this possible/how can I do this?

解决方案

You can define an observer for the {MAP_ID}_groups input in your Shiny server.

Example:

server <- function(input, output, session) {
    # ...

    output$my_map <- renderLeaflet({
        # ...
    })

    observe({
        selected_groups <- req(input$my_map_groups)
        # do whatever ... 
    })
}

This input gets updated when the user selects a group in the layers control.

这篇关于是否可以在 Shiny(传单之外)访问 R Leaflet 图层控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆