r - R 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 社区上找到了这个相关的问题,但它对我没有帮助。谁能帮我理解这一点?
最小工作示例
用户界面
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")
)
)
))
服务器.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
它是反应式的,但是您在反应式环境之外使用它。例如,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)
}
)
})
推荐阅读
- python - 分类异常时出现官方 Python 文档错误?
- python - 在 Sublime Text 3 中使用 wxPython
- hyperledger-fabric - 创建超级账本结构通道时出错
- html - 放置网格项目而不使用模板区域
- java - Java-9 上 CompletableFuture 的 orTimeout 方法是否会杀死长时间运行的任务?
- vue.js - 如何将路由器参数传递给 Vue 中的 import 语句?
- c# - Log4Net 自定义 appender 初始化
- rpm - 在 RPM 中安装期间更改安装前缀
- mysql - 如何在 MySQL 中查询多表?
- python - 用于更改具有多个输出的文件中的字母的 awk 命令