闪亮/传单:用户输入的choropleth贴图的颜色

时间:2018-11-30 16:10:25

标签: r shiny r-leaflet

我正在尝试构建一个可根据用户输入的变量来绘制Choropleth传单地图的应用程序。

现在,我想使其与分类变量一起使用,因此我尝试使用colorFactor()创建调色板。绘制了多边形,但没有颜色。

该应用程序在线here上,完整代码位于github repo上。我认为是错误的服务器代码如下:

function(input, output, session) {

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

  # Create pallete
  colorpal <- reactive({
    colorFactor("viridis", as.data.frame(ubs_malhas)[ubs_malhas$modelo == input$modelo, input$indicador])
  })

  indic.select <- isolate(switch(input$indicador,
                                 vars_setor))

  # Create lables
  labels_setor <- reactive({
    sprintf(
      "<strong>%s</strong><br/>
        CNES: %s<br/>
        Tempo médio até UBS: %g<br/>
        Distância média até UBS: %g<br/>
        Percetual AV: %s<br/>
        AC: %g",
      ubs_malhas$nomeubs[ubs_malhas$modelo == input$modelo],
      ubs_malhas$cnes[ubs_malhas$modelo == input$modelo],
      round(ubs_malhas$media_minutos_ubs[ubs_malhas$modelo == input$modelo], 1),
      round(ubs_malhas$media_minutos_ubs[ubs_malhas$modelo == input$modelo], 1),
      scales::percent(round(ubs_malhas$av_prop_ubs[ubs_malhas$modelo == input$modelo], 3)),
      round(ubs_malhas$ac_ubs[ubs_malhas$modelo == input$modelo], 2)
    ) %>% lapply(htmltools::HTML)
  })

  # plot the map
output$mapa <- renderLeaflet({

    pal <- colorpal()

    leaflet() %>% 
      addTiles() %>% 
      setView(lng = -46.64803, lat = -23.64992, zoom = 11)
  })

    # observe to add polygons and colors
    observe({
      pal <- colorpal()
      var <- input$indicador

      leafletProxy("mapa", data =  st_transform(ubs_malhas, 4326)[ubs_malhas$modelo == input$modelo, ]) %>% 
        clearShapes() %>%
        addPolygons(
          fillColor = pal(var),
          weight = 1, 
          opacity = 0.8,
          color = "black",
          fillOpacity = 0.6,
          # adicionar interação
          highlight = highlightOptions(
            weight = 3,
            color = "#666",
            fillOpacity = 0.6,
            bringToFront = FALSE),
          # adicionar pop-up
          label = labels_setor(),
          labelOptions = labelOptions(
            style = list("font-weight" = "normal", padding = "3px 8px"),
            textsize = "15px",
            direction = "auto")
          )
      })

# observe to add legend
    observe({
      pal <- colorpal()

      leafletProxy("mapa", data =  st_transform(ubs_malhas, 4326)[ubs_malhas$modelo == input$modelo, ]) %>% 
        addLegend(
          data = st_transform(ubs_malhas, 4326)[ubs_malhas$modelo == input$modelo, ],
          title = names(vars_setor[vars_setor == input$indicador]),
          pal = pal,
          values = ~input$indicador,
          opacity = 0.7,
          position = "topleft"
        )
    })
  }

runApp()之后,出现以下错误:

Listening on http://127.0.0.1:6204
Warning in pal(var) :
  Some values were outside the color scale and will be treated as NA
Warning in pal(v) :
  Some values were outside the color scale and will be treated as NA

有地图,但没有颜色。

我如何使它起作用?

谢谢。

1 个答案:

答案 0 :(得分:0)

您的函数colorpal的逻辑切片器格式错误,应为:     colorFactor("viridis", as.data.frame(ubs_malhas)[ubs_malhas$modelo == input$modelo, ], input$indicador)