This package provides a reusable chat module for R Shiny apps. The module allows multiple users to connect to a common chat and send messages to each other in real-time.
The messages can either be stored in a database or a rds data. Here is an example of the UI:To use the chat module in your own Shiny app, follow these steps:
library("shinyChatR")
The details of the different steps can be found below:
If you are using a database connection to store the chat messages,
you will need to initialize the database table before using the chat
module. The following example shows an example how to do this using the
DBI
and RSQLite
packages. Replace
db_file
with the path to your database file. The data will
be saved in the table chat_data
.
library(DBI)
library(RSQLite)
<- "path_to_your_database_file"
db_file <- dbConnect(RSQLite::SQLite(), db_file)
conn
# initiate chat table
<- data.frame(rowid = numeric(),
df user = character(),
text = character(),
time = double())
dbWriteTable(conn, "chat_data", df)
Now you can add the chat module to your app:
library(shinyChatR)
<- fluidPage(
ui chat_ui("test")
)
<- function(input, output, server) {
server chat_server("test", db_connection = conn,
db_table_name = "chat_data",
chat_user = "user1")
}
# Run the application
shinyApp(ui = ui, server = server)
A similar approach is required for rds data.
<- data.frame(rowid = numeric(),
df user = character(),
text = character(),
time = double())
#
<- "path_to_rds_file.rds"
test_rds saveRDS(df, test_rds)
Now you can add the chat module to your app:
library(shinyChatR)
<- "path_to_rds_file.rds"
test_rds
<- fluidPage(
ui chat_ui("test2")
)
<- function(input, output, server) {
server chat_server("test2",
rds_path = test_rds,
chat_user = "user2")
}
# Run the application
shinyApp(ui = ui, server = server)
Install from CRAN with
install.packages("shinyChatR")
You can install the development version of shinyChatR from GitHub with:
::install_github("julianschmocker/shinyChatR") remotes