I am trying to build a Shiny app that performs some modifications to a raster file, plots it and gives option to download (modified) raster files. I am getting the following error:
Listening on http://127.0.0.1:3371
Warning: Error in : Can't access reactive value 'divider' outside of reactive consumer.
ℹ Do you need to wrap inside reactive() or observer()?
53: <Anonymous>
Error : Can't access reactive value 'divider' outside of reactive consumer.
ℹ Do you need to wrap inside reactive() or observer()?
What am I doing wrong? I found this related question on RStudio community but it didn't help me. Could anyone please help me understand this?
Minimum Working Example
ui.R
shinyUI(fluidPage(
# Application title
titlePanel("Urban-Rural Raster Population"),
# Sidebar
sidebarLayout(
sidebarPanel(
sliderInput("divider",
"Divider number:",
min = 1,
max = 29,
value = 7),
downloadButton("download1", "Download Rural Raster"),
downloadButton("download2", "Download Urban Raster")
),
# plots
mainPanel(
plotOutput("plot1"),
plotOutput("plot2")
)
)
))
server.R
# Loading all external data - so that its done only once
rast_pop = read_stars("ind_ppp_2020_1km_Aggregated_UNadj.tif", proxy = F)
urca_raster = read_stars("URCA.tif", proxy = T)
names(urca_raster) = "urca"
# Finding rasters for India's boundaries
ind_shp = st_as_sf(getData('GADM', country ='IND', level = 0)) # for country
ind_st_shp = st_as_sf(getData('GADM', country ='IND', level = 1)) #for states boundary
ind_dis_shp = st_as_sf(getData('GADM', country = 'IND', level = 2)) # for district boundary
# cropping India from raster
ind_urca = st_crop(urca_raster, ind_shp)
# Define server logic
shinyServer(function(input, output) {
div_ru = input$divider
# dividing on the basis of rural and urban
ind_rur = ind_urca > div_ru
ind_urb = ind_urca <= div_ru
ind_rur_star = st_as_stars(ind_rur)
ind_urb_star = st_as_stars(ind_urb)
output$plot1 = renderPlot({
plot(ind_rur_star)
})
output$plot2 = renderPlot({
plot(ind_urb_star)
})
output$download1 = downloadHandler(
filename = function() {
paste0("rural_raster_", input$divider, ".tif")
},
content = function(file) {
write_stars(ind_rur_star, file, layer = 1)
}
)
output$download2 = downloadHandler(
filename = function() {
paste0("urban_raster_", input$divider, ".tif")
},
content = function(file) {
write_stars(ind_urb_star, file, layer = 1)
}
)
})
The problem is that, as the error suggests, divider
is reactive, but you are using it outside of the reactive environment. For example, variables like ind_rur
need to be recomputed each time divider
changes, but in your code they are computed only once.
Try this for your server
function:
shinyServer(function(input, output) {
div_ru <- reactive({
input$divider
})
# dividing on the basis of rural and urban
ind_rur <- reactive({ind_urca > div_ru()})
ind_urb <- reactive({ind_urca <= div_ru()})
ind_rur_star <- reactive({st_as_stars(ind_rur())})
ind_urb_star <- reactive({st_as_stars(ind_urb())})
output$plot1 = renderPlot({
plot(ind_rur_star())
})
output$plot2 = renderPlot({
plot(ind_urb_star())
})
output$download1 = downloadHandler(
filename = function() {
paste0("rural_raster_", div_ru(), ".tif")
},
content = function(file) {
write_stars(ind_rur_star(), file, layer = 1)
}
)
output$download2 = downloadHandler(
filename = function() {
paste0("urban_raster_", div_ru(), ".tif")
},
content = function(file) {
write_stars(ind_urb_star(), file, layer = 1)
}
)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.