简体   繁体   English

选项卡上的显示/隐藏按钮选择R闪亮

[英]Show/Hide button on tab select R shiny

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 我的ui.R中有一个按钮,我只想在选择“摘要”选项卡时显示,所以我想到了这段代码

 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. 问题是,按钮是隐藏的,即使在我的第一个选项卡中它应该是可见的,当我转到Plots并返回Summary时,按钮保持隐藏状态。

After looking at: How to use tabPanel as input in R Shiny? 看看之后: 如何在R Shiny中使用tabPanel作为输入?

I decided to play with observeEvent and the input$tabset option. 我决定使用observeEvent和输入$ tabset选项。 The result is 100% working and it's really simple. 结果是100%工作,而且非常简单。 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. 此外,我发现了为什么我以前的代码无法正常工作,这是因为当UI初始化时,按钮元素保留了最后一个样式修改(隐藏的代码),并且根据选项卡没有改变我选择了,因为它没有被动反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM