Created
July 18, 2017 14:02
-
-
Save jobonaf/f1a589fd140a698e2731a23264a64b3e to your computer and use it in GitHub Desktop.
R Shiny application protected by a password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list = ls()) | |
library(shiny) | |
Logged = FALSE; | |
## put here the credentials | |
my_username <- "test" | |
my_password <- "test" | |
## part of the user interface for login | |
ui1 <- function(){ | |
tagList( | |
div(id = "login", | |
wellPanel(textInput("userName", "Username"), | |
passwordInput("passwd", "Password"), | |
br(),actionButton("Login", "Log in"))), | |
tags$style(type="text/css", "#login {font-size:10px; text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -100px;margin-left: -150px;}") | |
)} | |
## part of user interface after login | |
ui2 <- function(){ | |
##----------------------------------- | |
## write what you need, from this line... | |
bootstrapPage( | |
selectInput(inputId = "n_breaks", | |
label = "Number of bins in histogram (approximate):", | |
choices = c(10, 20, 35, 50), | |
selected = 20), | |
checkboxInput(inputId = "individual_obs", | |
label = strong("Show individual observations"), | |
value = FALSE), | |
checkboxInput(inputId = "density", | |
label = strong("Show density estimate"), | |
value = FALSE), | |
plotOutput(outputId = "main_plot", height = "300px"), | |
conditionalPanel(condition = "input.density == true", | |
sliderInput(inputId = "bw_adjust", | |
label = "Bandwidth adjustment:", | |
min = 0.2, max = 2, value = 1, step = 0.2) | |
) | |
) | |
## ...to this line | |
##----------------------------------- | |
} | |
ui = (htmlOutput("page")) | |
server = (function(input, output,session) { | |
## part of the server managing the login | |
USER <- reactiveValues(Logged = Logged) | |
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(my_username == Username) | |
Id.password <- which(my_password == Password) | |
if (length(Id.username) > 0 & length(Id.password) > 0) { | |
if (Id.username == Id.password) { | |
USER$Logged <- TRUE | |
} | |
} | |
} | |
} | |
} | |
}) | |
observe({ | |
if (USER$Logged == FALSE) { | |
output$page <- renderUI({ | |
div(class="outer",do.call(bootstrapPage,c("",ui1()))) | |
}) | |
} | |
if (USER$Logged == TRUE) | |
{ | |
output$page <- renderUI({ | |
ui2() | |
}) | |
print(ui) | |
} | |
}) | |
## part of the server interacting with user interface after the login | |
##----------------------------------- | |
## write what you need, from this line... | |
output$main_plot <- renderPlot({ | |
hist(faithful$eruptions, | |
probability = TRUE, | |
breaks = as.numeric(input$n_breaks), | |
xlab = "Duration (minutes)", | |
main = "Geyser eruption duration") | |
if (input$individual_obs) { | |
rug(faithful$eruptions) | |
} | |
if (input$density) { | |
dens <- density(faithful$eruptions, | |
adjust = input$bw_adjust) | |
lines(dens, col = "blue") | |
} | |
}) | |
## ...to this line | |
##----------------------------------- | |
}) | |
runApp(list(ui = ui, server = server)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, that was really helpful!