0

I have a list of menu items in my sidebar and I want to hide some of them with a token. I have figured out how to make a place to input the token and set the condition to show the menu item, but I am missing something in the server side and I can't quite figure it out. Whenever I enter the token it authenticates but it doesn't show me the conditional menu item.

This is the code:

#Define UI --------------------
ui <- dashboardPage(
dashboardHeader(title=  "Health Portal"),
              titleWidth = 400),
dashboardSidebar(id = "Directory",
               sidebarMenu(
                 menuItem("Home", tabName = "HME"),
                 conditionalPanel(
                   condition = "input.token === 'token1'",
                   menuItem("Data", tabName = "surv")),
                 menuItem("Health & Prevention", tabName = "SHP"),
                 menuItem("Local Health Jurisdictions", tabName = "LHJs",
                 menuItem("Outbreak", tabName = "Outbreak"),
                 textInput("access_token", "Access Token:"),
                 actionButton("authenticate", "Authenticate")
               )),

#I have a few more menu items but they are not conditional or anything. I then have page     contents, but I don't think that is relevant to the issue. 

#Define Server Logic ----
server <- function(input, output, session){

access_tokens <- c("token1")

observeEvent(input$authenticate, {
if(input$access_token %in% access_tokens) {
  updateTextInput(session, "access_token", value = "")
  updateActionButton(session, "authenticate", label = "Authenticated", disabled = TRUE)
  updateCheckboxInput(session, "token", value = input$access_token)
} else {
  showModal(modalDialog(
    title = "Authentication Failed",
    "Invalid access token. Please try again.",
    easyClose = TRUE
  ))
}
})
}



#Deploy App ----
shinyApp(ui = ui, server = server)`

I am just missing something, I think in the server logic. But I can't quite figure it out.

2
  • 1
    It's much easier to help you if you provide code that's syntactically correct and runnable if we copy/paste it. It's good to keep it minimal but make sure it actually runs.
    – MrFlick
    Commented Jun 6 at 23:13
  • I’ll fix and amend when I get some time later this evening. Appreciate the tip.
    – XaiXai
    Commented Jun 7 at 0:01

1 Answer 1

0

Your conditionalPanel-approach won't work in a clean way, as it wraps the menuItem (li-tag) in a div-tag:

enter image description here

shinydashboard::renderMenu is intended for dynamic sidebar content:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  title =  "Health Portal",
  dashboardHeader(titleWidth = 400),
  dashboardSidebar(
    id = "Directory",
    sidebarMenu(
      menuItem("Home", tabName = "HME"),
      # menuItemOutput("DataProxy"),
      conditionalPanel(condition = "input.access_token === 'token1'", menuItem("Data", tabName = "surv")),
      menuItem("Health & Prevention", tabName = "SHP"),
      menuItem(
        "Local Health Jurisdictions",
        tabName = "LHJs",
        menuItem("Outbreak", tabName = "Outbreak"),
        textInput("access_token", "Access Token:"),
        actionButton("authenticate", "Authenticate")
      )
    )
  ),
  dashboardBody()
)

server <- function(input, output, session) {
  access_tokens <- c("token1")
  
  authenticated <- reactiveVal(FALSE)
  
  observeEvent(input$authenticate, {
    if (input$access_token %in% access_tokens) {
      authenticated(TRUE)
      updateTextInput(session, "access_token", value = "")
      updateActionButton(session,
                         "authenticate",
                         label = "Authenticated",
                         disabled = TRUE)
      # updateCheckboxInput(session, "token", value = input$access_token)
    } else {
      showModal(
        modalDialog(
          title = "Authentication Failed",
          "Invalid access token. Please try again.",
          easyClose = TRUE
        )
      )
    }
  })
  
  output$DataProxy <- renderMenu({
    if(authenticated()){
      menuItem("Data", tabName = "surv") 
    }
  })
}

shinyApp(ui = ui, server = server)

Related links:

Not the answer you're looking for? Browse other questions tagged or ask your own question.