Hi,
I'm looking at an issue with using reactiveValues as an R6 class property. It seems the values are shared between instances, i.e. I can instantiate two objects, but the reactiveValues class property has the same environment. Changing one instance property thus changes the other instance's property.
I found a workaround unlocking the R6 class environments, but I am not sure this is the best way to achieve proper instantiation. Is there are way to manually control the reactiveValues instantiation/environment?
I added two minimum examples:
Shared property:
library(shiny)
library(R6)
testBaseModel <- R6Class("testBaseModel",
public = list(
nonReactiveVar = FALSE,
reactiveVar = reactiveValues(bTest = FALSE)
)
)
testModel <- R6Class("testModel",
inherit = testBaseModel,
public = list(
)
)
ui <- function() {
fluidPage(
actionButton("button", "button"),
textOutput("instanceA"),
textOutput("instanceB")
)
}
server <- function(input, output, session) {
modelInstanceA <- testModel$new()
modelInstanceB <- testModel$new()
observeEvent(input$button, {
modelInstanceA$reactiveVar$bTest <- TRUE
modelInstanceA$nonReactiveVar <- TRUE
# changes both instances
output$instanceA <- renderText({
paste(modelInstanceA$reactiveVar$bTest, modelInstanceA$nonReactiveVar)
})
output$instanceB <- renderText({
paste(modelInstanceB$reactiveVar$bTest, modelInstanceB$nonReactiveVar)
})
})
}
shinyApp(ui, server)
Isolated property
library(shiny)
library(R6)
testBaseModel <- R6Class("testBaseModel",
lock=FALSE,
public = list(
nonReactiveVar = FALSE,
initialize = function() {
self[["reactiveVar"]] = reactiveValues(bTest = FALSE)
}
)
)
testModel <- R6Class("testModel",
inherit = testBaseModel,
lock=FALSE,
public = list(
)
)
ui <- function() {
fluidPage(
actionButton("button", "button"),
textOutput("instanceA"),
textOutput("instanceB")
)
}
server <- function(input, output, session) {
modelInstanceA <- testModel$new()
modelInstanceB <- testModel$new()
observeEvent(input$button, {
modelInstanceA$reactiveVar$bTest <- TRUE
modelInstanceA$nonReactiveVar <- TRUE
# changes both instances?
output$instanceA <- renderText({
paste(modelInstanceA$reactiveVar$bTest, modelInstanceA$nonReactiveVar)
})
output$instanceB <- renderText({
paste(modelInstanceB$reactiveVar$bTest, modelInstanceB$nonReactiveVar)
})
})
}
shinyApp(ui, server)
Thanks,
Christian