【问题标题】:Dynamic Color Fill For Polygon Using Leaflet In Shiny Not Working在闪亮的不工作中使用传单对多边形进行动态颜色填充
【发布时间】:2018-08-03 19:44:28
【问题描述】:

我的目标是创建一个基于下拉菜单突出显示状态的地图。我已经制作了一个地图,它可以按照预期的方式用于数据集中的一个指标,但是当我尝试在闪亮的应用程序中重新创建地图时,状态都是灰色的,而不是不同深浅的绿色。这是我的代码:

library(shiny)
library(tidyverse)
library(leaflet)
library(dplyr)
library(tigris)
library(DT)
library(rgdal)
library(RColorBrewer)


#create the UI
ui <- fluidPage(
  #create title
  titlePanel("Interactions by State"),
  sidebarLayout(
    sidebarPanel(

      selectInput("interactionMetric",
                  label = "Choose a Metric",
                  choices = c("Sentiment",
                              "Average Totals",
                              "Management Fee"),
                  selected = "Sentiment")
                ),
      mainPanel(
        leafletOutput("mymap"),
        p()
      )
  )
)


#Create the server

server <- function(input, output, session) {
  #get the data
  primaryDf <- read.csv('InteractionsFormattedFirstMonth.csv', header = TRUE, sep = ',')

  #select the input
  decision <- reactive({
      switch(input$interactionMetric,
                   "Sentiment" = primaryDf$Average.of.Average.Sentiment,
                   "Average Totals" = primaryDf$Average.of.Event.Value,
                   "Management Fee" = primaryDf$Average.of.MGT.Fee)
  })

  #create dataframe based on input
  newDf <- reactive({
    cbind(primaryDf["Row.Labels"],decision())
  })

  #create the states and join with dataframe to create spatial object
  statesUsed <- states(cb=T)
  states_merged <- reactive({
    geo_join(statesUsed, newDf(), "STUSPS", "Row.Labels", how = 'inner')
  })

  #create the function for a color pallette
  pal <- reactive({
    colorQuantile("YlGn", states_merged()$decision, n = 5)
  })

  #create the map to be rendered in the UI  
  output$mymap <- renderLeaflet({
      leaflet(data = states_merged()) %>% 
         addProviderTiles("CartoDB.Positron") %>%
         setView(-98.483330, 38.712046, zoom = 4) %>%
         addPolygons(data = states_merged(),
                      fillColor = ~pal(),
                      fillOpacity = 0.7,
                      weight = 0.2,
                      smoothFactor = 0.2
                      )
  })
}

shinyApp(ui, server)

作为参考,数据如下所示:

> head(primaryDf)
  UniqueID Row.Labels Average.of.Event.Value Average.of.Average.Sentiment Average.of.MGT.Fee Sum.of.UniqueID
1        1         AL               4.000000                     3.000000           600.0000             311
2        2         AR               1.500000                     3.000000           600.0000              83
3        3         AZ               3.600000                     3.000000           560.0000             736
4        5         CA               4.567568                     3.138108           883.7838            4346
5        6         CO               3.000000                     3.167500           450.0000             389
6        7         CT               6.333333                     3.033333           500.0000             249

此时的代码会生成一张美国地图,其中包含在数据集中的所有州都显示为灰色。作为修复,我尝试了这个:

addPolygons(data = states_merged(),
                      fillColor = ~pal(states_merged()$decision),
                      fillOpacity = 0.7,
                      weight = 0.2,
                      smoothFactor = 0.2
                      )

但是,这会导致错误“未使用的参数 (states_merged()$decision)” 感谢任何可以帮助我解决这个问题的人,这几天我一直在不停地挣扎,这让我发疯了!

【问题讨论】:

    标签: r shiny leaflet maps r-leaflet


    【解决方案1】:

    我找到了这个参考:https://github.com/rstudio/shiny/issues/858

    解决办法是 pal() 是一个函数,但是由于某种原因要传递给它的数据必须放在它旁边。这真的很奇怪,但工作代码如下所示:

      output$mymap <- renderLeaflet({
          leaflet(data = states_merged()) %>% 
             addProviderTiles("CartoDB.Positron") %>%
             setView(-98.483330, 38.712046, zoom = 4) %>%
             addPolygons(data = states_merged(),
                          #note the solution here, "pal()(decision())
                          fillColor = ~pal()(decision()),
                          fillOpacity = 0.7,
                          weight = 0.2,
                          smoothFactor = 0.2
                          )
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      相关资源
      最近更新 更多