闪亮的仪表板,用户身份验证

时间:2017-06-22 20:06:43

标签: r shiny shinydashboard

我正在尝试在我找到的代码段(https://github.com/treysp/shiny_password)中包含一个闪亮的仪表板,该代码段在功能中包含一个闪亮的应用程序以设置用户身份验证。

这个片段与fluidPage()完美配合,但我注意到当我包装dhasboardPage()时它无效:我尝试登录,输入我的用户名和密码,点击登录然后没有任何反应,我被困在登录页面上。控制台中没有错误消息我通过调用runApp()

来启动服务器

您是否知道可能导致此特定问题的原因?

提前致谢

3 个答案:

答案 0 :(得分:5)

这是一个开始的工作示例。这是一个非常基本的实现。

  1. 在测试用例中,存储的密码是可见的。您不希望以这种方式进行身份验证。这是不安全的。您需要找到一种方法来散列密码和匹配。惠东天github link

  2. 有一些线索
  3. 我在ui.r中实施了大部分server.r代码。不确定是否有解决方法。我注意到的缺点是代码行太多。将每个边标签分成单独的文件会很好。我自己没试过。但是,@ Dean Attali是split code

  4. 的极好闪亮资源

    <强> ui.r

    require(shiny)
    require(shinydashboard)
    
    header <- dashboardHeader(title = "my heading")
    sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
    body <- dashboardBody(uiOutput("body"))
    ui <- dashboardPage(header, sidebar, body)
    

    <强> server.r

    login_details <- data.frame(user = c("sam", "pam", "ron"),
                                pswd = c("123", "123", "123"))
    login <- box(
      title = "Login",
      textInput("userName", "Username"),
      passwordInput("passwd", "Password"),
      br(),
      actionButton("Login", "Log in")
    )
    
    server <- function(input, output, session) {
      # To logout back to login page
      login.page = paste(
        isolate(session$clientData$url_protocol),
        "//",
        isolate(session$clientData$url_hostname),
        ":",
        isolate(session$clientData$url_port),
        sep = ""
      )
      histdata <- rnorm(500)
      USER <- reactiveValues(Logged = F)
      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(login_details$user %in% Username)
              Id.password <- which(login_details$pswd %in% 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) {
          div(
            sidebarUserPanel(
              isolate(input$userName),
              subtitle = a(icon("usr"), "Logout", href = login.page)
            ),
            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"))
            )
          )
        }
      })
    
      output$body <- renderUI({
        if (USER$Logged == TRUE) {
          tabItems(
            # First tab content
            tabItem(tabName = "t_item1",
                    fluidRow(
                      output$plot1 <- renderPlot({
                        data <- histdata[seq_len(input$slider)]
                        hist(data)
                      }, height = 300, width = 300) ,
                      box(
                        title = "Controls",
                        sliderInput("slider", "observations:", 1, 100, 50)
                      )
                    )),
    
            # Second tab content
            tabItem(
              tabName = "t_item2",
              fluidRow(
                output$table1 <- renderDataTable({
                  iris
                }),
                box(
                  title = "Controls",
                  sliderInput("slider", "observations:", 1, 100, 50)
                )
              )
            )
          )
        } else {
          login
        }
      })
    }
    

答案 1 :(得分:2)

我最近写了一个R包,其中提供了可以与Shinydashboard集成的登录/注销模块。

Blogpost with example app

Package repo

软件包仓库中的inst/目录包含示例应用程序的代码。

答案 2 :(得分:0)

@ user5249203的答案非常有用,但是由于密码相同,因此会产生一个(不间断的)信息。

textStorage

更好(或更简单)的解决方案可能是在以下位置替换6行:

Warning in if (Id.username == Id.password) { :
  the condition has length > 1 and only the first element will be used

使用

 Password <- isolate(input$passwd) 
相关问题