Shiny apps problems in a short app for fire weather index

Hello; I'm new in shiny and R and I'm having a problem, and I'm getting the following error:

Warning: Error in <Anonymous>: error in evaluating the argument 'x' in selecting a method for function 'plot': objeto de tipo 'closure' no es subconjunto
  189: <Anonymous>
  188: <reactive:r3> [#20]
  186: .func
  183: contextFunc
  182: env$runWith
  175: ctx$run
  174: self$.updateValue
  172: r3
  170: renderPlot [#54]
  168: func
  128: drawPlot
  114: <reactive:plotObj>
   98: drawReactive
   85: renderFunc
   84: output$mapPlot
    3: runApp
    2: print.shiny.appobj
    1: <Anonymous>

My code is as follows:

# Get the region borders
municipios <- esp_get_munic_siane(region = "Andalucía", epsg = "4326") %>%
  # Homogeinizo labels
  mutate(
    Provincia = esp_dict_translate(ine.prov.name, "es")
  )

# Preparing rasters and IDC
riesgo <- raster("Data/Riesgo/down_p_fc024_RIESGO_25062024_1_geo_1719339124.tif") %>% crop(municipios) %>% mask(municipios)


idc <- st_read("Data/IDC/idc.shp") %>% st_transform(crs = 4326) 
idc <- idc %>% 
  select(values= "IDC_240625") 

extent(riesgo) <- extent(idc)

idc <- rasterize(idc, riesgo, field = "values" )

terra::writeRaster(idc, "Data/IDC.tif", filetype = "GTiff", overwrite = TRUE)

# create r3 from r1
r3 <- raster(riesgo)

# fill based on conditions

r3[idc == 1 & riesgo <= 5] <- 0
r3[idc == 2 & riesgo <= 2] <- 0
r3[idc == 2 & riesgo == 3 & riesgo ==4] <- 1
r3[idc == 2 & riesgo == 5] <- 2
r3[idc == 3 & riesgo <= 2] <- 0
r3[idc == 3 & riesgo == 3] <- 1
r3[idc == 3 & riesgo >= 4] <- 2
r3[idc == 4 & riesgo <= 3] <- 1
r3[idc == 4 & riesgo == 4] <- 2
r3[idc == 4 & riesgo == 5] <- 3
r3[idc == 5 & riesgo <= 2] <- 1
r3[idc == 5 & riesgo == 3] <- 2  
r3[idc == 5 & riesgo >= 4] <- 3 
r3[idc == 6 & riesgo <= 2] <- 2
r3[idc == 6 & riesgo == 3] <- 3
r3[idc == 6 & riesgo >= 4] <- 4
r3[idc == 7 & riesgo <= 2] <- 3
r3[idc == 7 & riesgo >= 3] <- 4
r3[idc >= 8 & riesgo >= 1] <- 4

#plot(r3)

m <-matrix(c(-Inf, 0, 0,
               0, 1, 1,
               1, 2, 2,
               2, 3, 3,
               3, 4, 4 ), ncol=3, byrow=TRUE)

r3 <-raster::reclassify(r3, m)


# Cálculos

# stack the raster layers
#s1 = stack(r3, idc, riesgo)





#f2 <- function(x){
#  x1 = x[1]
#  x2 = x[2]
#  x3 = x[3]
#  ifelse(is.na(x1) == T, NA, ifelse(x2 == 1 & x3 <= 5, 0,
#                            ifelse(x2 == 2 & x3 <= 2, 0,
#                           ifelse(x2 == 2 & x3 == 3 & x3 ==4, 1,
#                            ifelse(x2 == 2 & x3 == 5, 2,
#                            ifelse(x2 == 3 & x3 <= 2, 0,
#                            ifelse(x2 == 3 & x3 == 3, 1,
#                            ifelse(x2 == 3 & x3 >= 4, 2,
#                            ifelse(x2 == 4 & x3 <= 3, 1,
#                            ifelse(x2 == 4 & x3 == 4, 2,
#                            ifelse(x2 == 4 & x3 == 5, 3, 
#                           ifelse(x2 == 5 & x3 <= 2, 1, 
#                            ifelse(x2 == 5 & x3 == 3, 2,  
#                            ifelse(x2 == 5 & x3 >= 4, 3, 
#                           ifelse(x2 == 6 & x3 <= 2, 2,
#                            ifelse(x2 == 6 & x3 == 3, 3,
#                           ifelse(x2 == 6 & x3 >= 4, 4,
#                           ifelse(x2 == 7 & x3 <= 2, 3, 
#                            ifelse(x2 == 7 & x3 >= 3, 4,
#                            ifelse(x2 >= 8 & x3 >= 1, 4,
#                                  NA)))))))))))))))))))) 
#}


#s_new = overlay(s1, fun = f2)
#plot(s_new)

#df <- as.data.frame(s_new, xy=TRUE)

ui=shinyUI(fluidPage(pageWithSidebar(
  headerPanel("Header1"),
  sidebarPanel(
    fileInput('layer_1', 'Riesgo incendios aemet', multiple=FALSE, accept='tif'),
    fileInput('layer_2', 'IDC', multiple=FALSE, accept='tif'),
    
  ),
  mainPanel(
    plotOutput("mapPlot")
  )
)))

server = shinyServer(function(input,output){
  
  inFile_1 <- reactive({
    req(input$layer_1) 
    raster::brick(input$layer_1$datapath)  %>% crop(municipios) %>% mask(municipios)
  })
  
  inFile_2 = reactive({
    req(input$layer_2) 
    raster::brick(input$layer_2$datapath)
  })
  
  # create r3 from r1
  r3 = reactive({
    req(input$layer_2) 
    raster::brick(input$layer_2$datapath)
 
  # fill based on conditions
  
  r3[inFile_2() == 1 & inFile_1() <= 5] <- 0
  r3[inFile_2() == 2 & inFile_1() <= 2] <- 0
  r3[inFile_2() == 2 & inFile_1() == 3 & inFile_1() == 4] <- 1
  r3[inFile_2() == 2 & inFile_1() == 5] <- 2
  r3[inFile_2() == 3 & inFile_1() <= 2] <- 0
  r3[inFile_2() == 3 & inFile_1() == 3] <- 1
  r3[inFile_2() == 3 & inFile_1() >= 4] <- 2
  r3[inFile_2() == 4 & inFile_1() <= 3] <- 1
  r3[inFile_2() == 4 & inFile_1() == 4] <- 2
  r3[inFile_2() == 4 & inFile_1() == 5] <- 3
  r3[inFile_2() == 5 & inFile_1() <= 2] <- 1
  r3[inFile_2() == 5 & inFile_1() == 3] <- 2  
  r3[inFile_2() == 5 & inFile_1() >= 4] <- 3 
  r3[inFile_2() == 6 & inFile_1() <= 2] <- 2
  r3[inFile_2() == 6 & inFile_1() == 3] <- 3
  r3[inFile_2() == 6 & inFile_1() >= 4] <- 4
  r3[inFile_2() == 7 & inFile_1() <= 2] <- 3
  r3[inFile_2() == 7 & inFile_1() >= 3] <- 4
  r3[inFile_2() >= 8 & inFile_1() >= 1] <- 4
  
  #plot(r3)
  
#  m <-matrix(c(-Inf, 0, 0,
#               0, 1, 1,
#               1, 2, 2,
#               2, 3, 3,
#               3, 4, 4 ), ncol=3, byrow=TRUE)
#  
#  r3() <-raster::reclassify(r3(), m)
  
  })
  
  output$mapPlot<- renderPlot(
    {
      plot(r3())
     # plot(inShp(), add=TRUE)
    })
})

shinyApp(ui = ui, server = server)

thanks

The "Object of type closure is not subsettable" error usually means that you have an object that is usually a function or reactive expression (a closure) and you attempt to do subsetting (with square brackets []) on the function or reactive expression directly, rather than the value returned from evaluating the function or reactive expression.

In a Shiny app, this typically appears when you refer to a reactive expression (such as r3 in your case) without placing parentheses after it. So you want to do r3(), which I can see you have done in your call to plot().
This makes me wonder if r3() is not producing the correct final value.

I don't have your data or a suitable alternative to hand, so I'll suggest something I haven't tested and you can test out if it helps.

Try returning r3 at the end of making your r3 reactive. Something like...

r3 = reactive({
    req(input$layer_2) 
    raster::brick(input$layer_2$datapath)

    r3[inFile_2() == 1 & inFile_1() <= 5] <- 0
    # skipping a few lines, but keeping all your existing code...
    r3[inFile_2() >= 8 & inFile_1() >= 1] <- 4

    return(r3) ## <-- Add this line to your existing code
  })

Hi Keith, and thank you very much!!
I still don't get the application working, I replicate my code again and leave a drive link to the rasters WildfireRisk_Andalucía - Google Drive


ui = shinyUI(fluidPage(pageWithSidebar(
headerPanel("Header1"),
sidebarPanel(
fileInput('layer_1', 'Riesgo incendios aemet', multiple=FALSE, accept='tif'),
fileInput('layer_2', 'IDC', multiple=FALSE, accept='tif')

),
mainPanel(leafletOutput("mapPlot", width = "100%", height = "100%"))

)))

server = shinyServer(function(input,output){

inFile_1 <- reactive({
req(input$layer_1)
raster::brick(input$layer_1$datapath) %>% crop(municipios) %>% mask(municipios)
})

inFile_2 <- reactive({
req(input$layer_2)
raster::brick(input$layer_2$datapath) %>% crop(municipios) %>% mask(municipios) %>% extent(inFile_1())

})

create r3 from r1

r3 = reactive({
req(input$layer_2)
raster::raster(input$layer_2$datapath) %>% extent(inFile_1())

fill based on conditions

r3[inFile_2() == 1 & inFile_1() <= 5] <- 0
r3[inFile_2() == 2 & inFile_1() <= 2] <- 0
r3[inFile_2() == 2 & inFile_1() == 3 & inFile_1() == 4] <- 1
r3[inFile_2() == 2 & inFile_1() == 5] <- 2
r3[inFile_2() == 3 & inFile_1() <= 2] <- 0
r3[inFile_2() == 3 & inFile_1() == 3] <- 1
r3[inFile_2() == 3 & inFile_1() >= 4] <- 2
r3[inFile_2() == 4 & inFile_1() <= 3] <- 1
r3[inFile_2() == 4 & inFile_1() == 4] <- 2
r3[inFile_2() == 4 & inFile_1() == 5] <- 3
r3[inFile_2() == 5 & inFile_1() <= 2] <- 1
r3[inFile_2() == 5 & inFile_1() == 3] <- 2
r3[inFile_2() == 5 & inFile_1() >= 4] <- 3
r3[inFile_2() == 6 & inFile_1() <= 2] <- 2
r3[inFile_2() == 6 & inFile_1() == 3] <- 3
r3[inFile_2() == 6 & inFile_1() >= 4] <- 4
r3[inFile_2() == 7 & inFile_1() <= 2] <- 3
r3[inFile_2() == 7 & inFile_1() >= 3] <- 4
r3[inFile_2() >= 8 & inFile_1() >= 1] <- 4

return(r3)

plot(r3)

m <-matrix(c(-Inf, 0, 0,

0, 1, 1,

1, 2, 2,

2, 3, 3,

3, 4, 4 ), ncol=3, byrow=TRUE)

})

riskpal <- colorFactor(
palette = c('transparent', 'forestgreen', 'yellow', 'orange', 'red'),
domain = c(0:4),
na.color = "transparent")

output$mapPlot <- renderLeaflet({

  leaflet() %>% 
    addProviderTiles(provider = "OpenStreetMap") %>%
    setView(lng   = -4.80
            ,lat  = 37.51
            ,zoom = 8 ) %>%
    setMaxBounds(lng1 = -5.18
                 ,lat1 = 38.91
                 ,lng2 = -5.609
                 ,lat2 = 35.80 ) %>%
    addRasterImage(r3(), 
                   colors = riskpal,
                   opacity = .7) %>%
    addLegend(position = "bottomright", 
              title = "Riesgo",  
              labels="riesgo ", 
              pal = riskpal, 
              values = values(r3()),  
              opacity = .7) 
})

})

shinyApp(ui = ui, server = server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.