【问题标题】:Shiny dashboard, user authentication [duplicate]闪亮的仪表板,用户身份验证[重复]
【发布时间】:2017-11-26 06:27:00
【问题描述】:

我正在尝试在我找到的代码 sn-p (https://github.com/treysp/shiny_password) 中包含一个闪亮的仪表板,它将闪亮的应用程序包装在函数中以设置用户身份验证。

这个 sn-ps 与 fluidPage() 完美配合,但我注意到当我包装 dhasboardPage() 时它不起作用:我尝试登录,输入我的用户名和密码,点击登录,然后什么也没有发生了,我卡在登录页面上。我通过调用 runApp() 来启动服务器的控制台中没有错误消息

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

提前致谢

【问题讨论】:

  • 欢迎来到 StackOverflow!您的问题没有为其他人提供足够的信息来重新创建和解决。请通读how to make a great R reproducible example 并在提问时关注。在这种情况下,请包含来自 Shiny 应用程序的一些代码。
  • 谢谢@StevenMortimer,关于进口的问题在我这边。为了重现性,下次我会考虑发布代码。

标签: r shiny shinydashboard


【解决方案1】:

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

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

  2. 我在server.r 中实现了大部分ui.r 代码。不确定是否有解决方法。我注意到的缺点是代码行太多。将每个侧选项卡分成一个单独的文件会很好。自己还没试过。不过,这里是@Dean Attali 到split code 的极好的闪亮资源

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
    }
  })
}

【讨论】:

  • 我确实设置了密码哈希,并以更模块化的方式分离了代码库。无论如何感谢您花时间回答这个问题,这可能对其他人有帮助。
  • @JakeM,很高兴知道。我正在学习 Rshiny 的模块方面。研究类似的方面。
  • @JakeM,除了我的答案中的链接之外,您能否指出您用来散列密码的资源?
  • @user5249203,@JakeM ,上面的代码对我来说很好,但是一旦我在闪亮的服务器上从本地机器部署一个完美运行的仪表板,它会重新加载多次而不显示内容,你遇到过吗同样的问题???
【解决方案2】:

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

Blogpost with example app

Package repo

包 repo 中的 inst/ 目录包含示例应用程序的代码。

【讨论】:

    【解决方案3】:

    @user5249203 的回答非常有用,但由于密码相同,因此会产生(非破坏性)。

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

    更好(更简单)的解决方案可能是替换后面的 6 行:

     Password <- isolate(input$passwd) 
    

     if (nrow(login_details[login_details$user == Username & 
                            login_details$pswd == Password,]) >= 1) {
        USER$Logged <- TRUE
     }
    

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 2016-07-11
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多