0

I have three RShiny files:

ui.R, where I define two tabs. In the first one is sliderInput with graph and another is empty. Also here I define login module.

shinyUI(bootstrapPage(
  tagList(
    tabsetPanel(
      tabPanel("A Lot of Info in Tab1",
               column(2, sliderInput("obs", "Number of observations:", 
                                     min = 10000, max = 90000, 
                                     value = 50000, step = 10000)),
               column(10, plotOutput("distPlot"))
               ),
      tabPanel("A Lot of Info in Tab2")
    )
  ),


  ## Login module;
  div(class = "login",
      uiOutput("uiLogin"),
      textOutput("pass")
  )))

server.R, where I define that login and password is "test". Also here I load file Login.R, which is defined below.

library(shiny)
library(datasets)
Logged = FALSE;
PASSWORD <- data.frame(Brukernavn = "test", Passord = "test")
shinyServer(function(input, output) {
  source("www/Login.R",  local = TRUE)

  observe({
    if (USER$Logged == TRUE) {
      output$distPlot <- renderPlot({
        dist <- NULL
        dist <- rnorm(input$obs)
        hist(dist, breaks = 100, main = "")
      })
    }
  })
})

and Login.R in which are defined all sets of rules, needed to successfully log in.

#### Log in module ###
USER <- reactiveValues(Logged = Logged)

passwdInput <- function(inputId, label) {
  tagList(
    tags$label(label),
    tags$input(id = inputId, type="password", value="")
  )
}

output$uiLogin <- renderUI({
  if (USER$Logged == FALSE) {
    wellPanel(
      textInput("userName", "User Name:"),
      passwdInput("passwd", "Pass word:"),
      br(),
      actionButton("Login", "Log in")
    )
  }
})

output$pass <- renderText({  
  if (USER$Logged == FALSE) {
    if (!is.null(input$Login)) {
      if (input$Login > 0) {
        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
          } 
        } else  {
          "User name or password failed!"
        }
      } 
    }
  }
})

The main question would be, how can I hide tabs while I'm not logged in? As for now, while user is not logged in, he can't see only results, but can see all information in tabs, move sliders and so on. This code is just a small example, in reality, I have a more complicated situation.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
neringab
  • 613
  • 1
  • 7
  • 16

0 Answers0