This is my first question so I apologise if its not up to scratch, but I'm stuck on an issue and I really need help please.
I'm trying to create a shiny app, which will allow you to select a species from a drop down menu, thereby changing the colour of country polygons on a map, 1 colour for presence and another for absence. I've created a sf object with the shapefile data and merged it with a presence absence (1 +0 respectively) dataframe, the intention being that selection of this species will change input$SppSelect, selecting a different column in the merged sf object, and then this will cause my leaflet map to be redrawn with a new species occurrence.
To colour the map I intended to assign my species input variable to another variable: sppcol <- reactive({input$SppSel})
, and then use Botpal <- reactive({colorFactor(viridis(2), BotCon$sppcol())})
to make a reactive palette. I'd then use fillColor = ~Botpal(Botcon$sspcol())
to change the colour of the polygons.
I'm not sure if I can produce a reprex but I'll attempt to illustrate how the app should work. Palms = csv file with every species occurrence next to the country its in:
( china : caryota mitis)
(china : caryota no)
(Bhutan : caryota mitis).
BotCon is the botanical countries shape file I'm working with. :
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)
My question is therefore; how can I use my species selection input, to select a different column of the merged dataset I've created, and colour my map polygons using the 1s and 0s present inside this column please?
(really sorry for the layout, I'm pretty much self taught for all this stuff too)
```lang-r
(on a line by itself), then uninterrupted code, then```
(on a line by itself). (Ref: stackoverflow.com/editing-help) – r2evans