gpt4 book ai didi

R Shiny : Can't access reactive value outside of reactive consumer

转载 作者:行者123 更新时间:2023-12-04 14:58:46 26 4
gpt4 key购买 nike

我正在尝试构建一个 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()?
我究竟做错了什么?我找到了 this RStudio 社区上的相关问题,但对我没有帮助。任何人都可以帮助我理解这一点吗?
最小工作示例

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)
}
)
})

最佳答案

问题在于,正如错误所暗示的那样,divider是 react 性的,但您是在 react 性环境之外使用它。例如,像 ind_rur 这样的变量每次都需要重新计算divider更改,但在您的代码中,它们只计算一次。
为您的 server 试试这个功能:

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)
}
)
})

关于R Shiny : Can't access reactive value outside of reactive consumer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67359505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com