简体   繁体   English

Shiny/Leaflet:colors 由用户输入的 choropleth map

[英]Shiny/Leaflet: colors for choropleth map by user input

I'm trying to build an app which plot choropleth leaflet map based on variable inputed by user.我正在尝试构建一个应用程序,其中 plot choropleth leaflet map 基于用户输入的变量。

For now I want to make it work with categorical variables, so I have tried using colorFactor() to create the palette.现在我想让它与分类变量一起使用,所以我尝试使用colorFactor()来创建调色板。 The polygons are plotted, but without the colors.绘制了多边形,但没有 colors。

The app is online here and the full code is on a github repo .该应用程序在此处在线,完整代码位于github 存储库中。 The server code, where i believe is the error, is below:我认为是错误的服务器代码如下:

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"
        )
    })
  }

After runApp() i got the following error: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

The map is there, but the colors are not. map 在那里,但 colors 不在。

How could I make it works?我怎样才能让它发挥作用?

Thanks in advance.提前致谢。

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

I faced the exact issue.我遇到了确切的问题。 I spent an entire night debugging this but the problem lied in the variable which I passed to pal function. I misspelled a word "North-West" as "North-west".我花了整整一个晚上调试这个,但问题出在我传递给朋友function的变量上。我把“North-West”这个词拼错为“North-west”。 So I'd suggest you to go through the unique values which you are passing to pal and check whether it is aligning with variables used while initializing pal.因此,我建议您通过传递给 pal 的唯一值到 go,并检查它是否与初始化 pal 时使用的变量对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM