Search code examples
rshinytagsshinydashboard

shinydashboard error message "Expected an object with class 'shiny.tag"


I am trying to create a dynamic notification on header on a shinyapp, and get an error message as Warning: Error in FUN: Expected an object with class 'shiny.tag'. The code is as below,

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "test",
                  dropdownMenuOutput("menu")
  ),
  dashboardSidebar()
,
dashboardBody()
)

server <- function(input, output, session) {
  
  output$menu <- renderMenu({
    items <- if(Sys.Date()>as.Date("2022-06-30")){
      "June is over"
    }else(
      "June"
    )
    
    dropdownMenu(
      type = "notifications", 
      .list = items
    )
  })

}

shinyApp(ui, server)

Couldn't figure out how to fix it. Any help?


Solution

    1. you can write if else in one line
    2. to use .list argument, you need create your items in a list.
    3. each item in the list needs to be notificationItem or messageItem. Read this: https://rstudio.github.io/shinydashboard/structure.html

    Here is an example on how to fix:

    library(shiny)
    library(shinydashboard)
    
    ui <- dashboardPage(
        dashboardHeader(title = "test",
                        dropdownMenuOutput("menu")
        ),
        dashboardSidebar()
        ,
        dashboardBody()
    )
    
    server <- function(input, output, session) {
        
        output$menu <- renderMenu({
            items <- list(
                notificationItem(if(Sys.Date()>as.Date("2022-06-30"))"June is over" else "June"),
                notificationItem("123")
            )
            dropdownMenu(
                type = "notifications", 
                .list = items
            )
        })
        
    }
    
    shinyApp(ui, server)
    

    enter image description here