2017-03-17 40 views
0

我正在使用閃亮和小冊子的工具: 我希望當客戶點擊增值稅(請參閱例如NE的UI代碼)時,地圖會轉到另一個視圖以供參考像這樣的純傳單:小冊子R閃亮:選擇和縮放

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

這裏我R_code(預先感謝您的回答和支持;)


#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 
+0

因爲它是現在,這是不可複製的,我們應該從'至少有一個摘錄data'並有可能在'gomap.js'代碼。 – GGamba

回答

0

要更新,你應該使用leafletProxy一個leafletMap,你可以閱讀有關here

這個想法是,您需要有一個reactive值在更改選擇時更新,值由leafletProxy及其值用於執行更新。

它應該是這樣的:

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) 
    }) 
+0

我的數據集包括經度和緯度,以及一些變量,如Canton等 –

+0

沒有一些示例數據,我不能做任何事情比這更 – GGamba

+0

我更新我的例子,我希望它更有意義的U感謝您的幫助和時間;) –