Problem with plotting rasters from a stack


#1

Hello, I am having a problem plotting rasters in a shiny app and hoping that someone could help me to solve the problem. I want to add worldclim (a database) environmental data to a leaflet map. This is how it unfortunately does not work:

library(shiny)
library(maptools)
library(mapdata)
library(spocc)
library(leaflet)
library(raster)

setwd("~/wc10")
rlist=list.files(pattern="bil", full.names=T)
rasters=stack(rlist)

ui<-fluidPage(

selectInput(inputId="parameter", label="Please choose Variable",
            choices=c("BIO1 - Annual Mean Temperature" = "rasters$bio1",
                      "BIO2 - Mean Diurnal Range (Mean of monthly (max temp - min temp))"= "rasters$bio2",
                      "BIO3 - Isothermality (BIO2/BIO7) (* 100)"= "rasters$bio3",
                      "BIO4 - Temperature Seasonality (standard deviation *100)"= "rasters$bio4",
                      "BIO5 - Max Temperature of Warmest Month"= "rasters$bio5",
                      "BIO6 - Min Temperature of Coldest Month"= "rasters$bio6",
                      "BIO7 - Temperature Annual Range (BIO5-BIO6)"= "rasters$bio7",
                      "BIO8 - Mean Temperature of Wettest Quarter"= "rasters$bio8",
                      "BIO9 - Mean Temperature of Driest Quarter"= "rasters$bio9",
                      "BIO10 - Mean Temperature of Warmest Quarter"= "rasters$bio10",
                      "BIO11 - Mean Temperature of Coldest Quarter"= "rasters$bio11",
                      "BIO12 - Annual Precipitation"= "rasters$bio12",
                      "BIO13 - Precipitation of Wettest Month"= "rasters$bio13",
                      "BIO14 - Precipitation of Driest Month"= "rasters$bio14",
                      "BIO15 - Precipitation Seasonality (Coefficient of Variation)"= "rasters$bio15",
                      "BIO16 - Precipitation of Wettest Quarter"= "rasters$bio16",
                      "BIO17 - Precipitation of Driest Quarter"= "rasters$bio17",
                      "BIO18 - Precipitation of Warmest Quarter"= "rasters$bio18",
                      "BIO19 - Precipitation of Coldest Quarter" = "rasters$bio19")),
actionButton(inputId="retrieve", label="Retrieve environmental data"),
leafletOutput(outputId = "mymap"))

server<-function(input, output, session) {
  
  output$mymap<-renderLeaflet({
    map <- leaflet() %>% 
      addTiles()%>% 
      setView(0, 0, zoom = 1)
    map
  })
  
  observeEvent(input$retrieve, {
    leafletProxy('mymap') %>%
      addRasterImage(input$parameter)})
}
  
shinyApp(ui = ui, server = server) 

However, if i do it with actionbuttons it does work:

library(shiny)
library(maptools)
library(mapdata)
library(spocc)
library(leaflet)
library(raster)

setwd("C:/Users/Philipp/Documents/bio_10m_bil")
rlist=list.files(pattern="bil", full.names=T)
rasters=stack(rlist)
rasters

ui<-fluidPage(

  actionButton(inputId="bio1", label="Bio 1"),
  actionButton(inputId="bio2", label="Bio 2"),
  actionButton(inputId="bio3", label="Bio 3"),

  
  leafletOutput(outputId = "mymap")
) 

server<-function(input, output, session) {
  
  
  
  
  output$mymap<-renderLeaflet({
    map <- leaflet() %>% 
      addTiles()%>% 
      setView(0, 0, zoom = 1)
    map
  })
  
  
  observeEvent(input$bio1,{
    leafletProxy('mymap') %>%
      addRasterImage(rasters$bio1, opacity=0.25)})
  
  observeEvent(input$bio2,{
    leafletProxy('mymap') %>%
      addRasterImage(rasters$bio2)})
  
  observeEvent(input$bio3,{
    leafletProxy('mymap') %>%
      addRasterImage(rasters$bio3)})
  
}

shinyApp(ui = ui, server = server)

Anyway, that is not what I want to do (the action buttons). I think that the problem is that I have the rasterstack at a wrong position so that the app cannot access it but I don’t know where else to put or store it.

If anyone has an idea how to solve it please let me know. If there is something wrong with this post please lt me know.

Best,
Philipp