-
Notifications
You must be signed in to change notification settings - Fork 5
/
shinyChatR.Rmd
171 lines (131 loc) · 3.79 KB
/
shinyChatR.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: "Getting started with shinyChatR"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Getting started with shinyChatR}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
```{r setup}
library(shinyChatR)
```
# Database connection for storing chat data
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`.
```{r}
library(DBI)
library(RSQLite)
db_file <- "path_to_your_database_file"
conn <- dbConnect(RSQLite::SQLite(), db_file)
# initiate chat table
df <- data.frame(rowid = numeric(),
user = character(),
text = character(),
time = double())
dbWriteTable(conn, "chat_data", df)
```
Now you can add the chat module to your app:
```{r}
ui <- fluidPage(
chat_ui("test")
)
server <- function(input, output, server) {
chat_server("test", db_connection = conn,
db_table_name = "chat_data",
chat_user = "user2")
}
# Run the application
shinyApp(ui = ui, server = server)
```
## Reactive user name
This example show how the user name can be stored in a reactive. Here the user specifies the name in a textInput field.
```{r}
ui <- fluidPage(
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`.
```{r}
server <- function(input, output, server) {
# save user name in reactive
user_rv <- reactive({input$user})
# corresponding server part for id test_reactive
chat_server("test_reactive", db_connection = conn,
db_table_name = "chat_data",
chat_user = user_rv
)
}
```
# RDS file for storing chat data
Also the rds file first needs to be initialized.
```{r}
df <- data.frame(rowid = numeric(),
user = character(),
text = character(),
time = double())
test_rds <- "path_to_rds_file.rds"
saveRDS(df, test_rds)
```
Now you can add the chat module to your app:
```{r}
test_rds <- "path_to_rds_file.rds"
ui <- fluidPage(
chat_ui("test2")
)
server <- function(input, output, server) {
chat_server("test2",
rds_path = test_rds,
chat_user = "user2")
}
# Run the application
shinyApp(ui = ui, server = server)
```
# Update the textInput
The following example shows how to use updateChatTextInput to update the textInput field from outside of the module.
```{r}
# store db as tempfile. Select different path to store db locally
tempdb <- file.path(tempdir(), "db")
con <- dbConnect(RSQLite::SQLite(), tempdb)
ui <- fluidPage(
fluidRow(
column(width = 6,
# add chat ui elements
chat_ui("test1"),
)
),
br(),
fluidRow(
column(width = 6,
# add button to update text value
actionButton("set_value_abc", "Set text to 'abc'"),
)
),
)
server <- function(input, output, server) {
# corresponding server part for id test1
chat_server("test1", db_connection = con,
db_table_name = "chat_data",
chat_user = "user1"
)
observeEvent(input$set_value_abc,{
updateChatTextInput(id = "test1",
value = "abc")
})
}
# Run the application
shinyApp(ui = ui, server = server)
```