Rshiny - 禁用标签/向标签添加文本 [英] Rshiny - Disabling tabs / adding text to tabs

查看:28
本文介绍了Rshiny - 禁用标签/向标签添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对闪亮的标签有疑问.我想创建一个带有两个选项卡的导航页面.对他们来说,我想插入一些用户的登录详细信息.没有选项文本"或其他选项可以在导航栏页面中插入文本.但我创建了一个附加选项卡:

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

推荐答案

您可以使用一点点 javascript 实现禁用选项卡.我在最近的博客文章中有一个关于如何隐藏选项卡(而不是禁用)的示例,您可以看到 此处.我修改了该代码以禁用.

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.

这段代码很烂,因为它在 2 分钟内完成,但适用于基本用例

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...

这篇关于Rshiny - 禁用标签/向标签添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