Skip to content

Instantly share code, notes, and snippets.

@Ray901
Last active May 15, 2021 00:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ray901/656f4314d00a7b00a05f to your computer and use it in GitHub Desktop.
Save Ray901/656f4314d00a7b00a05f to your computer and use it in GitHub Desktop.
R shiny app userLogin
#### Log in module ###
PASSWORD <- data.frame(
Brukernavn = c("ray","gil"),
Passord = c("0000","1234")
)
output$uiLogin <- renderUI({
if (USER$Logged == FALSE) {
wellPanel(
textInput("userName", "User Name:"),
passwordInput("passwd", "Pass word:"),
br(),
actionButton("Login", "Log in")
)
}
})
output$pass <- renderText({
if (USER$Logged == FALSE) {
USER$pass
}
})
# Login info during session ----
output$userPanel <- renderUI({
if (USER$Logged == TRUE) {
fluidRow(
column(2,
"User: ", USER$name
),
column(1, actionLink("logout", "Logout"))
)
}
})
# control login
observeEvent(input$Login , {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
Id.username <- which(PASSWORD$Brukernavn == Username)
Id.password <- which(PASSWORD$Passord == Password)
if (length(Id.username) > 0 & length(Id.password) > 0) {
if (Id.username == Id.password) {
USER$Logged <- TRUE
USER$name <- Username
}
} else {
USER$pass <- "User name or password failed!"
}
})
# control logout
observeEvent(input$logout , {
USER$Logged <- FALSE
USER$pass <- ""
})
library(shiny)
########################################################
shinyServer(function(input, output, session) {
USER <- reactiveValues(Logged = FALSE , session = session$user)
source("www/Login.R", local = TRUE)
getDat <- eventReactive(input$search,{
withProgress(
message = 'Calculation in progress',
detail = 'get iris data', value=0 , {
setSpecies <- isolate(input$selectSpecies)
incProgress(0.5)
if (!is.null(setSpecies)) {
Dat <- iris[which(iris$Species %in% setSpecies),]
} else {
Dat <- NULL
}
setProgress(1)
})
return(Dat)
})
output$obs <- renderUI({
if (USER$Logged == TRUE) {
list(
selectizeInput(
'selectSpecies', 'Select iris Species', choices = as.character(unique(iris$Species)), multiple = TRUE
),
actionButton('search', 'Search')
)
}
})
output$dataTable <- renderUI({
if (USER$Logged == TRUE) {
dataTableOutput('table')
}
})
output$table <- renderDataTable(
getDat(),
options = list(
pageLength = 100,
lengthMenu = c(50,100,200,500)
)
)
})
div.login {
text-align: left;
position:absolute;
top: 40%;
left: 50%;
margin-top: -100px;
margin-left: -150px;
}
div.logininfo {
text-align: right;
position:relative;
top: 5%;
left: 90%;
background-color: yellow;
}
shinyUI(
fluidPage(
tagList(
tags$head(
tags$link(rel="stylesheet", type="text/css",href="style.css")
)
),
div(class = "login",
uiOutput("uiLogin"),
textOutput("pass"),
tags$head(tags$style("#pass{color: red;"))
),
fluidRow(
column(3,
div(class = "span1",
uiOutput("obs")
)
),
column(8,
div(class = "logininfo",
uiOutput("userPanel")
),
hr(),
div(class = "DataTable",
uiOutput('dataTable')
)
)
)
)
)
Copy link

ghost commented Dec 14, 2016

Could this work with only having a Single File (app.R) or does it need to have them as separate files?

@dukepang
Copy link

Thanks for sharing. It is great!

@JasonTan-code
Copy link

Maybe Shinymanager can handle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment