1

When I insert a userMessages-UI via renderUI and uiOutput into a shinydashboard then updateUserMessages doesn't update the messages.

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- fluidPage(
    dashboardPage(
        dashboardHeader(),
        dashboardSidebar(),
        dashboardBody(
            fluidRow(
                uiOutput("user_messages"),
                textInput("message_text",label="", value = ""),
                actionButton("message_send", "Send")
            )
        )
    )
)

server <- function(input, output, session) {
    
    output$user_messages <- renderUI({
        userMessages(
            width = 6,
            status = "danger",
            id = "user_messages",
            userMessage(
                author = "David",
                date = "20 Jan 2:00 pm",
                image = "",
                type = "received",
                "Message text"
            )
        )
    })
    
    observeEvent(input$message_send, {
        print("Button clicked")
        shinydashboardPlus::updateUserMessages(
            "user_messages",
            action = "add",
            content = list(
                author = "David",
                date = "Now",
                image = "",
                type = "sent",
                text = input$message_text
            )
        )
    })
}

shinyApp(ui, server)

I want to achieve an app working like this:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- fluidPage(
    dashboardPage(
        dashboardHeader(),
        dashboardSidebar(),
        dashboardBody(
            fluidRow(
                userMessages(
                    width = 6,
                    status = "danger",
                    id = "user_messages",
                    userMessage(
                        author = "David",
                        date = "20 Jan 2:00 pm",
                        image = "",
                        type = "received",
                        "Message text"
                    )
                ),
                textInput("message_text",label="", value = ""),
                actionButton("message_send", "Send")
            )
        )
    )
)

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

    observeEvent(input$message_send, {
        print("Button clicked")
        shinydashboardPlus::updateUserMessages(
            "user_messages",
            action = "add",
            content = list(
                author = "David",
                date = "Now",
                image = "",
                type = "sent",
                text = input$message_text
            )
        )
    })
}

shinyApp(ui, server)
Luca_brasi
  • 89
  • 7
  • 1
    Why do you want to use `renderUI`? In general you should prefer using `*update` functions as they are faster (not re-rendering an element but updating an existing element). – ismirsehregal Mar 14 '22 at 13:39
  • Hello @ismirsehregal, I want to use renderUI because I want to add multiple userMessages on a dashboard from data. To make things less complicated I boiled my problem down to a simpler version. – Luca_brasi Mar 14 '22 at 14:24
  • @ismirsehregal I tried to use `insertUI` and it actually worked! I'm not sure if that is what you meant but your comment helped me very much, thank you! – Luca_brasi Mar 14 '22 at 14:29

1 Answers1

0

As @ismirsehregal suggested I used insertUI and it works like I expect it to do:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- fluidPage(
    dashboardPage(
        dashboardHeader(),
        dashboardSidebar(),
        dashboardBody(
            fluidRow(
                id="user_messages_row",
                textInput("message_text",label="", value = ""),
                actionButton("message_send", "Send")
            )
        )
    )
)

server <- function(input, output, session) {
    
    insertUI(
        selector = '#user_messages_row',
        where = 'afterBegin',
            userMessages(
                    width = 6,
                    status = "danger",
                    id = "user_messages",
                    userMessage(
                        author = "David",
                        date = "20 Jan 2:00 pm",
                        image = "",
                        type = "received",
                        "Message text"
                    )
                )
    )
    
    observeEvent(input$message_send, {
        print("Button clicked")
        shinydashboardPlus::updateUserMessages(
            "user_messages",
            action = "add",
            content = list(
                author = "David",
                date = "Now",
                image = "",
                type = "sent",
                text = input$message_text
            )
        )
    })
}

shinyApp(ui, server)
Luca_brasi
  • 89
  • 7