0

I'm building a R Shiny dashboard, and am trying to implement conditionalPanels. Granted, I'm new to JavaScript. There's plenty of posts on SO about conditionalPanels, but every single one I've seen uses input.something in its condition expression, typically like this:

conditionalPanel(
    condition = "input.tabs == 'correlation'",

    actionButton("generate_correlation", "Generate Heat Map")
  )

I know that the expression inside the condition statement is a string that is evaluated as a JavaScript expression, but I'm wondering if it's possible to access other variables and objects beyond input? Can it evaluate reactive functions, for instance? I would like to have a button be pressed, and then a new tab conditionally appear as a result. I've create

         tabPanel("Tab 1",
                  actionButton("dropColumnsComplete", "Next"))

Then, I've created a reactive expression on my server side to handle this:

  dropColumnsComplete <- reactive(input$dropColumnsComplete,{
    return(TRUE)
  })

Clicking the button triggers the input$dropColumnsComplete event, which should invoke the reactive expression that returns TRUE when the function dropColumnsComplete() is called. Then, I add in

     conditionalPanel(condition = "dropColumnsComplete()",
                      tabPanel("Tab 2")
                      ),

and I expect that pressing the button will cause Tab 2 to appear. I know I'm not accessing the dropColumnsComplete() inside the condition expression correctly- should it be document.dropColumnsComplete()? shiny.dropColumnsComplete()? There's a surprising lack of documentation on this, so I'm at a loss for how to access variables beyond input$... within JavaScript.

Yu Chen
  • 6,540
  • 6
  • 51
  • 86

1 Answers1

0

There is no reason to write reactives to conditionally show/hide elements. Take a look at shinyjs instead by Dean Attali

Example of using shinyjs:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("show", "Click me once to show stuff!"),
  actionButton("toggle", "Click me any times to toggle stuff!"),
  hidden(
    div(id = "d1",
      "Content hidden until button is clicked"
    )
  ),
  hidden(
    div(id = "d2",
        "Content toggled when button is clicked"
    )
  )
)

server <- function(input, output) {
  observeEvent(input$show, {
    show("d1")
  })

  observeEvent(input$toggle, {
    toggle("d2")
  })
}
shinyApp(ui, server)
GyD
  • 3,902
  • 2
  • 18
  • 28
  • Thanks, but how would this work with `tabPanel`s in R? Since there isn't an element id I can reference as your case with `div` has. – Yu Chen Sep 28 '17 at 15:21
  • Actually, I just found this great resource: https://stackoverflow.com/questions/31703241/activate-tabpanel-from-another-tabpanel/31719425#31719425. – Yu Chen Sep 28 '17 at 15:23