所以我有一个dataframe
,其中指定了username
,password
和Roles
。我已经创建了一个ShinyDashboard
登录页面,用于根据角色创建UI,但是我无法弄清楚如何验证何时将其存储在数据框中。
Server.R
my_usernames <- c("u1","u2")
my_passwords <- c("u10", "u20")
roles<-c("admin","user")
shinyServer(function(input,output,session)
{
options(digits = 22)
USER <- reactiveValues(Logged = Logged, role= NULL)
# Return the UI for a modal dialog with data selection input. If 'failed'
# is TRUE, then display a message that the previous value was invalid.
loginModal <- function(failed = FALSE){
modalDialog( tags$img(src = "new.gif",width="140", height="50px"),br(),br(),
h3("Info App") ,br(),br(),br(),
textInput("userName", "Username"),
passwordInput("passwd", "Password"),
if (failed)
div(tags$b("Invalid Username / Password", style = "color: red;")),
size = "s",
footer = tagList(
actionButton("Login", "Log in")
)
)
}
# Show modal when button is clicked.
# This `observe` is suspended only whith right user credential
loginobs1 <- observe({
showModal(tags$div(id = "loginmodal1",loginModal()))
})
# When OK button is pressed, attempt to authenticate. If successful,
# remove the modal.
loginobs2 <- observe({
req(input$Login)
if (USER$Logged == FALSE) {
if (!is.null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
" Id.username <- which(my_username == Username)
Id.password <- which(my_password == Password)"
if ((length(Username) > 0 && length(Password) > 0)) {
if(my_passwords[which(my_usernames==Username)]==Password)
{
# browser()
USER$Logged <<- TRUE
if(Username=="u1")
{
loginobs1$suspend()
removeModal()
USER$role<-roles[1]
}
else{
if(Username=="u2")
{
loginobs1$suspend()
removeModal()
USER$role<-roles[2]
}
}
}
else {
USER$Logged <- FALSE
}
}
else {
USER$Logged <- FALSE
}
}
}
}
})
observe({
if ((USER$Logged == TRUE)){
if(USER$role == "admin"){
output$side <- renderUI({
ui2_side
})
output$page <- renderUI({
ui2_main
})
}
if(USER$role == "user"){
output$side <- renderUI({
ui3_side
})
output$page <- renderUI({
ui3_main
})
}
}
})
})
UI.R
library(shiny)
library(shinydashboard)
library(shinyjs)
shinyUI(
(
dashboardPage(
dashboardHeader(title = div(img(src="new.gif", height = 40, width = 200),"My_dashboard",width = 300)),
dashboardSidebar(
uiOutput("side"),width = 190),
dashboardBody(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = "shinyjs.refresh = function() { location.reload(); }"),
uiOutput("page",height=1000)
)
)
)
)
#UI for admin
ui2_side=list(
sidebarMenu(
actionButton("Logout", "Logout", icon = icon("sign-out")),
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
))
ui2_main<- list(
tabItems(
tabItem("dashboard",
tabsetPanel(
tabPanel( title = "Plot Info",
br(),
fluidRow(
box(
h3("All Status", align="center"))
)
)))))
#UI for users
ui3_side=list(
sidebarMenu(
actionButton("Logout", "Logout", icon = icon("sign-out")),
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
))
ui3_main<- list(
tabItems(
tabItem("dashboard",
tabsetPanel(
tabPanel( title = "Just an Info",
br(),
fluidRow(
box(
h3("General Status", align="center"))
)
)))))
dataframe
Username Password Role
u1 u10 admin
u2 u20 user
u3 u30 admin
u5 u50 user
在我的代码中,我对username
,password
和role
进行了硬编码,而不是如何使它从数据框中检索