Shiny - 在传单地图中控制小部件

Pro*_*nob 2 html r leaflet shiny

我有一个简单的闪亮应用程序,只有一个下拉列表阿富汗地区和一张相同的传单地图。 在此处输入图片说明

可以通过此链接访问形状文件- 使用来自http://www.gadm.org/download 的AFG_adm2.shp

这是应用程序代码:

library(shiny)
library(leaflet)
library(rgdal)
library(sp)

afg <- readOGR(dsn = "data", layer ="AFG_adm2", verbose = FALSE, stringsAsFactors = FALSE)

ui <- fluidPage(
    titlePanel("Test App"),
    selectInput("yours", choices = c("",afg$NAME_2), label = "Select Country:"),
    actionButton("zoomer","reset zoom"),
    leafletOutput("mymap")

)

server <- function(input, output){
  initial_lat = 33.93
  initial_lng = 67.71
  initial_zoom = 5

  output$mymap <- renderLeaflet({
    leaflet(afg) %>% #addTiles() %>%
       addPolylines(stroke=TRUE, color = "#00000", weight = 1) 
  })

  proxy <- leafletProxy("mymap")

  observe({
    if(input$yours!=""){
      #get the selected polygon and extract the label point 
      selected_polygon <- subset(afg,afg$NAME_2==input$yours)
      polygon_labelPt <- selected_polygon@polygons[[1]]@labpt

      #remove any previously highlighted polygon
      proxy %>% removeShape("highlighted_polygon")

      #center the view on the polygon 
      proxy %>% setView(lng=polygon_labelPt[1],lat=polygon_labelPt[2],zoom=7)

      #add a slightly thicker red polygon on top of the selected one
      proxy %>% addPolylines(stroke=TRUE, weight = 2,color="red",data=selected_polygon,layerId="highlighted_polygon")
    }
  })

  observeEvent(input$zoomer, {
    leafletProxy("mymap") %>% setView(lat = initial_lat, lng = initial_lng, zoom = initial_zoom) %>% removeShape("highlighted_polygon") 
  })


}


# Run the application 
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

编辑:我实际上是在尝试添加一个操作按钮,将缩放重置为默认值(使用leafletproxy 和setview),我想将此按钮放在地图的右上角,而不是在地图上方。

我可以addLayersControl用来做这个吗?

编辑2:

完整应用中的代码:

# Create the map
    output$mymap <- renderLeaflet({
      leaflet(afg) %>% addTiles() %>%
        addPolygons(fill = TRUE,
                    fillColor = ~factpal(acdf$WP_2012), #which color for which attribute
                    stroke = TRUE, 
                    fillOpacity = 1, #how dark/saturation the fill color should be
                    color = "black", #color of attribute boundaries
                    weight = 1, #weight of attribute boundaies
                    smoothFactor = 1,
                    layerId = aid
                    #popup = ac_popup
        ) %>% addPolylines(stroke=TRUE, color = "#000000", weight = 1) %>%
        addLegend("bottomleft", pal = factpal, values = ~WP_2012,
                  title = "Party",
                  opacity = 1
        ) %>% setView(lng = initial_lng, lat = initial_lat, zoom = initial_zoom) %>%
        addControl(html = actionButton("zoomer1","Reset", icon = icon("arrows-alt")), position = "topright")
    })
Run Code Online (Sandbox Code Playgroud)

我看不到来自 的地图图块addTiles或来自的缩放重置按钮addControl。任何想法为什么会发生这种情况?

Nic*_*icE 5

您可以addControl直接使用该功能:

output$mymap <- renderLeaflet({
    leaflet(afg) %>% #addTiles() %>%
        addPolylines(stroke=TRUE, color = "#00000", weight = 1) %>%
        addControl(actionButton("zoomer","Reset"),position="topright")
})
Run Code Online (Sandbox Code Playgroud)