How does reactiveValues differ from a list of reactive values? (unexpected req check)

170 views
Skip to first unread message

Rebecca Vessenes

unread,
Jul 15, 2016, 3:13:37 PM7/15/16
to Shiny - Web Framework for R

Hi-

I'm unclear on how reactiveValues work and best practices around it.  

Background: I've been using a reactiveValues variable as a list of reactiveValues, similar to r_data in the shiny app Radient (http://vnijs.github.io/radiant/).  This allows me to keep all the reactives for one part of my app together, so I can save or delete all of them without having to know which ones have been set. I have additional organizational structures embedded within that list.

Issue: Adding to subelements in a reactiveValue variable causes rerendering when req checks a different element of the variable.

This app works fine.  The checkbox hides some content and the button adds to the reactive value.
ui<- bootstrapPage(
  actionButton('button',
               'Add reactive value'),
  uiOutput('ui')
)
  
server <- shinyServer(function(input, output, session){
  r_match <<- reactiveValues(
    'text' = 1)

  output$ui<- renderUI({
    req(r_match[['text']])
    tagList(
      checkboxInput(
        'checkbox',
        'Hide text'
      ),
      conditionalPanel(
        'input.checkbox == false',
        h4('Show some text')
      )
    )
  })
    
  observeEvent(input$button, {
      r_match[['button_click']] <-'yes'
  })
})

shinyApp(ui = ui, server = server)

However, if 'text' and 'button_click' are both elements of a list, you get different results. If you hide the text by checking the box and then click the button, the box unchecks and text appears.
This app doesn't work
## Bad app
## unhides on button click

ui<- bootstrapPage(
  actionButton('button',
               'Add reactive value'),
  uiOutput('ui')
 )
  
server <- shinyServer(function(input, output, session){

  r_match <<- reactiveValues(
    'info' =list('text' = 1)
  )
  
  output$ui<- renderUI({
    req(r_match[['info']][['text']])
    tagList(
      checkboxInput(
        'checkbox',
        'Hide text'
      ),
      conditionalPanel(
        'input.checkbox == false',
        h4('Show some text')
      )
    )
  })
    
  observeEvent(
    input$button, {
      r_match[['info']][['button_click']] <-'yes'
  }) 
  
})

shinyApp(ui = ui, server = server)


However, commenting out req() leaves it working as intended.  I didn't expect req(r_match[['info']][['text']]) to react to  r_match[['info']][['button_click']] as they are different reactive elements in my mind.  What am I misunderstanding about reactiveValues?
 
I really want to keep a reactiveValues variable with hierarchical structure.  Is this a bad use of reactiveValues?  Am I likely to slowdown my app/cause unnecessary/unexpected reactive behavior?  

Any thoughts or pointers to relevant tutorials/documentation would be appreciated. 

-Rebecca
Reply all
Reply to author
Forward
0 new messages