Shiny: conditionalPanel condition can use output values only if they are also rendered elsewhere in UI

Created on 24 Aug 2016  路  2Comments  路  Source: rstudio/shiny

Following up on my own Stack Overflow question.

This article on building a dynamic UI suggests that one way to pass values to a conditionalPanel is to use an output variable. The example given below shows this only works if that variable is also rendered elsewhere in the UI - the JavaScript does not seem to have access to the value if it is not also rendered.

The example given in the SO question shows the use-case of encapsulating a selectize input within a module, but here is a more minimal example:

library(shiny)


ui <- shinyUI(fluidPage(
  ## Uncomment the following line and the first condition will evaluate TRUE
  #textOutput('selected'),
  conditionalPanel(condition = "output.selected == 'Option one'", p('Option one is selected.')),
  conditionalPanel(condition = "output.selected != 'Option one'", p('Option one is NOT selected.'))
))


server <- shinyServer(function(input, output, session) {
  output$selected <- renderText('Option one')
})


shinyApp(ui = ui, server = server)

Most helpful comment

Right--we don't, by default, calculate/render output values if they aren't going to be visible. And if we don't calculate them, you can't use them in conditions.

You can change this behavior this by setting an output to calculate every time, you can use this in your server.R (replace outputId with the corresponding value):

outputOptions(output, "outputId", suspendWhenHidden = FALSE)

All 2 comments

Right--we don't, by default, calculate/render output values if they aren't going to be visible. And if we don't calculate them, you can't use them in conditions.

You can change this behavior this by setting an output to calculate every time, you can use this in your server.R (replace outputId with the corresponding value):

outputOptions(output, "outputId", suspendWhenHidden = FALSE)

Ahh yes that solves it, thank you, Joe. I updated the SO question with this solution.

Was this page helpful?
0 / 5 - 0 ratings