trafficonese / leaflet.extras2

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras2/
GNU General Public License v3.0
85 stars 19 forks source link

Leaflet.extras2 incompatible with shiny dashboard ? #17

Closed floveil closed 3 years ago

floveil commented 3 years ago

Hi,

I develop a web interface via R with shiny, leaflet, mapedit, and mapview (these are the main packages used). To add a popup with WMS data in my map, I use the leaflet.extras2 package. In my shiny application, I also added a dashboard. But, when I use addWMS() from the leaflet.extras2 package with the dashboard, my page does not work. (Code n°1) To test, I added my WMS feed with the addWMStiles function (from the leaflet package and without popup), and my page works perfectly. (Code 2) The results are at the end of the post.

When the Leaflet.extras2 package is loaded, the shiny dashboard does not work? Does anyone have a solution? Thank you very much in advance.

Code n°1 : Using addWMS() with dashboard (when leaflet.extra is charged) :

library(shiny)
library(shinydashboard)
library(sf)
library(mapview)
library(mapedit)
library(leaflet.extras)
library(plainview)
library(leafsync)
library(shinyWidgets)
require(tmaptools)
require(leaflet.extras2)

mapviewOptions(basemaps = c("OpenStreetMap.Mapnik","GeoportailFrance.orthos"),
               viewer.suppress = TRUE, 
               homebutton.pos="bottomleft",
               layers.control.pos="bottomleft")

m = mapview()
m@map = m@map %>% 
  setView(lat=46.28336925761807 ,lng = 2.875103294898822,zoom = 5)%>% 
  addWMS(group = "RPG2019",
         baseUrl = "https://geobretagne.fr/geoserver/draaf/wfs",
         layers = c("RPG_PARCELLES_R53_2018"),
         options = leaflet::WMSTileOptions(
           transparent = TRUE,
           format = "image/png",
           info_format = "text/html",
           tiled = FALSE))%>% 
  addWMS(group = "Hydro",
         baseUrl = "https://geobretagne.fr/geoserver/ddtm29/wfs",
         layers = c("L_INVENTAIRE_CE_DDTM_029"),
         options = leaflet::WMSTileOptions(
           transparent = TRUE,
           format = "image/png",
           info_format = "text/html",
           tiled = FALSE))%>%
  addLayersControl(overlayGroups = c("RPG2019","Hydro"),
                   options = layersControlOptions(collapsed = FALSE,),
                   position = "bottomleft")%>%
  hideGroup(c("RPG2019","Hydro"))%>%
  addSearchOSM(options = searchOptions(collapsed = FALSE,position = "topright"))%>%
  addMeasure(position = "bottomright", primaryLengthUnit = "meters", primaryAreaUnit = "sqmeters",localization = "fr")

# ui ---------------------------------------------------------------------------

ui <- dashboardPage(

  # title ----
  dashboardHeader(title = "Title"),

  # sidebar ----
  dashboardSidebar(
    sidebarMenu(id = "sidebarid",
                menuItem("Paramétrage", tabName = "page1"),
                menuItem("Edition", tabName = "page2"),
                menuItem("Visualisation", tabName = "page3"))
  )
  ,
  # body ----
  dashboardBody(
    tabItems(
      # page 1 ----
      tabItem(tabName = "page1"),
      #page 2 ---
      tabItem(tabName = "page2",
              fluidRow(column(12,editModUI("mapin",height=600)))),
      #page 3 ---
      tabItem(tabName = "page3",
              sliderInput("tampon",  "Largeur de la bande enherbée (si existante)", value =5, min=1, max = 30),
              fluidRow(column(12,leafletOutput("mapout",height=600))))
    )
  )
)

# server -----------------------------------------------------------------------

server <- function(input, output, session) {
  #page 1
  BE <- callModule(editMod, "mapin",m@map)

  #page 2
  output$mapout <- renderLeaflet({
    req(BE()$finished)
    x<-mapview(st_buffer(BE()$finished, input$tampon/2), layer.name="BE")
    x@map = x@map %>% 
      addWMS(group = "RPG2019",
             baseUrl = "https://geobretagne.fr/geoserver/draaf/wfs",
             layers = c("RPG_PARCELLES_R53_2018"),
             options = leaflet::WMSTileOptions(
               transparent = TRUE,
               format = "image/png",
               info_format = "text/html",
               tiled = FALSE))%>% 
      addWMS(group = "Hydro",
             baseUrl = "https://geobretagne.fr/geoserver/ddtm29/wfs",
             layers = c("L_INVENTAIRE_CE_DDTM_029"),
             options = leaflet::WMSTileOptions(
               transparent = TRUE,
               format = "image/png",
               info_format = "text/html",
               tiled = FALSE))%>%
      addLayersControl(overlayGroups = c("RPG2019","Hydro"),
                       options = layersControlOptions(collapsed = FALSE,),
                       position = "bottomleft")%>%
      hideGroup(c("RPG2019","Hydro"))%>%
      addSearchOSM(options = searchOptions(collapsed = FALSE,position = "topright"))%>%
      addMeasure(position = "bottomright", primaryLengthUnit = "meters", primaryAreaUnit = "sqmeters",localization = "fr")

  })
}

# shiny app --------------------------------------------------------------------

shinyApp(ui, server)

Code n°2 : Using addWMSTile() with dashboard (when leaflet.extra is not charged) :

