PassWord on a Shiny App

Hello, I'm French and I've recently begin to learn RShiny.
For my intership I had to create a Shiny App about numbers of 23 maternities and I want that each maternity should find their proper results by searching his number on a list. I know aproximatly how to do that. However, each maternity is not allowed to see the result of another maternity. So I want to create a password box, thank to that each maternity will be able to enter his proper password and find their results. Is it possible with a Shiny app ? If not, how can I do that ?

(P.S : sorry if I did mistakes)
Thanks for your help !

Here is a very primitive version of displaying different data depending on the user name and password entered. I used the bcrypt package to generate hashed passwords and to check the password validity. In a real version, I would probably use conditionalPanel() to show the plot, rather than what I did here. I hope it gives you an idea of how to proceed.

library(bcrypt)
library(shiny)
library(tibble)
#In the real app, prepare passwords beforehand and store them in a file or data base
PasswordDF <- data.frame(username = c("alpha", "beta", "gamma"),
                         HashedPassword = c(hashpw("artichoke"), hashpw("crabapple"), hashpw("eggplant")),
                         group = c("A", "B", "C"),
                         stringsAsFactors = FALSE
                         )

Dat <- tibble(group = c(rep("A", 3), rep("B", 3), rep("C", 3)),
              x = rep(1:3, 3),
              y = c(2,3,4,14,13,12,23,22,24))

ui <- fluidPage(
  
  titlePanel("Mock App"),
  fluidRow(
    column(width = 2,
           textInput("user", "User Name") 
    ),
    column(width = 2,
           passwordInput("PW", "Password") 
    ),
    column(width = 2,
           actionButton("button", "Log in") 
    )
  ),
  fluidRow(
    plotOutput("plot")
  )
)

server <- function(input, output) {
  
  filteredDat <- eventReactive(input$button, {
    if (input$user %in% PasswordDF$username && 
        checkpw(input$PW, 
                hash = PasswordDF[PasswordDF$username == input$user, "HashedPassword"])) {
      Dat %>% filter(group == PasswordDF[PasswordDF$username == input$user, "group"])
    } else data.frame(x = 0, y = 0)
  })
  output$plot <- renderPlot({ plot(filteredDat()$x, filteredDat()$y, main = isolate(input$user), type = "b") })
}

# Run the application 
shinyApp(ui = ui, server = server)```

You can also be interested in these solutions

  • With helper packages

https://appsilon.com/user-authentication-in-r-shiny-sneak-peek-of-shiny-users-and-shiny-admin-packages/

  • using various deployment solutions

https://ipub.com/shiny-password-protect/

  • Using a third party service

Also know that you can have authentication using pro product from RStudio where you can deploy your application. Users will have to authenticate and you'll be able to check in your app who is running your app and select what one could see or not.
Example here with shinyserver Pro
https://shiny.rstudio.com/gallery/authentication-and-database.html

It would be similar with rstudio connect.

Thank you for your response. I try them as soon as possible. If I have any problem I will return here to ask for solutions.

Another package option you could try!

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.