Rshiny - Disabling tabs / adding text to tabs

两盒软妹~` 提交于 2019-12-25 04:13:23

问题


I have a problem with shiny tabs. I want to create a navigation page with two tabs. Right to them, I would like to insert some user's login details. There is no option "text" or other to insert a text in the navbarPage. But I created an additionnal tab instead:

library(shiny)
runApp(list(
    ui = navbarPage(
        title="My App",
        tabPanel("tab1 title"),
        tabPanel("tab2 title"),
        tabPanel("User: Madzia")),
    server = function(input, output) { }
))

It is OK like this, but I do not want the third tab to be "selectible": I want it to be disabled, so that we cannot click on it - the same as on "My App" text. Do you have any idea about how to handle this problem? Thank you! Best, Madzia


回答1:


You can achieve disabling a tab with a tiny bit of javascript. I have an example of how to hide a tab (not disable) in recent blog post, you can see the code for that here. I modified that code a bit for disabling instead.

This code is hacky because it was done in 2 minutes but will work for a basic use case

library(shiny)
library(shinyjs)

jscode <- '
shinyjs.init = function() {
  $(".nav").on("click", ".disabled", function (e) {
    e.preventDefault();
    return false;
  });
}
'

css <- '
.disabled {
  background: #eee !important;
  cursor: default !important;
  color: black !important;
}
'

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    extendShinyjs(text = jscode, functions = "init"),
    tags$style(css),
    checkboxInput("foo", "Disable tab2", FALSE),
    tabsetPanel(
      id = "navbar",
      tabPanel(title = "tab1",
               value = "tab1",
               h1("Tab 1")
      ),
      tabPanel(title = "tab2",
               value = "tab2",
               h1("Tab 2")
      ),
      tabPanel(title = "tab3",
               value = "tab3",
               h1("Tab 3")
      )
    )
  ),
  server = function(input, output) {
    observe({
      toggleClass(condition = input$foo,
                  class = "disabled",
                  selector = "#navbar li a[data-value=tab2]")
    })
  }
)

Edit I didn't fully read the question when I posted my answer, I just saw that you wanted a way to disable a tab and that was my answer. Your specific usecase (creating a tab only to show the name of a user) is a bit strange, but I suppose this will still work...




回答2:


I would like to keep my previous answer in existence because it may be useful for someone in the future who wants to know how to disable a tab.

But for this specific problem, disabling the tab is not the correct approach. It makes more sense to simply add text to the tab (as Valter pointed out in a comment). If you look at the documentation for bootstrap, it says you can add text into the navbar by adding an html element with class navbar-text. I experimented with the HTML a little bit to figure out exactly where this needs to be done, and created a little function that will wrap around navbarPage() to allow you to add text to it.

Here's an example:

library(shiny)

navbarPageWithText <- function(..., text) {
  navbar <- navbarPage(...)
  textEl <- tags$p(class = "navbar-text", text)
  navbar[[3]][[1]]$children[[1]] <- htmltools::tagAppendChild(
    navbar[[3]][[1]]$children[[1]], textEl)
  navbar
}

ui <- navbarPageWithText(
  "Test app",
  tabPanel("tab1", "tab 1"),
  tabPanel("tab2", "tab 2"),
  text = "User: Dean"
)

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

shinyApp(ui = ui, server = server)


来源:https://stackoverflow.com/questions/40741691/rshiny-disabling-tabs-adding-text-to-tabs

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