Dear all,
I am trying to embed a leaflet object in a renderUI (which perhaps is not so sensible but I haven't come up with other options yet) and it seems when you run the code for the first time, you can click on the leaflet map. However, after you refresh your input (e.g. choosing "Option 2"), the map is refreshed and not clickable anymore. Any suggestion here?
Thank you very much in advance!
Cipolla
I have tried to generate a minimal working example here:
library(shiny)
library(shinydashboard)
library(leaflet)
ui <- dashboardPage(
title = "Minimal Working Example",
dashboardHeader(title = "Example"),
dashboardSidebar(disable = FALSE,
sidebarMenu(id = "sidebarmenu",
menuItem("Tab 1",
tabName = "tab1",
icon = icon("gear")),
menuItem("Tab 2", tabName = "tab2", icon = icon("list-ol")),
conditionalPanel("input.sidebarmenu == 'tab1'",
selectInput(
inputId = "option",
label = "Options",
choices = c("Option 1", "Option 2"),
),
)
)
),
dashboardBody(
fluidRow(
uiOutput("test")
)
)
)
server <- function(input, output){
output$test <- renderUI({
tagList(
if (input$option == "Option 1"){
print(input$option)
leafletOutput("map")
},
if (input$option == "Option 2"){
print(input$option)
leafletOutput("map")
}
)
})
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Esri.WorldTopoMap) %>%
setView(lng = -174.349645, lat = 40.529522, zoom = 5) %>%
addMarkers(lng=c(-174.349645), lat=c(40.529522))
})
observe({
click = input$map_click
print(click)
if(is.null(click))
return()
text<-paste("Latitude: ", click$lat, ", Longtitude: ", click$lng)
distPlot_proxy = leafletProxy("map") %>%
clearMarkers() %>%
addMarkers(click$lng, click$lat) %>%
clearPopups() %>%
addPopups(click$lng, click$lat, text)
})
}
shinyApp(ui = ui, server = server)