I'm modifying a reactive value inside an observeEvent
. I have another observe
chunk which calls this reactive value. I expect the observe
chunk to be triggered at the end of the observeEvent
chunk given this reactive value dependency but that seems to not be happening. Is that expected behaviour?
http://shiny.rstudio.com/reference/shiny/latest/reactiveValues.html says - "When you read a value from it, the calling reactive expression takes a reactive dependency on that value, and when you write to it, it notifies any reactive functions that depend on that value."
In the below example, everytime I click the button, I expect cat
with 2 to be output, and depending on the value of counter
, the cat
with the 3 to be output too. That isn't the case, and the output typically prints the cat
with 1 but not the cat
s with 2 and 3.
shinyServer (
function(input, output, session) {
lReactiveValues = reactiveValues(a = 1.1)
voidaA = observeEvent(
input$buttonCommit,
{
a = runif(1)
cat(a,' 1\t')
lReactiveValues$a = a
}
)
voidB = observe({
counter = runif(1)
cat(counter,' 2\t');
if (counter > 0.5) {
cat('\n')
cat(lReactiveValues$a,' 3\n')
}
}
)
}
)
dashboardPage(
dashboardHeader(
title = "Analytics"
),
## Sidebar content
dashboardSidebar(
menuItem("Analysis", tabName = "tabAnalysis", icon = icon("calculator"))
),
## Body content
dashboardBody(
tabItems(
tabItem(
tabName = "tabAnalysis",
actionButton("buttonCommit", "Commit!")
)
)
#, style='width: 100%; height: 100%;'
)
)
I'm not 100% sure I understand exactly what you want, but perhaps what you want is to just add lReactiveValues$a
as the first line inside the voidB
observer. See if that works like you expect.
If that was your problem, I think I understand the source of the problem. The way observers work is that they run first and record all the reactive dependencies they can find, and then whenever one of those dependencies change, the observer runs again. Since you have a randomizer there, it means that half the times the app runs, the line of code using lReactiveValues$a
never gets run, so that observer doesn't know that lReactiveValues$a
is a dependency. Which is why you'll notice inconsistent behaviour - sometimes voidB will run a few times but will eventually stop running when the random number is < 0.5.
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.