Show/Hide button on tab select R shiny

跟風遠走 提交于 2020-01-23 03:44:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41738927/show-hide-button-on-tab-select-r-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!