r-当在Shiny中选择了不同的变量时,我如何制作一个反应调色板,它可以改变地图上多边形的颜色



这是我的第一个问题,如果不符合标准,我很抱歉,但我遇到了一个问题,我真的需要帮助。

我正在尝试创建一个闪亮的应用程序,它可以让你从下拉菜单中选择一个物种,从而改变地图上国家多边形的颜色,一种颜色表示存在,另一种颜色代表不存在。我已经用shapefile数据创建了一个sf对象,并将其与存在-不存在(分别为1+0(数据帧合并,目的是选择这个物种将更改输入$SppSelect,在合并的sf对象中选择不同的列,然后这将导致我的传单图被重新绘制,并出现新的物种。

为了给地图上色,我打算将我的物种输入变量分配给另一个变量:sppcol <- reactive({input$SppSel}),然后使用Botpal <- reactive({colorFactor(viridis(2), BotCon$sppcol())})制作一个反应调色板。然后我会使用fillColor = ~Botpal(Botcon$sspcol())来更改多边形的颜色。

我不确定我是否能制作一个reprex,但我会尝试说明该应用程序应该如何工作。Palms=csv文件,每个物种都出现在国家旁边,其位置:

(中国:caryota mitis(

(中国:caryota no(

(不丹:caryota mitis(。

BotCon是我正在合作的植物国家形状文件。:

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "SppSel", 
label = "Species Selection",
choices = paste(Palms$SpecName)),
),
mainPanel(
leafletOutput("mymap", height=600)
)))

server <- function(input, output) {
PresAb <- create.matrix(Palms, tax.name = "SpecName", locality = "Area_code_L3")
PresAb.df <- as.data.frame(t(PresAb))
PresAb.dfnamed <- cbind(LEVEL3_COD = rownames(PresAb.df), PresAb.df)
jointdataset <- merge(BotCon, PresAb.dfnamed, by = 'LEVEL3_COD', all.y=TRUE)
sppcol <- reactive({input$SppSel})
Botpal <- reactive({colorFactor(viridis(2), jointdataset$sppcol())})

output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data=jointdataset, 
stroke = TRUE,smoothFactor = 0.3, weight = 1, fillOpacity = 0.5,  
fillColor = ~Botpal(jointdataset$"caryota mitis")
}) }
shinyApp(ui = ui, server = server)

因此,我的问题是;我如何使用我的物种选择输入,选择我创建的合并数据集的不同列,并使用该列中的1和0为我的地图多边形上色?

(真的很抱歉布局,我对所有这些东西也很自学(

当然。我们可以在创建调色板时这样做,即代码的这一部分:
Botpal <- reactive({colorFactor(viridis(2), jointdataset$sppcol())})

我没有你的数据或地图文件,所以下面是一个通用的最小示例:


library(leaflet)
library(maps)
library(shiny)

ui <- fluidPage(
leafletOutput("map_1"),
selectInput(inputId = "input_species", label = "Species Selection", choices = c("Species 1", "Species 2", "Species 3"))
)

server <- function(input, output, session) {
#Load a map of the US from the 'map' package (runs once when apps starts)
shp_map = map("state", fill = TRUE, plot = FALSE)
#Make up a dataframe with some data for three species for each state (runs once when apps starts)
df_data <- expand.grid(state = unique(shp_map$names), species = c("Species 1", "Species 2", "Species 3"))
df_data$value <- sample(1:1000, nrow(df_data))
#Create map
output$map_1 <- renderLeaflet({
#Filter dataframe based on what species is selected
df_map <- df_data[df_data$species == input$input_species ,]
#Set color based on what species is selected
if(input$input_species == "Species 1") {color_selected = "Blues"}
if(input$input_species == "Species 2") {color_selected = "Reds"}
if(input$input_species == "Species 3") {color_selected = "Greens"}
#Create a palette function, using the selected color
palette <- colorNumeric(palette = color_selected, domain = df_map$value)
#Use the palette function created above to add the appropriate RGB value to our dataframe
df_map$color <- palette(df_map$value)
#Create map
map_1 <- leaflet(data = shp_map) %>% 
addPolygons(fillColor = df_map$color, fillOpacity = 1, weight = 1, color = "#000000", popup = paste(sep = "", "<b>", paste(shp_map$names), " ", "</b><br>", df_map$value)) 
map_1
})
}
shinyApp(ui, server)

最新更新