如何使shinydashboard中的一个选项卡不可见?

huangapple go评论114阅读模式
英文:

How can I make a tab of a shinydashboard invisible?

问题

我尝试使用CSS样式(对按钮有效)来隐藏以下应用程序的第三个选项卡:

library(shiny)
library(shinydashboard)
library(shinyDashboardPlus)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      id = "tabs",  # 为侧边栏菜单设置一个ID
      menuItem("选项卡1", tabName = "tab1"),
      menuItem("选项卡2", tabName = "tab2"),
      menuItem("隐藏选项卡", tabName = "invisible_tab", style = "visibility: hidden;")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "tab1",
        useShinyjs()
      ),
      tabItem(
        tabName = "tab2",
        useShinyjs(), 
        h3("选项卡2")
      ),
      tabItem(
        tabName = "invisible_tab",
        h3("隐藏选项卡内容")
      )
    )
  )
)

server <- function(input, output, session) {
}

shinyApp(ui, server)

选项卡的内容不会显示,但选项卡的标题仍然显示在侧边栏中...我如何使它隐藏,就像它不存在一样?

英文:

I've tried using CSS styling (that works on buttons) to hide the third tab of the following app:

library(shiny)
library(shinydashboard)
library(shinyDashboardPlus)
library(shinyjs)

ui &lt;- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      id = &quot;tabs&quot;,  # Set an ID for the sidebar menu
      menuItem(&quot;Tab 1&quot;, tabName = &quot;tab1&quot;),
      menuItem(&quot;Tab 2&quot;, tabName = &quot;tab2&quot;),
      menuItem(&quot;Invisible Tab&quot;, tabName = &quot;invisible_tab&quot;, style = &quot;visibility: hidden;&quot;)
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = &quot;tab1&quot;,
        useShinyjs()
      ),
      tabItem(
        tabName = &quot;tab2&quot;,
        useShinyjs(), 
        h3(&quot;tab2&quot;)
      ),
      tabItem(
        tabName = &quot;invisible_tab&quot;,
        h3(&quot;Invisible Tab Content&quot;)
      )
    )
  )
)

server &lt;- function(input, output, session) {
}

shinyApp(ui, server)

The content of the tab is not displayed, but the title of the tab remains in the sidebar... How can I hide it just as is did not exist?

答案1

得分: 1

所有`menuItem`都由一个带有`href`属性的`&lt;a&gt;`标签定义,该属性构造为`#shiny-tab-[tabName]`,其中`tabName`是`menuItem`的相应参数。

因此,如果你使用以下方式定义你的`menuItem`:

menuItem("Invisible Tab", tabName = "invisible_tab")

那么你可以应用以下`CSS`:

a[href = '#shiny-tab-invisible_tab']{
visibility: hidden;
}

以使其不可见。
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)

ui &lt;- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(
        tags$head(tags$style(HTML(&quot;
                                  a[href = &#39;#shiny-tab-invisible_tab&#39;]{
                                      visibility: hidden;
                                  }
                                  &quot;))),
        sidebarMenu(
            id = &quot;tabs&quot;,  # 为侧边栏菜单设置一个ID
            menuItem(&quot;Tab 1&quot;, tabName = &quot;tab1&quot;),
            menuItem(&quot;Tab 2&quot;, tabName = &quot;tab2&quot;),
            menuItem(&quot;Invisible Tab&quot;, tabName = &quot;invisible_tab&quot;)
        )
    ),
    dashboardBody(
        tabItems(
            tabItem(
                tabName = &quot;tab1&quot;,
                useShinyjs()
            ),
            tabItem(
                tabName = &quot;tab2&quot;,
                useShinyjs(), 
                h3(&quot;tab2&quot;)
            ),
            tabItem(
                tabName = &quot;invisible_tab&quot;,
                h3(&quot;Invisible Tab Content&quot;)
            )
        )
    )
)

server &lt;- function(input, output, session) {
}

shinyApp(ui, server)
英文:

All menuItem are defined by an &lt;a&gt; tag with an href attribute which is constructed as #shiny-tab-[tabName], where tabName is the corresponding parameter of menuItem.

Hence if you define your menuItem using

menuItem(&quot;Invisible Tab&quot;, tabName = &quot;invisible_tab&quot;)

then you can apply the CSS

a[href = &#39;#shiny-tab-invisible_tab&#39;]{
    visibility: hidden;
}

for setting it unvisible.

如何使shinydashboard中的一个选项卡不可见?

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)

ui &lt;- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(
        tags$head(tags$style(HTML(&quot;
                                  a[href = &#39;#shiny-tab-invisible_tab&#39;]{
                                      visibility: hidden;
                                  }
                                  &quot;))),
        sidebarMenu(
            id = &quot;tabs&quot;,  # Set an ID for the sidebar menu
            menuItem(&quot;Tab 1&quot;, tabName = &quot;tab1&quot;),
            menuItem(&quot;Tab 2&quot;, tabName = &quot;tab2&quot;),
            menuItem(&quot;Invisible Tab&quot;, tabName = &quot;invisible_tab&quot;)
        )
    ),
    dashboardBody(
        tabItems(
            tabItem(
                tabName = &quot;tab1&quot;,
                useShinyjs()
            ),
            tabItem(
                tabName = &quot;tab2&quot;,
                useShinyjs(), 
                h3(&quot;tab2&quot;)
            ),
            tabItem(
                tabName = &quot;invisible_tab&quot;,
                h3(&quot;Invisible Tab Content&quot;)
            )
        )
    )
)

server &lt;- function(input, output, session) {
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年8月11日 01:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定