Here's a small slice of a dataset I'm working on
> dput(df)
structure(list(nestbox = c("B1", "B10", "B100", "B101", "B102",
"B103", "B104", "B105", "B106", "B107", "B108", "B109", "B11",
"B110", "B111", "B112", "B113", "B114", "B115", "B116", "B117",
"B118", "B119", "B12", "B121", "B122", "B123", "B124", "B125",
"B126", "B127", "B128", "B129", "B13", "B130", "B131", "B133",
"B134", "B136", "B137", "B138", "B139", "B14", "B140", "B141",
"B142", "B142A", "B143", "B144", "B145", "B146", "B147", "B148",
"B149", "B15", "B150", "B151", "B152", "B153", "B154", "B155",
"B156", "B157", "B158", "B159", "B16", "B160", "B161", "B162",
"B163", "B164", "B165", "B166", "B168", "B169", "B170", "B170A",
"B171", "B172", "B173", "B174", "B175", "B176", "B177", "B178",
"B179", "B18", "B180", "B181", "B182", "B183", "B184", "B185",
"B186", "B187", "B188", "B189", "B190", "B191", "B192"), Longitude = c(-1.308809725,
-1.309306062, -1.308591288, -1.308725397, -1.308453941, -1.30826971,
-1.308223849, -1.308510734, -1.308774826, -1.309100942, -1.308661051,
-1.308788844, -1.309505208, -1.3091438, -1.309487017, -1.309235075,
-1.309665645, -1.308965346, -1.308648633, -1.307965425, -1.308046729,
-1.307626664, -1.307029421, -1.309264223, -1.306229451, -1.307167439,
-1.307846954, -1.308184483, -1.308361108, -1.308712982, -1.309586205,
-1.309736174, -1.309967322, -1.30988614, -1.310144073, -1.310207229,
-1.310332327, -1.310248542, -1.310671738, -1.311271535, -1.31180421,
-1.311607541, -1.309055439, -1.312474199, -1.312743518, -1.313056542,
-1.312898665, -1.312619555, -1.313038098, -1.312717702, -1.312295745,
-1.312181745, -1.311711673, -1.311154382, -1.308580716, -1.311484218,
-1.310989254, -1.311770452, -1.312408125, -1.312575426, -1.313220318,
-1.312965409, -1.312799476, -1.31262028, -1.313393272, -1.308310566,
-1.313737475, -1.314102982, -1.314181557, -1.313547026, -1.313998781,
-1.313728937, -1.313688331, -1.313176409, -1.312907533, -1.312311175,
-1.312342883, -1.31196085, -1.311368997, -1.309571159, -1.310019078,
-1.309446474, -1.310651343, -1.311742706, -1.312701894, -1.313811195,
-1.307353423, -1.313373501, -1.312720774, -1.312125596, -1.312104413,
-1.311495968, -1.311081217, -1.31002069, -1.309428424, -1.309651304,
-1.309355735, -1.308883116, -1.309512252, -1.308438024), Latitude = c(51.76481187,
51.76457202, 51.76488251, 51.76464054, 51.76438764, 51.76411637,
51.76370376, 51.76351538, 51.76330114, 51.76375109, 51.76410968,
51.76428126, 51.76481595, 51.76475988, 51.76505861, 51.76542577,
51.76569803, 51.76600861, 51.76587188, 51.76601172, 51.76638084,
51.76636938, 51.76657266, 51.76544329, 51.76676575, 51.76702303,
51.76712594, 51.76684919, 51.76667041, 51.76640274, 51.76615612,
51.76582432, 51.76587064, 51.76549253, 51.76568286, 51.76534201,
51.76473989, 51.7645326, 51.76433727, 51.76396316, 51.76419105,
51.76365969, 51.76580234, 51.76351641, 51.76340532, 51.76378478,
51.76368495, 51.76393508, 51.76404542, 51.76415144, 51.76426586,
51.76413932, 51.76455915, 51.76499646, 51.76557477, 51.76522317,
51.76537312, 51.76545862, 51.76545336, 51.76493284, 51.76445109,
51.76501604, 51.76544665, 51.76579626, 51.76592665, 51.76618459,
51.76570388, 51.76549023, 51.76508608, 51.76488457, 51.76471637,
51.76435515, 51.7641661, 51.76352474, 51.76310058, 51.76324994,
51.7630703, 51.76341873, 51.7632714, 51.76334178, 51.76342533,
51.76391649, 51.76567684, 51.7663306, 51.7661474, 51.76657646,
51.7662329, 51.76677171, 51.76681285, 51.7668813, 51.76732175,
51.7673092, 51.76790019, 51.76807379, 51.76829204, 51.76853917,
51.76891506, 51.76949672, 51.7691048, 51.76922436), Species = c("Empty",
"Blue tit", "Blue tit", "Great tit", "Empty", "Empty", "Great tit",
"Empty", "Empty", "Blue tit", "Empty", "Blue tit", "Blue tit",
"Empty", "Blue tit", "Empty", "Empty", "Empty", "Empty", "Great tit",
"Empty", "Great tit", "Empty", "Blue tit", "Blue tit", "Blue tit",
"Blue tit", "Empty", "Blue tit", "Empty", "Empty", "Empty", "Great tit",
"Empty", "Blue tit", "Blue tit", "Blue tit", "Empty", "Blue tit",
"Empty", "Blue tit", "Blue tit", "Empty", "Blue tit", "Blue tit",
"Blue tit", "Empty", "Empty", "Empty", "Empty", "Empty", "Blue tit",
"Empty", "Blue tit", "Empty", "Great tit", "Blue tit", "Empty",
"Blue tit", "Empty", "Empty", "Empty", "Blue tit", "Empty", "Empty",
"Empty", "Blue tit", "Blue tit", "Empty", "Great tit", "Blue tit",
"Blue tit", "Nuthatch", "Blue tit", "Great tit", "Empty", "Blue tit",
"Great tit", "Empty", "Blue tit", "Blue tit", "Great tit", "Blue tit",
"Blue tit", "Blue tit", "Empty", "Empty", "Blue tit", "Empty",
"Empty", "Blue tit", "Empty", "Blue tit", "Empty", "Empty", "Great tit",
"Blue tit", "Blue tit", "Blue tit", "Empty")), row.names = c(NA,
100L), class = "data.frame")
I am trying to make a shiny dashboard with the rest of the data and want a leaflet map output with an input selection box on the side, which displays species but I want them to display in different colours. I've tried making a colour palette with
df$Species<- as.factor(df$Species)
spcol<-colorFactor(palette = "viridis", df$Species)
Then adding a colour argument to addCircles or addCircleMarkers but this gives an error
Error:no applicable method for 'metaData' applied to an object of class "NULL"
So my question is how can I add different colours for different species to my map? Full code here works if I remove the colour argument in addCircles or addCircleMarkers
library(shiny)
library(leaflet)
library(shinydashboard)
library(ggplot2)
library(dplyr)
#colour palette for the map
df$Species<- as.factor(df$Species)
spcol<-colorFactor(palette = "viridis", df$Species)
ui <- (fluidPage(
title ="Map of Wytham",
sidebarLayout(
sidebarPanel(
selectInput("Species", label = "Species", choices = c(df$Species), selected = "Great tit")
),
mainPanel(
leafletOutput("NestMap", width = "700px", height = "700px"))
)
))
server <- function(input, output, session){
output$NestMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = -1.324640, lat = 51.770462, zoom = 14) %>%
addCircleMarkers(lng = df$Longitude, lat = df$Latitude, color = ~spcol(Species), radius = 8, popup = df$nestbox)
})
observeEvent(input$Species, {
if(input$Species != "")
{
leafletProxy("NestMap") %>% clearShapes()
index = which(df$Species == input$Species)
leafletProxy("NestMap")%>% addCircleMarkers(lng = df$Longitude[index], lat = df$Latitude[index],color = ~spcol(Species),
radius = 8, popup = df$nestbox[index])
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
Thanks to some help from @Vvdl in the comments I have the working code.
library(shiny)
library(leaflet)
library(shinydashboard)
library(ggplot2)
library(dplyr)
#colour palette for the map
df$Species<- as.factor(df$Species)
spcol<-colorFactor(palette = "viridis", df$Species)
ui <- (fluidPage(
title ="Map of Wytham",
sidebarLayout(
sidebarPanel(
selectInput("Species", label = "Species", choices = c(df$Species), selected = "Great tit")
),
mainPanel(
leafletOutput("NestMap", width = "700px", height = "700px"))
)
))
server <- function(input, output, session){
output$NestMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = -1.324640, lat = 51.770462, zoom = 14) %>%
addCircleMarkers(lng = df$Longitude, lat = df$Latitude, color = spcol(df$Species), radius = 8, popup = df$nestbox)
})
observeEvent(input$Species, {
if(input$Species != "")
{
leafletProxy("NestMap") %>% clearShapes() %>% clearMarkers()
index = which(df$Species == input$Species)
leafletProxy("NestMap")%>% addCircleMarkers(lng = df$Longitude[index], lat = df$Latitude[index],color = spcol(df$Species[index]),
radius = 8, popup = df$nestbox[index])
}
})
}
# Run the application
shinyApp(ui = ui, server = server)