提取闪亮仪表板的活动选项卡名称仪表板、选项卡、名称

2023-09-03 14:05:11 作者:深情在睫、孤意在眉

我要提取活动的选项卡名。我有定制的侧边栏,不返回选定的(或活动的)选项卡名。因此,我不能使用此方法:在侧栏菜单中id参数,然后使用在服务器中输入$id来调用它。因此,我想使用Java脚本来解决这个问题。我尝试使用JS$('.tab-content').find('.active').attr('data-value'),但它不能正常工作。

library(shiny)
# devtools::install_github("MarkEdmondson1234/gentelellaShiny")
library(gentelellaShiny)
library(shinyWidgets)

options(shiny.jquery.version=1)

shinyApp(
  ui = gentelellaPageCustom(
    title = "Shiny Gentelella",
    navbar = gentelellaNavbar(
      navbarItems = notif(
        id = "menunotif",
        icon = icon("envelope-o"),
        status = "primary",
        expanded = FALSE,
        lapply(X = 1:5, FUN = function(i) {
          notifItem(
            title = "John Doe",
            date = "3 min ago",
            img = paste0("https://image.flaticon.com/icons/svg/163/16382", i,".svg"),
            "Film festivals used to be do-or-die moments
       for movie makers. They were where..."
          )
        })
      )
    ),
    sidebar = gentelellaSidebar(
      uiOutput("profile"),
      sidebarDate(),
      sidebarMenu(
        sidebarItem(
          "Tab 1",
          tabName = "tab1", 
          icon = tags$i(class = "fas fa-chart-bar"), 
          badgeName = "new",
          badgeStatus = "danger"
        ),
        sidebarItem(
          "Tab 2",
          tabName = "tab2", 
          icon = tags$i(class = "fas fa-info")
        )
      )
    ),
    body = gentelellaBody(
      tabItems(
        tabItem(
          tabName = "tab1",
          fluidRow(
            column(
              width = 4,
              align = "center",
              sliderInput(
                "obs",
                "Number of observations:",
                min = 0,
                max = 1000,
                value = 500
              )
            ),
            column(
              width = 8,
              align = "center",
              plotOutput("distPlot")
            )
          )
        ),
        tabItem(
          tabName = "tab2",
          jumbotron(
            title = "Hello, world!",
            "This is a simple hero unit, a simple jumbotron-style
        component for calling extra attention to featured
        content or information."
          )
        )
      )
    ),
    footer = gentelellaFooter()
  ),
  server = function(input, output, session) {
    output$distPlot <- renderPlot({
      hist(rnorm(input$obs))
    })

    counter <- reactiveValues(connect = 0)


    output$profile <- renderUI({
      sidebarProfile(
        name = input$name,
        img = "https://image.flaticon.com/icons/svg/236/236831.svg"
      )
    })
  }
)

推荐答案

代码如下:

js <- '
$(document).ready(function(){
  $("a[data-toggle=tab]").on("show.bs.tab", function(e){
    Shiny.setInputValue("activeTab", $(this).attr("data-value"));
  });
});
'
Excel批量处理文件,提取文件名

将其包含在用户界面中:

body = gentelellaBody(
  tags$head(
    tags$script(HTML(js))
  ),
  tabItems(
    ......

现在活动选项卡的名称由input[["activeTab"]]提供:

observe({
  print(input[["activeTab"]])
})

启动时为NULL。您可以改用以下JS代码:

js <- '
$(document).on("shiny:connected", function(){
  Shiny.setInputValue("activeTab", $("li.active>a").attr("data-value"));
  $("a[data-toggle=tab]").on("show.bs.tab", function(e){
    Shiny.setInputValue("activeTab", $(this).attr("data-value"));
  });
});
'

现在input[["activeTab"]]在启动时仍是NULL,但之后它会立即切换到第一个活动选项卡的名称。