Search code examples
rshiny

Is it possible to have a Shiny ConditionalPanel whose condition is a global variable?


My goal is to have a tabsetPanel wrapped in a conditionalPanel whose condition is a global variable being false.

ui.R

mainPanel(
 conditionalPanel("searchPage == \"false\"",
     tabsetPanel(
      tabPanel("Summary",htmlOutput("summary")),
      tabPanel("Description", htmlOutput("description"))
))),

global.R

searchPage <- "true"

then in server.R I assign new values to it a few different times, but all like this:

 observeEvent(input$openButton, 
           output$results <- renderUI({
             textOutput("")
             searchPage <- "false"
           }))

No matter what I do, I always get "Uncaught ReferenceError: searchPage is not defined". I've tried changing the global.R to multiple different combinations of using quotes, not using quotes, using <- or <<-, making it this.searchPage, my.searchPage and numerous other things (of course always making server.R and ui.R match too), but haven't had much luck at all.


Solution

  • As mentioned in a comment on the question's post, this is a perfect usecase for the shinyjs toggle()/show()/hide() functions. Whenever you need to conditionally show something where the condition is not a simple javascript expression of an input, it's easy to use these functions instead of a conditionalPanel().

    In order to use these functions, you need to have some way to specify the element you want to hide/show (in this case, the mainPanel()). The easist way to do this is to just wrap the entire panel in a div with an id. So define the panel like mainPanel(div(id = "mainpanel", ...)) and voila, there's an id to your panel, and now you can call shinyjs::show("mainpanel") or hide() whenever you want in the server code.