0

I’m working with a login function for a shiny website, where different users have access to different things (basic vs. advanced). I am trying to create a new tab, just like the one called ‘Personal Page’ where only advanced users should have access.

Separating between advanced and basic users seems to work fine, however, I’m struggling to create a new visible tab while doing so. It creates an 'extra' tab with no name (see attached image), where I want the tab to look like 'Personal Page'.

see image here

My code:

    ui <- navbarPage(title = "TBI Rehabilitation",
                 theme = "style/style.css",
                 fluid = FALSE,
                 collapsible = TRUE,
                 inverse = FALSE,

                 div(class = "login",
                     uiOutput("uiLogin"),
                     textOutput("pass"),
                     tags$head(tags$style("#pass{color: red;"))),

                 div(class = "login",
                     uiOutput("permission")),

                 div(class = "login",
                     uiOutput("body")),


                 fluidRow(
                   column(8,
                          div(class = "logininfo",
                              uiOutput("userPanel")
                          ))),

                 tabPanel("Personal page",icon=icon("spa"),
                                           h1("This works fine here. 
                                         But needs to be moved to Login function 
                                        in order to make the tab available only
                                        for the advanced user"))
)



server <- function(input, output, session) {

  USER <- reactiveValues(Logged = FALSE , session = session$user) 

  source("www/Login.R",  local = TRUE)

}


shinyApp(ui = ui, server = server) 

and the code for my login function:

PASSWORD <- data.frame(
  user = c("patient","caregiver"), 
  pw = c("0000","1234"),
  permission  = c("basic", "advanced"), 
  stringsAsFactors = F
)

output$uiLogin <- renderUI({
  if (USER$Logged == FALSE) {
    wellPanel(
      textInput("userName", "Username:"),
      passwordInput("passwd", "Password:"),
      br(),
      actionButton("Login", "Login")
    )
  }
})


output$body <- renderUI ({
  if (USER$Logged == TRUE) {
    if (PASSWORD[,"permission"][which(PASSWORD$user==input$userName)]=="advanced"){
      tabItems( 
        tabItem(
          title = "Test",class = "active",
          h2("test Test ")),
        p(strong("Test - Advanced access")))

    }
    else {
      tabItems(
        tabPanel(
          title = "Test - Basic access "))

    }
  }
})   

output$permission <- renderUI ({
  if (USER$Logged == TRUE) {
    if (PASSWORD[,"permission"][which(PASSWORD$user==input$userName)]=="advanced"){
      tabItems( 
        tabPanel("Test",tabName = "Advanced access test Page", icon=icon("user"))
      )

    }
    else {
      tabItems(
        tabPanel(
          title = "Basic access test page"))


    }
  }
})     



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$user == Username)
  Id.password <- which(PASSWORD$pw    == 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 <- ""


})

I've tried many different options (producing different errors), but at the moment I get the following error: Expected tag to have class 'tab-pane'. I get the same error if I change class = 'login' to class = 'tab-pane' for uiOutput("permission") and uiOutput("body").

Is there a simple way to display different tabs (in the header, not sidebar panel) based on the user's permission?

  • Thanks @Ben. I tried, but couldn't make that work either, sadly. – Matilde Nesheim May 09 '20 at 18:10
  • It's a little confused regarding the layout you have intended here. There are [other examples](https://stackoverflow.com/questions/48096603/shiny-start-the-app-with-hidden-tabs-with-no-delay) of adding tabs (besides my previous recommendation) to `navbarPage`. Maybe you want to start with those? – Ben May 09 '20 at 21:57
  • 1
    Thank you @Ben! It works! I ended up only using password and no username in server along with hideTab + showTab, following the link you posted. Thanks a lot :) – Matilde Nesheim May 10 '20 at 08:25

0 Answers0