为什么此代码不适用于反应函数?
why does this code not work with reactive function?
我希望此代码能够在我移动地图时刷新要刷新的输入值。
谁能告诉我为什么这段代码不起作用?当我用 "observe" 函数替换服务器端的 "reactive" 值时,它会执行我想要的操作。
ui.R
library(shiny)
library(leaflet)
shinyUI(navbarPage(
title="HeatXPlorers", id="nav",
tabPanel(title="Interactive map",
div(class="outer",
leafletOutput(outputId = "map", width="100%", height="100%")
)
),
tabPanel(title="Producers",
fluidRow(column(2,
numericInput(inputId= "minLon_prod",
label = "Min Longitude", value=2.5)
))
)
))
server.r
library(shiny)
library(leaflet)
library(dplyr)
shinyServer(function(input, output, session) {
# Create the map
output$map <- renderLeaflet({
#input$map_bounds and input$map_zoom are created when the leaflet is created
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map- 5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>') %>%
setView(lng = 2.49, lat = 47, zoom = 6)
})
################ END OF SINGLE TIME EXECUTION
## Interactive Map - for map ###########################################
prodInBounds <- reactive({
bounds <- input$map_bounds
updateNumericInput(session, inputId="minLon_prod", value = bounds$west)
})
})
您的问题可以简化为观察者和反应性表达之间的根本区别。引用observe的官方文档吧:
An observer is like a reactive expression (...) But unlike reactive expressions, it doesn't yield a result (...) Thus, observers are only useful for their side effects (...).
Another contrast between reactive expressions and observers is their execution strategy. Reactive expressions use lazy evaluation (...) if they are not called then they will never re-execute. In contrast, observers use eager evaluation; as soon as their dependencies change, they schedule themselves to re-execute.
现在让我们强调两件事:
- 在您的代码中,您使用
updateNumericInput
作为副作用,因此不会返回任何有用的信息,因为 observe
是一个自然的选择。
prodInBounds
从未使用过,因此它从未被评估过,因此 updateNumericInput
也从未被调用过。
我希望此代码能够在我移动地图时刷新要刷新的输入值。
谁能告诉我为什么这段代码不起作用?当我用 "observe" 函数替换服务器端的 "reactive" 值时,它会执行我想要的操作。
ui.R
library(shiny)
library(leaflet)
shinyUI(navbarPage(
title="HeatXPlorers", id="nav",
tabPanel(title="Interactive map",
div(class="outer",
leafletOutput(outputId = "map", width="100%", height="100%")
)
),
tabPanel(title="Producers",
fluidRow(column(2,
numericInput(inputId= "minLon_prod",
label = "Min Longitude", value=2.5)
))
)
))
server.r
library(shiny)
library(leaflet)
library(dplyr)
shinyServer(function(input, output, session) {
# Create the map
output$map <- renderLeaflet({
#input$map_bounds and input$map_zoom are created when the leaflet is created
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map- 5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>') %>%
setView(lng = 2.49, lat = 47, zoom = 6)
})
################ END OF SINGLE TIME EXECUTION
## Interactive Map - for map ###########################################
prodInBounds <- reactive({
bounds <- input$map_bounds
updateNumericInput(session, inputId="minLon_prod", value = bounds$west)
})
})
您的问题可以简化为观察者和反应性表达之间的根本区别。引用observe的官方文档吧:
An observer is like a reactive expression (...) But unlike reactive expressions, it doesn't yield a result (...) Thus, observers are only useful for their side effects (...).
Another contrast between reactive expressions and observers is their execution strategy. Reactive expressions use lazy evaluation (...) if they are not called then they will never re-execute. In contrast, observers use eager evaluation; as soon as their dependencies change, they schedule themselves to re-execute.
现在让我们强调两件事:
- 在您的代码中,您使用
updateNumericInput
作为副作用,因此不会返回任何有用的信息,因为observe
是一个自然的选择。 prodInBounds
从未使用过,因此它从未被评估过,因此updateNumericInput
也从未被调用过。