thmschk
thmschk

Reputation: 644

How to hide navbarMenus with shinyjs?

This question delas with the combination of navbarMenus and the shinyjs package. I've build a shiny app and I also added an user-login following this suggestion: https://gist.github.com/withr/9001831.

So I know, which user is logged in. Now I'd like to show and hide both a whole navbarMenu and single tabPanels, depending on which user is logged in. Let's say, there are two users, 'admin' and 'custom', and for 'admin' all navbarMenus and all tabPanels should be shown, while 'custom' only gets navbarMenu 2 and within this menu only tabPanel 22 to see.

library(shiny)
library(shinyjs)

# Define UI
ui <-
fluidPage(
titlePanel("NAVBARPAGE"), # title
br(),
################### NAVBAR  ############################
navbarPage(
"", # title
################### TABS THEMA  #######################
hidden(div(id='m1',
navbarMenu("Menu1",
tabPanel('tab11',plotOutput('tab_11')),
tabPanel('tab12',uiOutput('tab_12'))))),
navbarMenu("Menu2",
tabPanel('tab21',uiOutput('tab_21')),
hidden(div(id='xxx',tabPanel('tab22',uiOutput('tab_22')))))
#######################################################
))

###### SERVER #####
server <- function(input, output,session) {
#Define user
user<-'admin'

observe({
if (user=='admin') {
show('xxx')
show('m1')
}
else {
hide('xxx')
hide(m1)
}
})
# Run the application
shinyApp(ui = ui, server = server)

In this minimal example, the tabPanel ('tab22') is hidden, but is does not show up, when I define

user<-'custom'

Plus, the appearance of the first navbarMenu 'm1' is strange - it is actually not hidden, it is only only empty. Does anyone know help?

Upvotes: 3

Views: 1285

Answers (1)

Geovany
Geovany

Reputation: 5677

Your code has some errors, missing } at the end of your server function, calling the hide funciton with m1 instead of "m1", and not initialize shinyjs with shinyjs::useShinyjs(). In addition, you can not use a div around navbarMenu or tabPanel, they will not be rendered. Lastly, it would be very easy for any user to show/hide the content.

A better option is to create the navbar according to the user using a uiOutput and control what to render using the user name. Below is an example based on your code to show content based on the users.

library(shiny)
library(shinyjs)
ui <- fluidPage(
  selectInput("userSL", 'Users:', c('admin', 'custom')),
  titlePanel("NAVBARPAGE"), 
  br(),
  uiOutput("navbarPageUI")
)

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

  output$navbarPageUI <- renderUI({
    user <- input$userSL
    if (user == 'admin') {
      navbarPage("", 
        navbarMenu("Menu1",
          tabPanel('tab11',plotOutput('tab_11')),
          tabPanel('tab12',uiOutput('tab_12'))
        ),
        navbarMenu("Menu2",
          tabPanel('tab21',uiOutput('tab_21')),
          tabPanel('tab22',uiOutput('tab_22'))
        )
      )
    } else {
      navbarPage("", 
        navbarMenu("Menu2",
          tabPanel('tab21',uiOutput('tab_21'))
        )
      )
    }
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions