library(shiny)
stuttgart <- data.frame(lat = c(49.340052, 49.400860, 48.029136), lng = c(8.011806, 10.923183, 9.195583))
ui <- fluidPage(
leafletOutput("mymap"),
p(),
checkboxInput("show", "Show Stuttgart Triangle", TRUE))
server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
cbind(rnorm(20) + 9.2, rnorm(20) + 48.79)
}, ignoreNULL = FALSE)
### Base plot
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles()
addPolygons(lat = stuttgart$lat, lng = stuttgart$lng, color = "#FFBF00", layerId = "stuttgart") %>%
addCircleMarkers(data = points(), popup = "Here!")
})
### Takes away the polygon
observeEvent(input$show, {
if(input$show == FALSE) {
leafletProxy("mymap", session) %>%
removeShape(
layerId = "stuttgart")
}
})
### Puts the polygon back
observeEvent(input$show, {
if(input$show == TRUE) {
leafletProxy("mymap", session) %>%
addPolygons(lat = stuttgart$lat, lng = stuttgart$lng, color = "#FFBF00", layerId = "stuttgart")
%>% addCircleMarkers(data = points(), popup = "Here!")
}
})
}
shinyApp(ui, server)