2

I have a button in my ui.R that I want to be shown only when "Summary" tab is selected, so I thought of this code

 fluidRow(
  column(4, 
   column(12,id="sub",
       actionButton("submit", "SUBMIT", width = "100%"))),
  column(8,
   bsCollapse(id = "collapse7", open = "Results",
       bsCollapsePanel("Results",
         tabsetPanel(
          tabPanel("Summary",
            tags$script(HTML("document.getElementById('sub').style.visibility = 'visible';")))
          tabPanel("Plot",
            tags$script(HTML("document.getElementById('sub').style.visibility = 'hidden';"))))
        ))))

The problem is, the button is hidden even though in my first tab it should be visible and also when i go to Plots and back to Summary, the button stays hidden.

CnewbieWannabePro
  • 124
  • 1
  • 1
  • 14
  • have a look at deans `shinyjs` package. http://deanattali.com/2015/04/23/shinyjs-r-package/ – Pork Chop Jan 19 '17 at 10:59
  • @PorkChop ty for the link, i checked it out and found many useful things. I posted the solution to my problem after I managed to solve it myself below. Thanks for the info. – CnewbieWannabePro Jan 19 '17 at 11:39

1 Answers1

3

After looking at: How to use tabPanel as input in R Shiny?

I decided to play with observeEvent and the input$tabset option. The result is 100% working and it's really simple. Here's the code:

observeEvent(input$choices, {
 choice = input$choices
 if(choice == "Summary")
 {
  runjs(
    "document.getElementById('submit').style.visibility = 'visible';"
  )
 }
 else
 {
  runjs(
    "document.getElementById('submit').style.visibility = 'hidden';"
  )
 }
})

Also, I found out why my previous code wasn't working, it was due to the fact that when the UI was initialized, the button element kept the last style modification (the hidden one) and it didn't change depending on the tab I have selected, since its not reactive.

Community
  • 1
  • 1
CnewbieWannabePro
  • 124
  • 1
  • 1
  • 14