Search code examples
rshinyshinydashboard

How to put list into dropdownMenu?


I want to make a list of notifications and put it in a dropdown list programatically. dropdownMenu has a .list argument which is supposed to serve such mechanism. I created a list and try to put it in this dropdown but error shows up. Is the below the proper way of doing it? Example is taken from r bs4Dash documentary for dropdownMenu, I just added a list:

if (interactive()) {
   library(shiny)
   library(bs4Dash)
   library(dplyr)
   
   shinyApp(
      ui = dashboardPage(
         header = dashboardHeader(
            
            rightUi =  tagList(
               dropdownMenu(
                  type = "messages",
                  badgeStatus = "danger",
                  icon = icon("comments"),
                  .list = list(verbatimTextOutput(outputId = "listIris"))
                  ),         
               
               dropdownMenu(
                  type = "messages",
                  badgeStatus = "danger",
                  icon = icon("comments"),
                  messageItem("Message from driver Adrian Baker",
                                       "Customer closed",
                                        time = "5 mins ago"),
                  messageItem("Message from driver Louis Plitz",
                                       "Huge traffic, I'll be late",
                                        time = "8 mins ago")))
            
         ),
         
         sidebar = dashboardSidebar(disable = TRUE),
         controlbar = dashboardControlbar(),
         footer = dashboardFooter(),
         title = "dropdownMenu",
         body = dashboardBody()
      ),
      
          
  server = function(input, output) {
     
         observeEvent(input$triggerAction1, {
            showModal(modalDialog(
               title = "Important message",
               "This is an important message!"
            ))
         })
         
         # df with unique text values
         df <- iris %>% 
            select(Species) %>% 
            group_by(Species) %>% 
            unique()
         
         # create a list
         uniqueList <- as.list(df)
         
         output$listIris <- renderPrint({
            uniqueList
         })          
      }
   )
}

There are 2 types of errors:

Error in writeImpl: Text to be written must be a length-one character vector

and

Error in cat: argument 1 (type 'list')..

EDIT: I changed above code based on @Stephane's solution. I can now generate list now but not in a proper form. When you run the above code then you will see two dropdowns (in the right side of header): first - generated from .list, second - generated manually (using messageItem). You can compare those two dropdowns - my goal is to generate list programatically but in the pretty form of the second dropdown. How can I put list elements into messageItem programatically? And also how to add unique inputId to each messageItem to make it behave as actionButton (as per documentation which says it's possible)?


Solution

    • the "id" argument of verbatimTextOutput is outputId: try verbatimTextOutput(outputId = "listIris")

    • according to the doc, the .list argument of dropdownMenu must be a list: try .list = list(verbatimTextOutput(outputId = "listIris"))

    • renderText is used to render a character string and here you have a list; use renderPrint instead: renderPrint({uniqueList}) (without print())