1

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 cats with 2 and 3.

server.r

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')

         }
      }
      )

   }
)

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%;'

   )
)
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54

1 Answers1

3

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.

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • also, you don't need to assign the output of observe or observeEvent into a variable generally – DeanAttali Sep 11 '15 at 23:07
  • Thanks, this seems to be the case. I feel this is not the right way for `observe` to behave and I'm tempted to file a bug request. In my actual app the `lReactiveValues$a` object is actually a really big data frame that I'm modifying. Do you know if there would be a performance impact if I just call it at the beginning of the function and then do something like `a = isolate(lReactiveValues$a`)` later on for the actual logic anyway? – TheComeOnMan Sep 12 '15 at 04:23
  • It's definitely not a bug, but it is indeed a bit of an unfortunate issue that happens. It only makes sense if you know how reactivity works. I believe [this article](http://shiny.rstudio.com/articles/reactivity-overview.html) explains these concepts. I did actually ask Joe (creator of shiny) about this issue and he agreed that it's a limitation, but not really a "bug". https://groups.google.com/forum/#!topic/shiny-discuss/dSZVgU960wU Also, another workaround would be to use `observeEvent(lReactiveValues$a, ...)` – DeanAttali Sep 12 '15 at 22:56
  • as for you question, I don't think there'll be any performance hit by just calling the variable as long as you're not printing or modifying it. You can test it yourslef with `microbenchmark`, or make a separate SO question about it, but I'm pretty sure it won't cause any slowness – DeanAttali Sep 12 '15 at 22:57