As a follow up to Modifying a reactive value should trigger an observe chunk, I investigated the issue further and realised that the issue probably stems from my usage of data.table
s. data.table
s are modified by reference unlike data.frame
s which makes them more efficient to use but also apparently invisible to shiny
's reactivity.
In the below example, pressing the commit button triggers the observe chunk in the data.frame
case but not in the data.table
case. A workaround could be to have a value linked to the changing of data.table
which also helps trigger the reactivity.
with data.frame
shinyServer (
function(input, output, session) {
lReactiveValues = reactiveValues(a = data.frame(firstcol = runif(1)))
voidaA = observeEvent(
input$buttonCommit,
{
new = runif(1)
cat(new,' one')
lReactiveValues$a[letters[ncol(isolate(lReactiveValues$a))]] = new
}
)
voidB = observe(priority = 50,{
# input$buttonCommit
cat(ncol(lReactiveValues$a))
counter = runif(1)
cat(counter,' two');
if (counter > 0.5) {
cat('
')
cat(ncol(lReactiveValues$a),' three
')
}
}
)
}
)
with data.table
shinyServer (
function(input, output, session) {
lReactiveValues = reactiveValues(a = data.table(firstcol = runif(1)))
# lReactiveValues = reactiveValues(a = data.frame(firstcol = runif(1)))
voidaA = observeEvent(
input$buttonCommit,
{
new = runif(1)
cat(new,' one')
setnames(
lReactiveValues$a[, newcol := new],
'newcol',
letters[ncol(isolate(lReactiveValues$a))]
)
cat(ncol(lReactiveValues$a))
}
)
voidB = observe(priority = 50,{
# input$buttonCommit
cat(ncol(lReactiveValues$a))
counter = runif(1)
cat(counter,' two');
if (counter > 0.5) {
cat('
')
cat(ncol(lReactiveValues$a),' three
')
}
}
)
}
)
ui.r
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%;'
)
)
Short summary of what the code does - Pressing button should print some text to the console which includes the string 'one'. The button should further trigger the observe chunk prompting the printing of some text containing the string 'two'. Depending on the if condition in the observe chunk, another set of text including 'three' might get printed. In the data.frame's server.r case this behaviour persists all the time the app runs. In the data.table's server.r case, this behaviour occurs for a few clicks of the button after which only the 'one' string is printed and the 'two' and 'three' aren't. This flip in behaviour, think, occurs after the if condition is found to be false for the first time.
See Question&Answers more detail:
os