Im trying to use shinydashboard
with leatlef
. Having read this post on SO I have tried several things without success to show corresponding information from df
by mouse click event on a marker in a leaflet map.
Example data:
latitude<-c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
longitude<-c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262,-78.67600)
amounts1<-c(27, 44, 34, 46, 25, 15)
amounts2<-c(34, 52, 35, 78, 14, 24)
ids<-c("a", "b", "c", "d", "e", "f")
df<-data.frame(ids,amounts1,amounts2,latitude,longitude)
My code looks like:
library(shiny)
library(shinydashboard)
library(leaflet)
ui = dashboardPage(
dashboardHeader(title = "Testing leatlef"),
dashboardSidebar(
sidebarMenu(menuItem(
"Dashboard",
tabName = "dashboard",
icon = icon("dashboard")
))
),
dashboardBody(
tags$script(HTML("addClass(‘sidebar-mini’);")),
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "style.css")
),
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
fluidRow(column(width = 12,
leafletOutput('map', height = 550))),
fluidRow(verbatimTextOutput("Click_text"))
)
)
################################################################################
# Server
################################################################################
server = function(input, output, session) {
map = createLeafletMap(session, 'map')
session$onFlushed(once = T, function() {
output$map <- renderLeaflet({
leaflet(df) %>%
addMarkers( ~ longitude,
~ latitude)
})
})
observe({
click <- input$map_marker_click
if (is.null(click))
return()
text <-
paste("Lattitude ",
click$latitude,
"Longtitude ",
click$longtitude)
map$clearPopups()
map$showPopup(click$latitude, click$longtitude, text)
})
}
################################################################################
# run app
shinyApp(ui, server)