R闪亮传单如何根据缩放级别更改圆圈大小

  • 本文关键字:缩放 单如何 r shiny leaflet
  • 更新时间 :
  • 英文 :


我正在创建一个带有传单地图的Shiny应用程序。它上面画了成千上万个小圆圈,圆圈的颜色告诉了一个故事。缩小后,圆会保持较小(权重=1(,因此重叠较少。但是,当用户放大时,很难看到小圆圈。我希望圆圈的大小根据缩放级别而增加。我知道有一个输入$MAPID_zoom功能,它以整数形式返回缩放级别。

以下是一些可复制的代码,将德克萨斯州达拉斯的15个随机点绘制在缩放设置为6的传单地图上。当缩放级别增加到8时,如何将圆的权重从1更改为2,当缩放级别提高到10时,如何从2更改为3?

我在网上看到了一些讨论,但没有任何对我有用的内容。请参阅下面的代码。提前谢谢。

## app.R ##
library(leaflet)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
leafletOutput("Map", width = "100%", height = "500px")

)
)
server <- function(input, output) {
df <- data.frame("Lat"=c(32.921821,32.910853,32.793803,32.995084,32.683745,32.759999,32.800652,32.958861,32.835963,32.762578,32.649651,32.862843,32.862217,32.936876,32.963381),
"Long"=c(-96.840609,-96.738831,-96.689232,-96.857858,-96.825345,-96.684475,-96.794144,-96.816111,-96.676371,-96.897331,-96.944426,-96.754719,-96.856976,-96.752718,-96.770249))
output$Map <- renderLeaflet({
leaflet(df) %>%
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 = -96.84, lat = 32.92, zoom = 6) %>%
addCircles(lng = ~Long, lat = ~Lat, weight = 1,
opacity = 1, fill = TRUE,  fillOpacity = 1 )
})
}
shinyApp(ui, server)

好吧,这比我想象的要复杂得多,但我终于开始工作了。有一个观察者是关键。我还必须了解什么是leafletProxy(((如果你不知道的话,可以研究一下(。最后,在观察者中使用clearShapes((清除形状是在放大和缩小时实现这一功能的关键。请参阅下面的代码。

## app.R ##
library(leaflet)
library(shinydashboard)
library(shinydashboardPlus)
library(dplyr)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
leafletOutput("map", width = "100%", height = "500px")

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

df <- data.frame("Lat"=c(32.921821,32.910853,32.793803,32.995084,32.683745,32.759999,32.800652,32.958861,32.835963,32.762578,32.649651,32.862843,32.862217,32.936876,32.963381),
"Long"=c(-96.840609,-96.738831,-96.689232,-96.857858,-96.825345,-96.684475,-96.794144,-96.816111,-96.676371,-96.897331,-96.944426,-96.754719,-96.856976,-96.752718,-96.770249))

observeEvent(
eventExpr = input$map_zoom, {
print(input$map_zoom)           # Display zoom level in the console
leafletProxy(
mapId = "map", 
session = session
)%>% clearShapes() %>%
addCircles(data=df,lng = ~Long, lat = ~Lat, 
weight = case_when(input$map_zoom <=4 ~1, 
input$map_zoom ==5 ~2, 
input$map_zoom ==6 ~3, 
input$map_zoom ==7 ~5, 
input$map_zoom ==8 ~7, 
input$map_zoom ==9 ~9, 
input$map_zoom >9 ~11),
opacity = 1, fill = TRUE, fillOpacity = 1 )
}
)

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 = -96.84, lat = 32.92, zoom = 6) 
})
}
shinyApp(ui, server)

试试这个

output$Map <- renderLeaflet({
new_zoom <- 6
wt <- 2
if(!is.null(input$Map_zoom)) new_zoom <- input$Map_zoom

leaflet(df) %>%
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 = -96.84, lat = 32.92, zoom=new_zoom ) %>%
addCircles(lng = ~Long, lat = ~Lat, weight = wt, # radius=radius,
opacity = 1, fill = TRUE,  fillOpacity = 1 )
})

最新更新