| Title: | R Shiny Chat Module |
|---|---|
| Description: | Provides an easy-to-use module for adding a chat to a Shiny app. Allows users to send messages and view messages from other users. Messages can be stored in a database or a .rds file. |
| Authors: | Julian Schmocker [aut, cre, cph], Ivo Kwee [aut] |
| Maintainer: | Julian Schmocker <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.2.0 |
| Built: | 2026-05-15 06:26:42 UTC |
| Source: | https://github.com/julianschmocker/shinychatr |
Creates the server logic for the chat module, which handles adding new messages to the database or RDS file, and retrieving messages to display
chat_server( id, chat_user, db_connection = NULL, db_table_name = "chat_data", rds_path = NULL, csv_path = NULL, invalidateDSMillis = 1000, pretty = TRUE, nlast = 100 )chat_server( id, chat_user, db_connection = NULL, db_table_name = "chat_data", rds_path = NULL, csv_path = NULL, invalidateDSMillis = 1000, pretty = TRUE, nlast = 100 )
id |
The id of the module. |
chat_user |
The user name that should be displayed next to the message. |
db_connection |
A database connection object, created using the |
db_table_name |
he name of the database table to use for storing the chat messages. If |
rds_path |
The path to an RDS file to use for storing the chat messages. If provided, the chat messages will be stored in an RDS file. |
csv_path |
The path to an csv file to use for storing the chat messages. If provided, the chat messages will be stored in an csv file. |
invalidateDSMillis |
The milliseconds to wait before the data source is read again. The default is 1 second. |
pretty |
Logical that determines if the date should be displayed in a pretty format |
nlast |
The number of last messages to be read in and displayed |
the reactive values chat_rv with all the chat information
Creates the user interface for the chat module, which includes a chat message display area, a text input field for entering new messages, and a send button.
chat_ui(id, ui_title = "", height = "300px", width = "100%")chat_ui(id, ui_title = "", height = "300px", width = "100%")
id |
The id of the module |
ui_title |
The title of the chat area. |
height |
The height of the chat display area. Default is 300px. |
width |
The width of the chat display area. |
CSVConnection R6 Class
CSVConnection R6 Class
An R6 class representing a connection to a CSV file for the chat module.
The full dataset
Save a message to data source
csv_pathThe path to the CSV file.
nlastThe number of messages to be read in and displayed.
Initialize the R6 Object
new()
CSVConnection$new(csv_path, nlast = NULL)
csv_pathThe path to the csv file.
nlastThe number of messages to be read-in.
get_data()
Reads the full dataset
CSVConnection$get_data()
insert_message()
CSVConnection$insert_message(message, user, time)
messageThe message to be stores
userThe user who entered the message
timeThe time when message was submitted
clone()
The objects of this class are cloneable with this method.
CSVConnection$clone(deep = FALSE)
deepWhether to make a deep clone.
DBConnection R6 Class
DBConnection R6 Class
An R6 class representing a connection to a database for the chat module.
The full dataset
Save a message to data source
connectionA database connection object, created using a package such as RSQLite.
tableThe table that contains the chat information.
Initialize the R6 Object
new()
DBConnection$new(connection, table = "chat_data")
connectionDB connection
tableTable name
get_data()
Reads the full dataset
DBConnection$get_data()
insert_message()
DBConnection$insert_message(message, user, time)
messageThe message to be stores
userThe user who entered the message
timeThe time when message was submitted
clone()
The objects of this class are cloneable with this method.
DBConnection$clone(deep = FALSE)
deepWhether to make a deep clone.
RDSConnection R6 Class
RDSConnection R6 Class
An R6 class representing a connection to a rds file for the chat module.
The full dataset
Save a message to data source
rds_pathThe path to the rds file.
Initialize the R6 Object
new()
RDSConnection$new(rds_path)
rds_pathThe path to the rds file.
get_data()
Reads the full dataset
RDSConnection$get_data()
insert_message()
RDSConnection$insert_message(message, user, time)
messageThe message to be stores
userThe user who entered the message
timeThe time when message was submitted
clone()
The objects of this class are cloneable with this method.
RDSConnection$clone(deep = FALSE)
deepWhether to make a deep clone.
Render the messages for the chat
render_msg_divs(texts, users, act_user)render_msg_divs(texts, users, act_user)
texts |
a character vector with the texts |
users |
a character vector with the users |
act_user |
a character with the current user (that is using the app) |
The HTML code containing the chat messages
Render the messages for the chat
render_msg_divs2(texts, users, act_user, time, pretty = TRUE)render_msg_divs2(texts, users, act_user, time, pretty = TRUE)
texts |
a character vector with the texts |
users |
a character vector with the users |
act_user |
a character with the current user (that is using the app) |
time |
a datetime object |
pretty |
a logical that indicates if it should simplify the date |
The HTML code containing the chat messages
Updates the value of the chat textInput
updateChatTextInput(session = getDefaultReactiveDomain(), id, value)updateChatTextInput(session = getDefaultReactiveDomain(), id, value)
session |
The shiny session. |
id |
The id of the module. |
value |
The new value that should be shown in the chat textInput. |