Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditionalPanel condition can use output values only if they are also rendered elsewhere in UI #1318

Closed
brianstamper opened this issue Aug 24, 2016 · 2 comments

Comments

@brianstamper
Copy link

brianstamper commented Aug 24, 2016

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)
@jcheng5
Copy link
Member

jcheng5 commented Aug 25, 2016

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)

@jcheng5 jcheng5 closed this as completed Aug 25, 2016
@brianstamper
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants