如何动态更改已存在多边形的颜色

时间:2018-03-14 19:21:14

标签: javascript r shiny leaflet

所以我在Shiny with Leaflet for R中构建了一个交互式的等值区域图。它使用了美国所有县的形状文件,并将它们绘制为多边形。此外,我想展示这些县的特点(失业率,没有高中教育的百分比等)

我不想每次想要显示新特征时重绘多边形,而是希望更新多边形的基础颜色。

library(shiny)
library(leaflet)
library(dplyr)
ui <- fluidPage("Mapping Characteristics of the USA",
               fluidRow(
                        column(10, leafletOutput("map")),
                        column(2, radioButtons(inputId = "radio", selected=character(0), label="Characteristics",
                                               choiceNames = c("Unemployment Rate", "% w/out high school education", "% w/out insurance"),
                                               choiceValues = c("uer", "no_hs_per", "no_insur")))))

server <- function(input, output, session)
{
  #Render the map
  output$map = renderLeaflet({leaflet(counties)})

   observeEvent(input$radio,
           { 
             #Create color function
             pal = colorBin(palette = topo.colors(10), domain = counties[[input$radio]], pretty=TRUE)

             #Redraw Map
             m = leafletProxy("map", session=session, data=counties) %>% clearShapes() %>%
               addPolygons(stroke=TRUE, weight=1, fillOpacity = 0.5, color = pal(counties[[input$radio]]))
           }) #End of observeEvent(input$radio, ...)
}

# Run the application 
shinyApp(ui = ui, server = server)

你会在上面的代码中注意到,每当用户点击单选按钮以查看映射的新特征时,我必须绘制一组新的多边形(即使它是同一组多边形,也就是说美国的县)。我希望能够重新着色多边形,因为地图的更新速度有点慢。

经过一些搜索后,似乎在R的传单中不存在重新着色多边形的能力。它确实存在于Javascript的传单中。

使用传单更改Javascript中的颜色的示例: Dynamically change the color of a polygon in leaflet?

https://gis.stackexchange.com/questions/78069/how-do-i-set-the-fillcolor-of-polygons-in-leaflet-js-dynamically

我的问题是,有人可以告诉我如何将用于将多边形颜色更改为闪亮应用的javascript代码注入?

0 个答案:

没有答案
相关问题