在reactive()选择更改后更新addImageQuery()值

问题描述 投票:0回答:1

在包含带有光栅的传单地图的闪亮应用程序中,可以获取鼠标位置处的像素值。

在同一个闪亮的应用程序中,可以从光栅列表中进行选择来决定要显示的内容。

我不知道该怎么做是让闪亮的应用程序在更改为新的栅格图层后继续为用户提供鼠标位置处的栅格值。下面的代码是一个示例。

当闪亮的应用程序打开并且窗口最大化时,将显示鼠标光标处的光栅值。 一旦切换到新的栅格,就不再显示。 我对如何更改此设置感到困惑,以便显示新选择的栅格的鼠标处的栅格值。 我在之前的文章中最接近这个问题的是鼠标悬停时的光栅值

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) 

# 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)
r shiny r-raster r-leaflet
1个回答
0
投票

我不确定我是如何想到这一点的。 也许是直觉? 实际上尝试不同的事情。 反复。 无论如何,问题的解决方法是我需要稍后构建的主地图(“地图”)以具有 RasterLayer 和 addImageQuery()。 我添加了这些东西,现在可以使用了。 精炼代码如下。 添加第 40-42 行解决了问题。

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) %>%
  addRasterImage(modis.rasters[[1]], colors=pal1, layerId ="values",
                 opacity=0.5) %>%
  addImageQuery(modis.rasters[[1]], type="mousemove", digits=2, position="topright", layerId="values") %>%
  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) 
© www.soinside.com 2019 - 2024. All rights reserved.