I am in the process of making a map of street food in Oaxaca, Mexico, although I am running into some trouble. I am trying to produce a leaflet map in Shiny and understand the UI side of shiny, although the server side not as much. I understand that I have to add either observe() or reactive() to the server side to produce the desired results, but I cannot seem to get it to work. I included addlayercontrol() to the leaflet as this effect is desired (hide and show markers based on what is selected) although I would like the selectizeImput to control the layers. Below is my code, I included a small subset of the dataset, as well as some commented out code of what I tried on the server side.
library(leaflet)
library(shiny)
library(shinydashboard)
library(dplyr)
#Data Sample
long <- c(-96.72363, -96.72880, -96.72700)
lat <- c(17.06167, 17.06200, 17.06170 )
name <- c("jim", "grant", "pablo")
food <- c("tacos", "burger", "elote")
df <- data.frame(long, lat, name, food)
#subsets
palette<- colorFactor(palette = c("blue", "green", "black"),
domain = df$food)
tacos <- dplyr::filter(df, grepl("tacos", food))
burger <- dplyr::filter(df, grepl("burger", food))
elote <- dplyr::filter(df, grepl("elote", food))
#ui
ui <-fluidPage(
titlePanel(title = "Street Food Oaxaca"),
sidebarLayout(
sidebarPanel(
selectizeInput("food","Select a Type:",
choices = unique(df$food),
selected = ,
multiple = TRUE)),
mainPanel(h3("Map"), leafletOutput("m", width = "800", height = "600"))
)
)
#server
server <- function(input, output, session){
output$m = renderLeaflet({
leaflet(data = df) %>%
setView(lng = -96.725, lat = 17.0618, zoom =14)%>%
addTiles() %>%
addCircleMarkers( layerId = tacos,
data = tacos,
~long,
~lat,
group = 'tacos',
popup = ~as.character(name),
radius = 2,
color = ~palette(food),
fillOpacity = 0.5) %>%
addCircleMarkers( layerId = burger,
data = burger,
~long,
~lat,
group = 'burger',
popup = ~as.character(name),
radius = 2,
color = ~palette(food),
fillOpacity = 0.5) %>%
addCircleMarkers( layerId = elote,
data = elote,
~long,
~lat,
group = 'elote',
popup = ~as.character(name),
radius = 2,
color = ~palette(food),
fillOpacity = 0.5) %>%
# addPolygons(data = poly,
# ~long,
# ~lat,
# weight = 3,
# color = "red",
# group = "Restricted Zone" ,
# popup = "Restricted Zone")
addLayersControl(
overlayGroups = c("tacos","burger","elote", "Restricted Zone"),
options = layersControlOptions(collapsed = TRUE)
)
})
}
# observeEvent(
# if(function(input$food, value ="tacos"))
# output$m = renderLeaflet('m')%>%
# removeMarker(layerId = m,c('burger', 'elote')))
#removeMarker(group == C('two', 'three'))
# data_f <- reactive({
# dplyr::filter(df$food == "tacos")
# })
#
# observeEvent(data_f, {
#
# leafletProxy("map") %>%
#
# clearMarkers() })
#Run the application
shinyApp(ui = ui , server = server)
Try this
library(leaflet)
library(shiny)
library(shinydashboard)
library(dplyr)
#Data Sample
long <- c(-96.72363, -96.72880, -96.72700)
lat <- c(17.06167, 17.06200, 17.06170 )
name <- c("jim", "grant", "pablo")
food <- c("tacos", "burger", "elote")
df <- data.frame(long, lat, name, food)
#subsets
palette <- colorFactor(palette = c("blue", "green", "black"),
domain = df$food)
tacos <- dplyr::filter(df, grepl("tacos", food))
burger <- dplyr::filter(df, grepl("burger", food))
elote <- dplyr::filter(df, grepl("elote", food))
ui <-fluidPage(
titlePanel(title = "Street Food Oaxaca"),
sidebarLayout(
sidebarPanel(
selectizeInput("food","Select a Type:",
choices = unique(df$food),
selected = food[1],
multiple = TRUE)),
mainPanel(h3("Map"), leafletOutput("m", width = "800", height = "600"))
)
)
#server
server <- function(input, output, session){
df1 <- eventReactive(input$food, {
df %>% dplyr::filter(food %in% input$food)
})
output$m = renderLeaflet({
leaflet(data = df1()) %>%
setView(lng = -96.725, lat = 17.0618, zoom =14)%>%
addTiles() %>%
addCircleMarkers( # layerId = input$food,
# data = input$food,
~long,
~lat,
#group = input$food,
#popup = ~as.character(name),
radius = 2,
color = ~palette(input$food),
fillOpacity = 0.5) %>%
# addCircleMarkers( layerId = burger,
# data = burger,
# ~long,
# ~lat,
# group = 'burger',
# popup = ~as.character(name),
# radius = 2,
# color = ~palette(food),
# fillOpacity = 0.5) %>%
# addCircleMarkers( layerId = elote,
# data = elote,
# ~long,
# ~lat,
# group = 'elote',
# popup = ~as.character(name),
# radius = 2,
# color = ~palette(food),
# fillOpacity = 0.5) %>%
# addPolygons(data = poly,
# ~long,
# ~lat,
# weight = 3,
# color = "red",
# group = "Restricted Zone" ,
# popup = "Restricted Zone")
addLayersControl(
overlayGroups = c("tacos","burger","elote", "Restricted Zone"),
options = layersControlOptions(collapsed = TRUE)
)
})
}
#Run the application
shinyApp(ui = ui , server = server)