【问题标题】:Navigate through tabitems based on conditions matched in shiny dashboard根据闪亮仪表板中匹配的条件浏览 tabitems
【发布时间】:2020-11-20 11:27:29
【问题描述】:

我有下面的闪亮应用程序,其中包含 3 个操作按钮。每个按钮都与某个选项卡相连。我想做的是根据标签Consent的内容构建如下架构。

当应用程序第一次执行时,用户登陆Welcome 标签。然后按下Get started 操作按钮。

如果用户在Consent标签中输入了consent.name和相对textInput()Name,在按下Get started按钮之前,他将被移动到Password标签。

如果用户没有在Name中输入文本就按下Get started按钮,那么他将被移动到Consent选项卡中,在Name中输入文本,按Continue然后被移动到@987654336 @标签。

我想知道如何有条件地使用这些操作按钮来浏览标签。当然,用户可以通过单击顶部的操作按钮转到他想要的任何选项卡,但是当单击 Get started 按钮时,上述过程将被激活。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
mytitle <- paste0("Life, Death & Statins")
dbHeader <- dashboardHeaderPlus(

)

shinyApp(
  ui = dashboardPagePlus(
    header = dbHeader,
    sidebar = dashboardSidebar(width = "0px",
                               sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           menuItem("Welcome", tabName = "well", icon = icon("house")),
                                           menuItem("Consent", tabName = "conse", icon = icon("line-chart")),
                                           menuItem("Password", tabName = "pswd", icon = icon("house"))
                                           
                               )           ),
    body = dashboardBody(
      fluidRow(
        column(6,),
        column(1,
               tags$li(class = "dropdown", actionButton("well", "Welcome"))),
        column(1,
               tags$li(class = "dropdown", actionButton("conse", "Consent"))),
        column(1,
               tags$li(class = "dropdown", actionButton("pswd", "Password")))
        ),
      
      tabItems(
        tabItem("well",
                         actionButton("button", "Get started",style='padding:4px; font-size:140%')),
        tabItem("conse",
                
                tags$hr(),
                fluidRow(column(3,textInput("name", label = ("Name"), value = "consent.name"))),
                tags$hr(),
                fluidRow(column(3,actionButton('continue', "Continue",style='padding:4px; font-size:180%')))
        ),
        tabItem("pswd",
                passwordInput("pwd", "Enter the Database browser password")
                  )
        
      ),
      
      
      
    )
    
  ),
  server<-shinyServer(function(input, output,session) { 
    hide(selector = "body > div > header > nav > a")
    observeEvent(input$well, {
      updateTabItems(session, "sidebar", "well")
    })
    observeEvent(input$conse, {
      updateTabItems(session, "sidebar", "conse")
    })
    observeEvent(input$pswd, {
      updateTabItems(session, "sidebar", "pswd")
    })
    
    
  }
  )
)

【问题讨论】:

  • 我用this Q适配tabItems。

标签: r shiny shinydashboard


【解决方案1】:

一种方法是再添加两个observeEvents,如下所示:

server<-shinyServer(function(input, output,session) { 
    hide(selector = "body > div > header > nav > a")
    
    observeEvent(input$well, {
      updateTabItems(session, "sidebar", "well")
    })
    
    observeEvent(input$conse, {
      updateTabItems(session, "sidebar", "conse")
    })
      
    observeEvent(input$pswd, {
      updateTabItems(session, "sidebar", "pswd")
    })
    
    observeEvent(input$button, {
      if (is.null(input$name) | nchar(gsub("[[:space:]]", "", input$name))==0 | input$name=="consent.name" ) {
        updateTabItems(session, "sidebar", "conse")
      }
      
      if (!is.null(input$name) & nchar(gsub("[[:space:]]", "", input$name))>0 & input$name!="consent.name" ){
        updateTabItems(session, "sidebar", "pswd")
      }
    })
    
    observeEvent(input$continue, {
      if (!is.null(input$name) & nchar(gsub("[[:space:]]", "", input$name))>0 & input$name!="consent.name" ){
        updateTabItems(session, "sidebar", "pswd")
      }
      
    })
    
  }
  )

【讨论】:

    猜你喜欢
    • 2016-12-19
    • 2019-07-26
    • 2015-04-22
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多