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