addImageQuery() stops working in leafletProxy()

shiny
leaflet

#1

Hi all

I've written a shiny app whose intent is to allow users to view raster MODIS data for a chosen time period that they select from a menu created by selectInput(). I want the user to be able to see the value of the displayed raster at the mouse using addImageQuery().

This app partly works. I am able to select and view different rasters from the list, but once I select a raster other than the first one in the list that is displayed when the app opens, addImageQuery() stops working.

I really suspect it is just because I have missed a key connection in my code. Can someone help me confirm that and maybe even remedy the limitation?

A reproducible example is below.
Thanks,
Dave

 #> A code chunk   
library(raster)
library(leaflet)
library(shiny)
library(mapview)
# Create raster data 
# Each raster represents the average of multiple rasters
# during a weekly period.  
# In this example, there are five weeks represented
# create an extent object
myext <- extent(707900, 980000,540000,1100000)
mycrs <-  "+proj=aea +lat_1=42.122774 +lat_2=49.01518 +lat_0=45.568977 +lon_0=-84.455955 +x_0=1000000 +y_0=1000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"

r1 <- raster(ncol=50, nrow=75, ext=myext, crs=mycrs)
values(r1) <-rnorm(3750, 0, 2)
r2 <- raster(ncol=50, nrow=75, ext=myext, crs=mycrs)
values(r2) <-rnorm(3750, 0, 2)
r3 <- raster(ncol=50, nrow=75, ext=myext, crs=mycrs)
values(r3) <-rnorm(3750, 0, 2)
r4 <- raster(ncol=50, nrow=75, ext=myext, crs=mycrs)
values(r4) <-rnorm(3750, 0, 2)
r5 <- raster(ncol=50, nrow=75, ext=myext, crs=mycrs)
values(r5) <-rnorm(3750, 0, 2)

# create list of rasters that the use can choose from in the shiny app
myras <- list(r1, r2, r3, r4, r5) 
modis.rasters <- stack(myras) 
nmaps<-length(names(modis.rasters))

# set up color display
# #this sets up the color palette and is the reverse Spectral with 10 levels
my.max<- 10
x <- -10:my.max # this is the observed range for chlorophyll in the data
names(modis.rasters) <- c("Week of 2016-04-01", "Week of 2016-04-08","Week of 2016-04-15",
                          "Week of 2016-04-22", "Week of 2016-04-29")
pal1 <- colorNumeric(palette = c("#5E4FA2", "#3288BD", "#66C2A5", "#ABDDA4", "#E6F598", "#FEE08B", "#FDAE61", "#F46D43", "#D53E4F", "#9E0142" ), domain = x,na.color = "transparent")


map <- leaflet() %>% addTiles() %>%
  setView(lng = -86.0589, lat = 43, zoom =7) %>%
  addLegend(pal=pal1, values = values(modis.rasters), 
            title ='Random normal variate (mean=0, SD=2)', position="bottomleft",
            opacity=1)%>% 
  addMouseCoordinates(style = "basic")


# Now set up the UI 
ui <- shinyUI(fluidPage(

     titlePanel("Stuff"),

     # Generate a row with a sidebar
     sidebarLayout(      
       
       # Define the sidebar with one input
       # Here "period" is a weekly time period/raster
       sidebarPanel(
         selectInput("period", "Choose a time period:", 
                     choices=names(modis.rasters)),
         hr(),
         helpText("Some raster data that I will replace.",
                  br(),
                  width=8)
       ),
       # Create a spot for the map
       mainPanel(leafletOutput('raster_map', width=800,height=900))  
     )
)
)

# Define a server for the Shiny app
server <- shinyServer(function(input, output){ 
  
  # Fill in the spot we created for a map
  output$raster_map = renderLeaflet({ 
    map %>% 
      addRasterImage(reactiveRaster(), colors=pal1, layerId =input$period,
                     opacity=0.5)%>%
      addImageQuery(reactiveRaster(), type="mousemove", digits=2, position="topright", layerId=input$period)
})
    
  reactiveRaster <- reactive({modis.rasters[[input$period]]}) 
  # add the selected raster to the map
  observe({ 
    leafletProxy("raster_map") %>% 
      clearImages() %>% 
      addRasterImage(reactiveRaster(), colors=pal1, layerId =input$period,
                     opacity=0.5)
  })
  })


shinyApp(ui = ui, server = server)