Friday, February 22, 2019

r - Adding Login to Shiny App

EDIT: I heavily edited this question, because it was not very good.



I'm trying to add a login to a shiny app, so that only users with the correct username (test) and password (test) can view the app. I'm a frequent R user, but have very limited experience with Shiny (and HTML) and I need to edit someone else's shiny app to add this login feature.



I was trying to use this example
Starting Shiny app after password input



The app I'm trying to add the login to uses dashboardPage and is fairly complicated, but I just want to know how to get this working with a bare minimum example such as this basic dashboardPage template.
https://www.rdocumentation.org/packages/shinydashboard/versions/0.6.1/topics/dashboardPage




I tried to edit the code from the other answer in the following way but it did not work.



 rm(list = ls())
library(shiny)

reports = data.frame(list.files("",full.names=FALSE))
colnames(reports) = c("Reports")

Logged = FALSE;

my_username <- "test"
my_password <- "test"

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

ui2 <- function(){ source("ui.R", local = T) }

ui = (htmlOutput("page"))



##############---------------------------------------------------------------------################


##############---------------------------------------------------------------------################
####Server function
server = (function(input, output,session) {

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({
div(class="outer",do.call(navbarPage,c(inverse=TRUE,title = "Contratulations you got in!",ui2()) ) )
})
print(ui)
}
})
})


runApp(list(ui = ui, server = server))


Where ui.R is local and is simply



 library(shiny)

ui = dashboardPage(
dashboardHeader(),

dashboardSidebar(),
dashboardBody(),
title = "Dashboard example"
)


But I get the error



Warning in file(filename, "r", encoding = encoding) :
cannot open file 'ui.R': No such file or directory



I'm just trying to figure out how to edit the server/ui parts of the template for the login so that I can apply this to an app that uses dashboardPage. I was able to get it to work with a simply histogram shiny app, but not with anything else.

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...