[R-sig-Geo] addImageQuery() result stops working in shiny app

Warner, David dmw@rner @ending from u@g@@gov
Fri Oct 12 03:49:09 CEST 2018


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
form.

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....
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 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
2016-04-29")
pal1 <- colorNumeric(palette = c("#5E4FA2", "#3288BD", "#66C2A5",
"#ABDDA4",
                                 "#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)',
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)



-- 
David Warner
Research Fisheries Biologist
U.S.G.S. Great Lakes Science Center
1451 Green Road
Ann Arbor, MI 48105
734-214-9392
orcid.org/0000-0003-4939-5368

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list