0

I have an R Shiny dashboard with 2 tabs. Each tab displays a raster. Image query works on the first tab, but not on the second. On the second tab it shows a small empty space where the queried data should be displayed, but no values. If I remove the image query from the first tab, then it works on the second. I have ensured I have the latest updates of all the required packages. I am running the latest RStudio (1.2.5001) and R (3.6.1). Any help is appreciated.

I have created a minimal example shinyapp for which the image query fails on the second tab:

library(raster) 
library(leaflet) 
library(shiny)
library(leafem)
library(shinydashboard) 

filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
r2 <- r*2

#Set up the Shiny app
ui <-function(request) {dashboardPage(
  dashboardHeader(title="Test"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Tab1", tabName = "Tab1"),
      menuItem("Tab2", tabName = "Tab2")
    )
  ),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "Tab1",
              leafletOutput("MyMap")
      ),

      # Second tab content
      tabItem(tabName = "Tab2",
              leafletOutput("MyMap2")
      )
    )
  )
)}
server <- function(input, output, session) {

#Draw the first map  
 output$MyMap <- renderLeaflet({
    leaflet() %>%
     addRasterImage(r,layerId= "A") %>%
     addImageQuery(r, position="topleft", layerId = "A")
  })

#Draw the second map
  output$MyMap2 <- renderLeaflet({
   leaflet() %>%
     addRasterImage(r2,layerId= "B") %>%
     addImageQuery(r2, position="bottomleft", layerId = "B")
 })
}
shinyApp(ui, server)
Rainfall.NZ
  • 197
  • 1
  • 12
  • Generally, you want to call `leafletOutput` and `renderLeaflet` only once each, and modify the map by using observers and `leafletProxy`. – Jeff Oct 18 '19 at 22:44
  • I think the problem lays in calling addImageQuery with leafletProxy. There is a compatibility issue using them together that I have yet to overcome. Good luck and let us posted if you figured it out! – waskawy_wombat Apr 06 '20 at 16:17

0 Answers0