library(shiny)
library(shinydashboard)
library(sf)
library(mapview)
library(mapedit)
library(leaflet.extras)
library(plainview)
library(leafsync)
library(shinyWidgets)
require(tmaptools)

mapviewOptions(basemaps = c("OpenStreetMap.Mapnik","GeoportailFrance.orthos"),
               viewer.suppress = TRUE, 
               homebutton.pos="bottomleft",
               layers.control.pos="bottomleft")

#pmpview page 1 
m = mapview()
m@map = m@map %>% 
  setView(lat=46.28336925761807 ,lng = 2.875103294898822,zoom = 5)%>% 
  addWMSTiles(group = "RPG2019",
              baseUrl = "https://geobretagne.fr/geoserver/draaf/wfs",
              layers = "RPG_PARCELLES_R53_2018",
              options = WMSTileOptions(format = "image/png",
                                       uppercase = TRUE,
                                       transparent = TRUE,
                                       continuousWorld=TRUE,
                                       tiled = FALSE,
                                       info_format="text/html",
                                       identify = FALSE,
                                       zIndex = 3,
                                       opacity = 1))%>% 
  addWMSTiles(group = "Hydro",
              baseUrl = "https://geobretagne.fr/geoserver/ddtm29/wfs",
              layers = "L_INVENTAIRE_CE_DDTM_029",
              options = WMSTileOptions(format = "image/png",
                                       uppercase = TRUE,
                                       transparent = TRUE,
                                       continuousWorld=TRUE,
                                       tiled = FALSE,
                                       info_format="text/html",
                                       identify = FALSE,
                                       zIndex = 4,
                                       opacity = 1))%>%
  addDrawToolbar(editOptions = editToolbarOptions(edit=TRUE),
                 polygonOptions = FALSE,
                 circleOptions = FALSE,
                 rectangleOptions = FALSE,
                 markerOptions = FALSE,
                 circleMarkerOptions = FALSE)%>%
  mapview:::mapViewLayersControl(names = c("RPG2019","Hydro"))%>%
  addSearchOSM(options = searchOptions(collapsed = FALSE,position = "topright"))%>%
  addMeasure(position = "bottomright", primaryLengthUnit = "meters", primaryAreaUnit = "sqmeters",localization = "fr")

# ui ---------------------------------------------------------------------------

ui <- dashboardPage(

  # title ----
  dashboardHeader(title = "Title"),

  # sidebar ----
  dashboardSidebar(
    sidebarMenu(id = "sidebarid",
                menuItem("Paramétrage", tabName = "page1"),
                menuItem("Edition", tabName = "page2"),
                menuItem("Visualisation", tabName = "page3"))
    )
  ,
  # body ----
  dashboardBody(
    tabItems(
      # page 1 ----
      tabItem(tabName = "page1"),
      #page 2 ---
      tabItem(tabName = "page2",
              fluidRow(column(12,editModUI("mapin",height=600)))),
      #page 3 ---
      tabItem(tabName = "page3",
              sliderInput("tampon",  "Largeur de la bande enherbée (si existante)", value =5, min=1, max = 30),
              fluidRow(column(12,leafletOutput("mapout",height=600))))

    )
  )
)

# server -----------------------------------------------------------------------

server <- function(input, output, session) {
  #data<-eventReactive(input$go,{input$adresse })

  #page 1
  BE <- callModule(editMod, "mapin",m@map)

  #page 2
  output$mapout <- renderLeaflet({
    req(BE()$finished)
    x<-mapview(st_buffer(BE()$finished, input$tampon/2), layer.name="BE")
    x@map = x@map %>% addWMSTiles(group ="RPG2019" ,
                                  baseUrl = "https://geobretagne.fr/geoserver/draaf/wfs",
                                  layers = "RPG_PARCELLES_R53_2018",
                                  options = WMSTileOptions(format = "image/png",
                                                           uppercase = TRUE,
                                                           transparent = TRUE,
                                                           continuousWorld=TRUE,
                                                           tiled = FALSE,
                                                           info_format="text/html",
                                                           identify = FALSE,
                                                           zIndex = 3,
                                                           opacity = 1))%>% 
      addWMSTiles(group = "Hydro" ,
                  baseUrl = "https://geobretagne.fr/geoserver/ddtm29/wfs",
                  layers = "L_INVENTAIRE_CE_DDTM_029",
                  options = WMSTileOptions(format = "image/png",
                                           uppercase = TRUE,
                                           transparent = TRUE,
                                           continuousWorld=TRUE,
                                           tiled = FALSE,
                                           info_format="text/html",
                                           identify = FALSE,
                                           zIndex = 4,
                                           opacity = 1))%>%
      mapview:::mapViewLayersControl(names = c("RPG2019","Hydro"))%>%
      addMeasure(position = "bottomright", primaryLengthUnit = "meters", primaryAreaUnit = "sqmeters",localization = "fr")

  })
}

# shiny app --------------------------------------------------------------------

shinyApp(ui, server)

Results of Code n°1 and Code N°2

with_leafletextra2 without_leafletextra22

trafficonese commented 3 years ago

Ah, this is a nasty bug, also because there are no errors / warnings. Unfortunately I named some functions in leaflet.extras2 alsomenuItem, which apparently causes some confusion.

I will be renaming these functions in the next release. In the meantime, you can explicitly call all the menuItem ofshinydashboard like this shinydashboard::menuItem ()

floveil commented 3 years ago

It works ! Thank you very much