是否可以访问Shiny中(小叶外部)的R小叶层控件? [英] Is it possible to access R Leaflet layer controls in Shiny (outside of leaflet)?

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

问题描述

我正在创建类似于这是在Tableau中完成的.它显示了世界不同年份的贫困状况视图,使用户可以按变量,地区和年份过滤地图.

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.

问题在于,全球国家/地区级shapefile(来自NaturalEarthData)渲染速度非常慢.我正在研究简化这些多边形以减少加载时间的不同方法,但是与此同时,我正在研究其他可能的解决方案.

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.

理想情况下,我将使用Shiny控件切换不同的地图图层,并使用leafletProxy更新地图.但是因为每个图层的更改都会再次绘制整个地图,所以这也很慢.

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.

当我在内部的 小叶中包含不同的图层时,这些图层的渲染速度要快得多. (我认为这是因为Leaflet中的addLayersControl选项仅更改了多边形的fillColor,而不是像leafletProxy那样重新绘制了整个全局shapefile).但是,有什么方法可以在Leaflet之外访问这些层吗?

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

具有以下输出:

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

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?

推荐答案

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

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

示例:

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小叶层控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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