传单 R 闪亮:选择 &飞涨 [英] Leaflet R shiny: select & zoom

查看:14
本文介绍了传单 R 闪亮:选择 &飞涨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有闪亮和传单的工具:我希望当客户单击 vars 时(参见示例 NE 的 UI 代码),地图转到另​​一个视图,例如在纯传单中这样的示例:

I'm working on a tool with shiny and leaflet: I want that when a customer click on the vars (see UI code for exemple NE), the map go to another view for exemple like this in pure leaflet:

  L.easyButton( '<strong>NE</strong>', function(){
  //zoomTo.setView([55, -2], 4);
  map.setView([46.95, 6.85], 12);
  }).addTo(map);

这是我的 R_code(提前感谢您的回答和支持;)

Here my R_code (thank you in advance for your answer and support;)

 #UI:
 library(leaflet)

Choices for drop-downs
vars <- c(
"NE" = "NE",
"VD" = "VD",
"VS" = "VS",
"JU" = "JU",
"BE" = "BE",
"GE" = "GE")


navbarPage("Près de chez toi ciao.ch", id="nav",

tabPanel("Interactive map",
div(class="outer",

  tags$head(
    # Include our custom CSS
    includeCSS("styles.css"),
    includeScript("gomap.js")
  ),

  leafletOutput("map", width="100%", height="100%"),

  # Shiny versions prior to 0.11 should use class="modal" instead.
  absolutePanel(id = "controls", class = "panel panel-default", fixed =             TRUE,
    draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
    width = 330, height = "auto",

    h2("Rechercher "),

    selectInput("canton", "Canton", data$canton,selected = "" )


  ),

  tags$div(id="cite",
    "sddssd"
  )
  )
 ),

tabPanel("Adresses",
                  dataTableOutput('mytable')    
)
)

<小时>

   #SERVER:
         data <- read_csv("~/Desktop/superzip r/Sans titre 2.csv")
    function(input, output, session) {

 ## Interactive Map ###########################################

 # Create the map
 output$map <- renderLeaflet({
 leaflet() %>%
  addTiles(
 urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-        5ebohr46/{z}/{x}/{y}.png",
    attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
  ) %>%
  setView(lng =  6.6328200, lat = 46.5160000, zoom = 12)%>%
  addMarkers(data,lng=data$Longitude,lat=data$Latitude,label =    data$nom)

 })


 ###observation
observe({
canton<-input$canton
 })
output$mytable = renderDataTable({
       data
      },options = list(
        autoWidth = TRUE,
        columnDefs = list(list(width = '200px', targets = "_all"))))}

<小时>

 #data
 1           Genève     GE 022 329 11 69                                                          www.fegpa.ch  6.164722 46.19853
 2 Chavannes-près-Renens     VD 021 633 44 32                                                        croix-bleue.ch  6.575761 46.53280
 3              Lausanne     VD 021 623 84 84                                                            www.fva.ch  6.611342 46.52284
 4             Neuchâtel     NE 032 889 62 10                                                  http://www.cenea.ch/  6.909872 46.98825
 5              Delémont     JU 032 421 80 80 http://www.addiction-jura.ch  6.411595 46.94195
 6              Lausanne     VD 021 321 29 11                                                www.addictionsuisse.ch  6.626040 46.51873

推荐答案

要更新一个leafletMap,你应该使用leafletProxy,你可以阅读这里.

To update a leafletMap you should use leafletProxy, you can read about that here.

这个想法是您需要在更改选择时更新一个 reactive 值,该 reactive 值由 leafletProxy 及其用于执行更新的值.

The idea is that you need to have a reactive value that is updated when you change the selection, that reactive value is observed by the leafletProxy and its values used to perform the update.

应该是这样的:

output$map <- renderLeaflet({
        leaflet(data) %>%
            addTiles(urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
                     attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
            ) %>% 
            setView(lng = 6.6328200, lat = 46.5160000, zoom = 12) %>% 
            addMarkers(data$long, data$lat, label = data$nom)            
    })

    center <- reactive({
        subset(data, nom == input$canton) 
        # or whatever operation is needed to transform the selection 
        # to an object that contains lat and long
    })

    observe({
        leafletProxy('map') %>% 
            setView(lng =  center()$long, lat = center()$lat, zoom = 12)
    })

这篇关于传单 R 闪亮:选择 &amp;飞涨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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