Dynamically display a dashboardPage

后端 未结 2 569
长情又很酷
长情又很酷 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:29

    It also works with invalidateLater(), but also only temporary.

    library(shiny)
    library(shinydashboard)
    
    ui <- uiOutput("ui")
    
    server <- function(input, output, session) {
    
      state <- reactiveValues(LoggedIn = FALSE)
    
      observeEvent(input$btn_login, {
        state$LoggedIn = !state$LoggedIn
      })
    
      ui1 <- reactive({
        fixedPage(actionButton("btn_login", "Login"))
      })
    
      ui2 <- reactive({
        ui2 <- dashboardPage(dashboardHeader(), dashboardSidebar(), dashboardBody(
           sliderInput("slider", "slider", min = 1, max = 10, value = 2)
         ))
        invalidateLater(100, session)
        ui2
      })
    
      output$ui <- renderUI({if (!state$LoggedIn) ui1() else ui2()})
    
    }
    
    shinyApp(ui, server)
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题