我正在尝试构建一个Shiny应用程序,对栅格文件进行一些修改,绘制它并提供下载(修改后的)栅格文件的选项。我遇到了以下错误:
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()?
我做错了什么?我在RStudio社区找到了一个相关问题this,但它没有帮助我。请问有人可以帮我理解吗?
最小工作示例
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)
}
)
})
renderPlot()
。因此,您可以在renderPlot()
内部使用input$input1
。但是您特定的问题是,在任何响应式环境之外,在server
中有类似input$input1
的内容。 - VitaminB16ind_rur_star = st_as_stars(ind_rur)
变成ind_rur_star <- reactive({st_as_stars(ind_rur())})
等等,每当输入变化时需要重新计算的所有其他变量,也应该相似地修改。而且,每次调用ind_rur_star
时,它都应该写成ind_rur_star()
。 - VitaminB16