Rendering rasterlayer problem with leaflet

Good morning, community,
my goal is to create an online application that allows farmers to view the properties of their land.
I would like to give farmers the freedom to choose which soil ownership they want to view.
The output of the web app is as follows

Warning: Error in addRasterImage: inherits(x, "RasterLayer") is not TRUE
107:

I assure you that the files imported here are rasterlayers.
Could you help me

i attach the entire code

clay <- raster("C:/Users/Fiorentini/Desktop/Smart_Farming_App/data/argilla.tif")
sand <- raster("C:/Users/Fiorentini/Desktop/Smart_Farming_App/data/sabbia.tif")

ui<-fluidPage(
  dashboardPage(
    skin = "red",
    dashboardHeader(title = "Soil Map", titleWidth = 350),
    dashboardSidebar(width = 350,
                     sidebarMenu(
                       menuItem("Soil", tabName = "soil_maps", icon = icon("Map")))),
    dashboardBody(
      fluidRow(
        tabItems(
          tabItem(tabName = "soil_maps",
                  box(title = "Soil map visualization",
                      width = 3,
                      status ="danger",
                      solidHeader = TRUE,
                      collapsible = TRUE,
                      radioButtons("soil",
                                  label = "Select the desired soil properties",
                                  choices = list("clay" = clay,
                                                 "sand" = sand),
                                  selected = 1),
                  leafletOutput(outputId = "soil_map", width = "100%", height = 500))))))))

server<-function(input, output) {
  
  filteredData <- reactive({
    as.raster(input$soil)
    })

  output$soil_map <- renderLeaflet({
    leaflet() %>% addProviderTiles('Esri.WorldImagery') %>%
      addRasterImage(filteredData)
  })
  
}
  
shinyApp(ui = ui, server = server)

Your clay & sand objects may be rasters, but you don't seem to be using them properly.

The issue seems to be in this statement:

  filteredData <- reactive({
    as.raster(input$soil)
    })

The "soil" input item is a radiobutton, and as such it can not hold a raster; only string.

You will want to copmpare the content of the input$soil to string values "clay" and "sand" and assign either clay or sand rasters; this assignment has to happen in the server section (i.e. not in ui).

1 Like

Hello @jlacko,
Thank you so much for your explanation.
in fact with it I went to slightly modify the code with

ui<-fluidPage(
  dashboardPage(
    skin = "red",
    dashboardHeader(title = "Soil Map", titleWidth = 350),
    dashboardSidebar(width = 350,
                     sidebarMenu(
                       menuItem("Soil", tabName = "soil_maps", icon = icon("Map")))),
    dashboardBody(
      fluidRow(
        tabItems(
          tabItem(tabName = "soil_maps",
                  box(title = "Soil map visualization",
                      width = 3,
                      status ="danger",
                      solidHeader = TRUE,
                      collapsible = TRUE,
                      radioButtons("soil",
                                  label = "Select the desired soil properties",
                                  choices = list("clay" = "clay",
                                                 "sand" = "sand"),
                                  selected = 1),
                  leafletOutput(outputId = "soil_map", width = "100%", height = 500))))))))

server<-function(input, output) {
  
  clay <- raster("C:/Users/Fiorentini/Desktop/Smart_Farming_App/data/argilla.tif")
  sand <- raster("C:/Users/Fiorentini/Desktop/Smart_Farming_App/data/sabbia.tif")
  
  raster <- reactive({
    switch(input$soil,
           "clay" = clay,
           "sand" = sand)
  }) 

  output$soil_map <- renderLeaflet({
    leaflet() %>% addProviderTiles('Esri.WorldImagery') %>%
      addRasterImage(raster())
  })
  
}
  
shinyApp(ui = ui, server = server)

Also this time, the output is the same of previous

"Warning: Error in addRasterImage: inherits(x, "RasterLayer") is not TRUE"

Do you know a method or shiny widget that i could use to fix this bug?

Hi @jlacko,
I finally solved the problem.

ui<-fluidPage(
  dashboardPage(
    skin = "red",
    dashboardHeader(title = "Soil Map", titleWidth = 350),
    dashboardSidebar(width = 350,
                     sidebarMenu(
                       menuItem("Soil", tabName = "soil_maps", icon = icon("Map")))),
    dashboardBody(
      fluidRow(
        tabItems(
          tabItem(tabName = "soil_maps",
                  box(title = "Soil map visualization",
                      width = 3,
                      status ="danger",
                      solidHeader = TRUE,
                      collapsible = TRUE,
                      radioButtons("soil",
                                   label = "Select the desired soil properties",
                                   choices = list("clay" = "clay",
                                                  "sand" = "sand"),
                                   selected = 1)),
                  leafletOutput(outputId = "soil_map", width = "100%", height = 500)))))))

server<-function(input, output) {
  
  clay <- raster("C:/Users/Fiorentini/Desktop/Smart_Farming_App/data/argilla.tif")
  sand <- raster("C:/Users/Fiorentini/Desktop/Smart_Farming_App/data/sabbia.tif")
  
  observeEvent(input$soil, {
    raster <- switch(input$soil,
                     "clay" = clay,
                     "sand" = sand)
    output$soil_map <- renderLeaflet({
      leaflet() %>% addProviderTiles('Esri.WorldImagery') %>%
        addRasterImage(raster)
    }) 
  })

}

shinyApp(ui = ui, server = server)

Thanks a lot, beacuse after your reply i started to think different ways to solve the bug.

1 Like

That is nice. And I think @jlacko will appreciate that you give this feedback.
Can you also indicate this problem as solved: you will see a button for that somewhere.

2 Likes

I am glad I was able to nudge you in the right direction :slight_smile:

A personal note: you might want to consider more descriptive names for your variables; it is very easy to confuse raster variable with raster::raster() function, which I believe was the issue with your code from about three hours ago - but all is well that ends well, and you found it yourself, so thumbs up!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.