library(shinyChatR)
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:
<- 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)
This example show how the user name can be stored in a reactive. Here the user specifies the name in a textInput field.
<- fluidPage(
ui
fluidRow(
column(width = 6,
# Here the user specifies the user name
textInput("user", "Enter User Name"),
br(),
# add chat ui elements
chat_ui("test_reactive"),
)
) )
Then, in the server the user name is save in the reactive user_rv
. This reactive can be passed into the argument chat_user
.
<- function(input, output, server) {
server
# save user name in reactive
<- reactive({input$user})
user_rv
# corresponding server part for id test_reactive
chat_server("test_reactive", db_connection = conn,
db_table_name = "chat_data",
chat_user = user_rv
) }
Also the rds file first needs to be initialized.
<- 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:
<- "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)