chat_ui | R Documentation |
Inserts a chat UI element into a Shiny UI, which includes a scrollable section for displaying chat messages, and an input field for the user to enter new messages.
To respond to user input, listen for input$ID_user_input
(for example, if
id="my_chat"
, user input will be at input$my_chat_user_input
), and use
chat_append()
to append messages to the chat.
chat_ui(
id,
...,
messages = NULL,
placeholder = "Enter a message...",
width = "min(680px, 100%)",
height = "auto",
fill = TRUE
)
id |
The ID of the chat element |
... |
Extra HTML attributes to include on the chat element |
messages |
A list of messages to prepopulate the chat with. Each message can be one of the following:
|
placeholder |
The placeholder text for the chat's user input field |
width |
The CSS width of the chat element |
height |
The CSS height of the chat element |
fill |
Whether the chat element should try to vertically fill its container, if the container is fillable |
A Shiny tag object, suitable for inclusion in a Shiny UI
library(shiny)
library(bslib)
library(shinychat)
ui <- page_fillable(
chat_ui("chat", fill = TRUE)
)
server <- function(input, output, session) {
observeEvent(input$chat_user_input, {
# In a real app, this would call out to a chat model or API,
# perhaps using the 'ellmer' package.
response <- paste0(
"You said:\n\n",
"<blockquote>",
htmltools::htmlEscape(input$chat_user_input),
"</blockquote>"
)
chat_append("chat", response)
})
}
shinyApp(ui, server)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.