Dynamically display a dashboardPage

后端 未结 2 585
长情又很酷
长情又很酷 2021-02-19 09:03

I have a functional shiny app that uses the shinydashboard package.

A new feature requires user-specific behavior (e.g. use different data sets for differen

2条回答
  •  借酒劲吻你
    2021-02-19 09:48

    Not sure this is the kind of solution you are after, but here's my attempt using shinyjs and some CSS. It seems hard to switch from a fixedPage to a dashboardPage, so if you really want to use shinydashboard, I would stick with shinydashboard and disable the dashboard look on the login page.

    library(shiny)
    library(shinyjs)
    library(shinydashboard)
    
    ui1 <- div(
      id = "login-page",
      actionButton("btn_login", "Login")
    )
    
    ui2 <- hidden(    
      div(
        id = "main-page",
        sliderInput("slider", "slider", 3, 2, 2)
      )
    )
    
    ui <- dashboardPage(dashboardHeader(), 
                        dashboardSidebar(collapsed = TRUE), 
                        dashboardBody(useShinyjs(),
                                      tags$head(
                                        tags$style(
                                          HTML('.main-header {
                                                  display: none;
                                                }
    
                                                .header-visible {
                                                  display: inherit;
                                                }')
                                        )
                                      ),
                                      fluidPage(ui1, ui2)
                        )
    )
    
    server <- function(input, output, session) {
    
      state <- reactiveValues(LoggedIn = FALSE)
    
      observeEvent(input$btn_login, {
        state$LoggedIn = TRUE
        shinyjs::addClass(selector = "header", class = "header-visible")
        shinyjs::removeClass(selector = "body", class = "sidebar-collapse")
        shinyjs::hide(id = "login-page")
        shinyjs::show(id = "main-page")
      })
    
    }
    
    shinyApp(ui, server)
    

    If you want to be able to come back to the login page, you can always add a login button that shows the login page, and hides the appropriate elements (sidebar/header/current page).

提交回复
热议问题