Starting Shiny app after password input (with Shinydashboard)

后端 未结 3 1472
生来不讨喜
生来不讨喜 2020-12-03 04:28

In this topic is well explained how to start the shinyapp after some password input. I am trying to do the same, but instead of \"navbarPage\", I would like to have a \"dash

3条回答
  •  旧时难觅i
    2020-12-03 04:32

    Your example uses a single user. I made some modifications for multiple user/password situations. This seems to work for me. Hopefully, others may find it helpful:

    library(shiny)
    library(shinydashboard)
    library(tidyverse)
    
    user_base <- tibble(
      user =     c("Test1", "Test2", "Test3"),
      password = c("abc", "bcd", "cde"),
      name =     c("User1", "User2", "User3")
    )
    
    ###########################/ui.R/##################################
    
    header <- dashboardHeader(title = "my heading")
    sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
    body <- dashboardBody(uiOutput("body") )
    ui <- dashboardPage(header, sidebar, body)
    
    ###########################/server.R/##################################
    
    server <- function(input, output, session) {
      Logged <- FALSE
    
      USER <<- reactiveValues(Logged = Logged)
    
      observe({ 
        if (USER$Logged == FALSE) {
          if (!is.null(input$Login)) {
            if (input$Login > 0) {
              Username <- isolate(input$userName)
              Password <- isolate(input$passwd)
              Id.username <- which(user_base$user == Username)
              Id.password <- which(user_base$password == Password)
              if (length(Id.username) > 0 & length(Id.password) > 0) {
                if (Id.username == Id.password) {
                  USER$Logged <<- TRUE
                } 
              }
            } 
          }
        }    
      })
    
      output$sidebarpanel <- renderUI({
        if (USER$Logged == TRUE) { 
          dashboardSidebar(
            sidebarUserPanel("myuser", subtitle = a(icon("user"), "Logout", href="")),
            selectInput("in_var", "myvar", multiple = FALSE,
                        choices = c("option 1","option 2")),
            sidebarMenu(
              menuItem("Item 1", tabName = "t_item1", icon = icon("line-chart")),
              menuItem("Item 2", tabName = "t_item2", icon = icon("dollar")),
              menuItem("Item 3", tabName = "t_item3", icon = icon("credit-card")),
              menuItem("Item 4", tabName = "t_item4", icon = icon("share-alt"))
            ))}
      })
    
      output$body <- renderUI({
        if (USER$Logged == TRUE) {
          B <- c(2,3,4,3,7,5,4)
    
          box(
            title = p("Histogram", actionLink("Expand", "", icon = icon("expand"))), 
            status = "primary", solidHeader = TRUE, width = 4,
            hist(B)
          )
        }
        if (USER$Logged == FALSE) {
          box(title = "Login",textInput("userName", "Username"),
              passwordInput("passwd", "Password"),
              br(),
              actionButton("Login", "Log in"))
        }
      })
    }
    
    shinyApp(ui, server)
    

提交回复
热议问题