根据用户选择更改地图颜色填充

时间:2019-02-11 05:56:57

标签: r shiny leaflet geospatial

已经有一段时间了,因为我遇到了这个问题。我当前的闪亮应用程序无法与用户选择进行通信。当用户进行选择时,应该显示出跨不同区域的颜色填充变化。但是,地图似乎没有显示它。我的最佳猜测是,它无法正确读取“ pal”功能,并且无法捕获用户输入的选择。

任何帮助都是很好的。

library(datasets)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(leaflet)
library(leaflet.extras)
library(Matrix)
library(readxl)
library(rgdal)
library(shiny)
library(stringr)
library(tidyverse)
library(tidyr)
library(RColorBrewer)

cf <- read.csv("datafile.csv")
sgmap55 <-readOGR("shapefile.kml")

bins <-c(1,50,100,150,200)
pal <- colorBin("Blues", domain = NULL, bins = bins, na.color = "#808080")

#5) setting for the labels. 
labels <- sprintf(
  "<strong>%s</strong><br/>%g respondents </sup>",
  cf$planarea, cf$planarea)%>% lapply(htmltools::HTML)


##Section C: ShinyApp starts here
ui <- fluidPage(
  titlePanel("Brand Interaction with Regions"),
  sidebarLayout(
    sidebarPanel(
      radioButtons("brand", "Select First Brand:", choices = colnames(cf[,c(3,4,5,6,7,8,9,10,11,12,13,14)]))),
    mainPanel(
      leafletOutput("sgmap2")
      
      
    )
  )
)

server <- function(input, output, session) {
  
  output$sgmap2 <- renderLeaflet({
    
    selected_brand <- input$brand
    
    leaflet() %>%
      addTiles() %>%
      addResetMapButton()%>%
      clearMarkers()%>%
      addProviderTiles("OpenStreetMap") %>%
      setView(103.8198,1.3521,11)%>%
      addPolygons(data = sgmap55,
                  highlight = highlightOptions(
                    weight = 5,
                    color = "#666666",
                    fillOpacity = 0.7,
                    fillColor = pal(input$selected_brand),
                    bringToFront = TRUE))
    
  })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

通过以下代码在服务器部分之间进行管理:

    req(input$brand)
    cpop <- cf[[input$brand]]