我希望能够动态选择Region,然后将属于所选区域的国家/地区作为子集,然后执行颜色映射。
我的代码独立运行选择,即国家/地区不依赖于区域。当我进行颜色映射时,国家名称会失真(圣路易斯被标记为非洲的城市)
library(shiny)
library(shinydashboard)
library(rgdal)
library(raster)
library(sp)
library(leaflet)
library(DT)
library(RColorBrewer)
ui<-fluidPage(
geo<-readOGR(dsn = path.expand("./www/map"),layer = "geo")
column(6,
leafletOutput("leaf"),
absolutePanel(fixed = TRUE, draggable = TRUE,
top = 80, left = 'auto', right = 10, bottom = "auto",
width = 220, height =
"auto",style="opacity:0.8;background:#ffffff;",
selectInput(inputId = "region",
label = "REGION",
choices = c(
"All",
unique(as.character(geo$Region))
)
),
selectInput(inputId = "country",
label = "COUNTRY",
choices = c(
"All",
unique(as.character(geo$Country))
)
)
),
)
server<-function(input,output){
#reactive function for region
fregion<-reactive({
data<-geo
if(input$region!="All"){
data<-subset(data,Region%in%input$region)
}
return(
data
)
})
#reactive function for country
fcountry<-reactive({
data<-geo
if(input$country!="All"){
data<-subset(data,Country%in% input$country)
}
return(
data
)
})
#base map for color mapping
output$leaf<-renderLeaflet({
leaflet(geo)%>%
#addTiles()%>%
addProviderTiles("Esri.NatGeoWorldMap")%>%
addAwesomeMarkers(
data=geo,
label = geo$City
)
}
)
#observe map function for region
observe({
data<-geo
if(input$region!="All"){
data<-subset(data,Region%in%input$region)
}
return(
data
)
#color mapping function for Region
pal<-colorFactor(rainbow(7),geo$Region)
leafletProxy("leaf",data=geo) %>%
clearMarkers() %>%
clearControls() %>%
addProviderTiles("Esri.NatGeoWorldMap")%>%
addCircleMarkers(
color=~pal(Region),
# fillColor=color,
label =geo$City,
)%>%
addLegend(title = "Region", position = "topleft",
pal = pal, values = ~Region, opacity = 1)
})
#observe map function for country
observe({
#color mapping function for country
pal<-colorFactor(rainbow(7),geo$Country)
leafletProxy("leaf",data=fcountry()) %>%
clearMarkers() %>%
clearControls() %>%
addProviderTiles("Esri.NatGeoWorldMap")%>%
addCircleMarkers(
color=~pal(Country),
# fillColor=color,
label =geo$City,
)%>%
addLegend(title = "Country", position = "topleft",
pal = pal, values = ~Country, opacity = 1)
})
}
对于selectInput,我希望国家/地区选项依赖于地区(如果选择亚洲,则仅希望亚洲国家/地区作为我的国家/地区选项)。 对于颜色映射,我希望能够准确地对我的选择进行映射。