addImageQuery() result stops working in shiny app

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

addImageQuery() result stops working in shiny app

Warner, David
Hi all

I've put together a shiny app whose ultimate use would be to share
MODIS-Aqua based estimates of chlorophyll-a concentration in RasterLayer

I would like the user to be able to select from a list of layers that are
in a stack and once that layer is displayed I'd like them to be able to see
the RasterLayer value wherever the cursor is located.

I have no problems make the shiny app do this consistently for a single
RasterLayer.  However, once the user switches to a RasterLayer other than
the default that is shown when you open the app, the value at the cursor is
no longer displayed.

Does anyone know if there is a way to change this behavior by changing my
code?  I am guessing that because of my location on the learning curve that
my code is the issue.  Somehow I think I have failed to correctly relay the
change to addImageQuery().

The code below should be a reproducible example.  It may not be an ideal
example, but it is a self-contained example that will only require that the
user has the required packages.  And it is the best I know how to do.

Thanks in advance to anyone who reads this!

#The example....
# 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 user can choose from in the shiny app
myras <- list(r1, r2, r3, r4, r5)
modis.rasters <- stack(myras)

# 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
pal1 <- colorNumeric(palette = c("#5E4FA2", "#3288BD", "#66C2A5",
                                 "#E6F598", "#FEE08B", "#FDAE61",
"#F46D43", "#D53E4F", "#9E0142" ), domain =
                       x,na.color = "transparent")

# Create a map for use in shiny app
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)',
  addMouseCoordinates(style = "basic")

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


  # Generate a row with a sidebar

    # Define the sidebar with one input
    # Here "period" is a weekly time period/raster
      selectInput("period", "Choose a time period:",
      helpText("Some raster data that I will replace.",
    # 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,
      addImageQuery(reactiveRaster(), type="mousemove", digits=2,
                    position="topright", layerId=input$period)

  reactiveRaster <- reactive({modis.rasters[[input$period]]})
  # add the selected raster to the map
    leafletProxy("raster_map") %>%
      clearImages() %>%
      addRasterImage(reactiveRaster(), colors=pal1, layerId =input$period,
shinyApp(ui = ui, server = server)

David Warner
Research Fisheries Biologist
U.S.G.S. Great Lakes Science Center
1451 Green Road
Ann Arbor, MI 48105

        [[alternative HTML version deleted]]

R-sig-Geo mailing list
[hidden email]