I am attempting to create a shiny dashboard with an embedded leaflet map. I want the user to be able to select a city from a dropdown where the leaflet map then automatically zooms to that city based on a set of coordinates. This is the code I have created which works to a point:
#Load the following packages
library(shiny)
library(leaflet)
library(leaflet.extras)
library(leaflet.extras2)
library(shinydashboard)
#City selection
cities<-data.frame(matrix(ncol=3,nrow=2));colnames(cities)<-c("Name","Latitude","Longitude")
cities[,1]<-c("Sydney","Melbourne")
cities[,2]<-c(-33.846697,-37.8157)
cities[,3]<-c(151.192083,144.95920)
citychoices<-cities[,1]
#Initial web page layout
ui <- fluidPage(
dashboardPage(
dashboardHeader(title="TEST cities"),
dashboardSidebar(
sidebarMenu(
menuItem(
"Maps",
tabName = "maps",
icon=icon("globe")
)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "maps",
tags$style(type="text/css","#testmap {height:calc(100vh - 80px) !important;}"),
fluidRow(column(4),
column(8,
selectInput(inputId = "citieselect",label="Select a city:",choices=citychoices, selected = "Sydney")
)),
leafletOutput("testmap")
)
)
)
)
)
#Create the server functions
server <- function(input, output, session) {
TESTcities=reactive({
filteredData=subset(cities,Name == input$citieselect)
return(filteredData)
})
output$testmap=renderLeaflet({
data=TESTcities()
leaflet(data=data) %>% setView(data$Longitude, data$Latitude, zoom = 8) %>%
addWMSTiles(baseUrl = "https://maps.dwd.de/geoserver/dwd/wms",
layers = c("Natural_Earth_Map"),
group = "Natural_Earth_Map",
options = leaflet::WMSTileOptions(
format = "image/png",
info_format = "text/html"
))
# addWMS(baseUrl = "https://maps.dwd.de/geoserver/dwd/wms", #!!DOES NOT WORK!!
# layers = c("Natural_Earth_Map"),
# group = "Natural_Earth_Map",
# options = leaflet::WMSTileOptions(
# format = "image/png",
# info_format = "text/html"
# ))
})
}
runApp(list(ui = ui, server = server))
However, problems arise when I change the wms service from addWMSTile to addWMS (which is based from leaflet.extras2 package). This is preferred because I want the user to be able to query the map via the identify function using mouse click - which this function allows. Thus, when another city is selected from the dropdown the leaflet map greys out and becomes unresponsive when addWMS is used instead of addWMSTile.
Can anyone help to fix this issue?