首页 > 解决方案 > 使用闪亮模块的简单密码设置

问题描述

我能够按照这个解决方案设置一个闪亮的应用程序密码: 在输入密码后启动闪亮的应用程序(使用 Shinydashboard)

现在,我刚刚接触了闪亮的模块,所以我开始想知道是否可以使用模块来制作一个易于设置的密码模块,我可以在我拥有的其他应用程序中实现它。(我使用模块的原因只是了解它。这可以使用常规函数来完成)。

这是我目前的工作解决方案:

##This part of the module is going to set modal's UI
passwordModuleUI=function(id,failed=FALSE){
  ns=NS(id)
  tagList(
    modalDialog(
      textInput(ns("username"), "Usuário:"),
      passwordInput(ns("password"), "Senha:"),
      footer = tagList(
        # modalButton("Cancel"),
        actionButton(ns("ok"), "OK")
      )
    )
  )
}

#This will load modal UI from observer inside the server
passwordModuleShow=function(ID){
  showModal(passwordModuleUI(ID))
}

#This is server side module
passwordModuleEval=function(input,output,session,id,users,passwords){
  authenticated <- FALSE
  isolate({
    Username <- input$username
    Password <- input$password
  })
  Id.username <- which(users == Username)
  Id.password <- grep(Password,passwords)
  if ((length(Id.username) > 0&nchar(Password)>1) & (length(Id.password) > 0)&nchar(Username)>1) {
    if (Id.username %in% Id.password) {
      authenticated <- TRUE
      removeModal()

    } else {
      authenticated <- FALSE
    }     
  }
  return(authenticated)
}
#This is trying to simplify implementation in shiny apps
setupPassProtection=function(input,output,session,ID,Users,Passwords){

  obs1=observe(priority = 1,{
    passwordModuleShow(ID=ID)
  })

  observeEvent(input[[paste0(ID,"ok",sep="-")]],{
    if(callModule(passwordModuleEval,ID,users=Users,passwords=Passwords))
    {obs1$suspend()}
  }) 
}

my_username=my_password="test"
ui=basicPage(h3("testando passprotect module"))
server=function(input,output,session){
  passID="test"
  obs1=observe(priority = 1,{
    passwordModuleShow(ID=passID)
  })

  observeEvent(input[[paste0(passID,"-ok")]],{
    if(callModule(passwordModuleEval,"test",users=my_username,passwords=my_password))
    {obs1$suspend()}
  })
}


shinyApp(ui,server)

我想让以下 SERVER 工作,因为它是最简单且可读性更强的版本,而且我可以轻松获取其他功能。

这不起作用:

server=function(input,output,session){
  setupPassProtection(input,output,session,ID="test",Users=my_username,Passwords=my_password)

}

感谢有关解决方案或代码简化的任何帮助。

标签: rmoduleshiny

解决方案


推荐阅读