Activate tabItem with either an actionButton or another actionButton in a shiny app

不羁岁月 提交于 2021-01-07 02:51:54

问题


I have the shiny app below which includes tabItems. I would like when the app is launched for first time to display an actionbutton which will be out of the tabItems content. Then when I press it I will be moved into the Consent tabItem. This button will have no use from there and later and it should be disappeared since the tabItems' content will be displayed. I want also the user to be able to press Consent button before pressing "Get Started" button and still be able to move to Consent tabItem.

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

mytitle <- paste0("")
dbHeader <- dashboardHeaderPlus(
  titleWidth = "0px",
  tags$li(a(
    div(style="display: inline;margin-top:-35px; padding: 0px 90px 0px 1250px ;font-size: 44px ;color:#001641;font-family:Chronicle Display Light; width: 500px;",HTML(mytitle)),


    div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("conse", "Consent",
                                                                                                         style=" background-color: #faf0e6; border-color: #faf0e6") ),

  ),  class = "dropdown")
)

shinyApp(
  ui = dashboardPagePlus(
    header = dbHeader,
    sidebar = dashboardSidebar(width = "0px",
                               sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           menuItem("Consent", tabName = "conse", icon = icon("line-chart"))
                               )           ),
    body = dashboardBody(
      tags$head(tags$style(HTML('

    '))),
      useShinyjs(),
      tags$script(HTML("$('body').addClass('fixed');")),

      tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),

      actionButton("button", "Get started",style='padding:4px; font-size:140%'),
      tabItems(
        tabItem("conse", textInput("pos", label = ("Position"), value = "") )
      )

    )

  ),
  server<-shinyServer(function(input, output,session) {
    hide(selector = "body > div > header > nav > a")
    shinyjs::hide("conse")
    shinyjs::hide("pos")

    observeEvent(input$button, {
      shinyjs::show("conse")
      shinyjs::show("pos")
      updateTabItems(session, "sidebar",
                     selected = "conse")
      shinyjs::hide("button")

    })

  }
  )
)

回答1:


One way to do it is use shinyjs to hide the element in the tabItem. Relevant code is below.

shinyApp(
  ui = dashboardPagePlus(
    header = dbHeader,
    sidebar = dashboardSidebar(width = "0px",
                               sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           menuItem("Consent", tabName = "conse", icon = icon("line-chart"))
                               )
                               ),
    body = dashboardBody(
      useShinyjs(),
      tags$script(HTML("$('body').addClass('fixed');")),

      tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),

      actionButton("button", "Get started", style='padding:4px; font-size:140%'),
      tabItems(
        tabItem("conse", textInput("pos", label = ("Position"), value = "")
                )
      )

    )
  ),

  server<-shinyServer(function(input, output,session) {
    hide(selector = "body > div > header > nav > a")
    shinyjs::hide("pos")

    observeEvent(input$button, {
      shinyjs::show("pos")
      updateTabItems(session, "sidebar", selected = "conse")
      shinyjs::hide("button")

    })
    observeEvent(input$conse, {
        shinyjs::show("pos")
        updateTabItems(session, "sidebar", selected = "conse")
        shinyjs::hide("button")

    })

  }
  )
)

Another way to do it is use conditionalPanel to hide all items in the tab. Depending on your use case, this may be a better option.

shinyApp(
  ui = dashboardPagePlus(
    header = dbHeader,
    sidebar = dashboardSidebar(width = "0px",
                               sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           menuItem("Consent", tabName = "conse", icon = icon("line-chart"))
                               )           
    ),
    body = dashboardBody(
      useShinyjs(),
      tags$script(HTML("$('body').addClass('fixed');")),
      
      tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),
      
      actionButton("button", "Get started", style='padding:4px; font-size:140%'),
      
      tabItems(
        tabItem("conse", 
                conditionalPanel(condition = "input.conse >0 || input.button>0",
                                 textInput("pos", label = ("Position"), value = "")
                )
        )
      )
      
    )
  ),
  
  server<-shinyServer(function(input, output,session) {
    hide(selector = "body > div > header > nav > a")
    
    observeEvent(input$button, {
      updateTabItems(session, "sidebar", selected = "conse")
      shinyjs::hide("button")
      
    })
    observeEvent(input$conse, {
      updateTabItems(session, "sidebar", selected = "conse")
      shinyjs::hide("button")
    })
    
  }
  )
)


来源:https://stackoverflow.com/questions/65535690/activate-tabitem-with-either-an-actionbutton-or-another-actionbutton-in-a-shiny

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