簡體   English   中英

如何更新通過 renderUI 和 uiOutput 插入的 R shiny 中的 userMessages?

[英]How do I update userMessages in R shiny which were inserted via renderUI and uiOutput?

當我通過renderUIuiOutputshinydashboard userMessagesupdateUserMessages不會更新消息。

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)

我想實現一個像這樣工作的應用程序:

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)

正如@ismirsehregal 建議的那樣,我使用insertUI ,它的工作方式與我預期的一樣:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM