-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapp.R
377 lines (307 loc) · 10.1 KB
/
app.R
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
library(shiny)
library(bslib)
library(promises)
library(fastmap)
library(duckdb)
library(DBI)
library(fontawesome)
library(reactable)
library(here)
library(plotly)
library(ggplot2)
library(ggridges)
library(dplyr)
library(elmer)
library(shinychat)
# Open the duckdb database
conn <- dbConnect(duckdb(), dbdir = here("tips.duckdb"), read_only = TRUE)
# Close the database when the app stops
onStop(\() dbDisconnect(conn))
# gpt-4o does much better than gpt-4o-mini, especially at interpreting plots
openai_model <- "gpt-4o"
# Dynamically create the system prompt, based on the real data. For an actually
# large database, you wouldn't want to retrieve all the data like this, but
# instead either hand-write the schema or write your own routine that is more
# efficient than system_prompt().
system_prompt_str <- system_prompt(dbGetQuery(conn, "SELECT * FROM tips"), "tips")
# This is the greeting that should initially appear in the sidebar when the app
# loads.
greeting <- paste(readLines(here("greeting.md")), collapse = "\n")
icon_explain <- tags$img(src = "stars.svg")
ui <- page_sidebar(
style = "background-color: rgb(248, 248, 248);",
title = "Restaurant tipping",
includeCSS(here("styles.css")),
sidebar = sidebar(
width = 400,
style = "height: 100%;",
chat_ui("chat", height = "100%", fill = TRUE)
),
useBusyIndicators(),
# 🏷️ Header
textOutput("show_title", container = h3),
verbatimTextOutput("show_query") |>
tagAppendAttributes(style = "max-height: 100px; overflow: auto;"),
# 🎯 Value boxes
layout_columns(
fill = FALSE,
value_box(
showcase = fa_i("user"),
"Total tippers",
textOutput("total_tippers", inline = TRUE)
),
value_box(
showcase = fa_i("wallet"),
"Average tips",
textOutput("average_tip", inline = TRUE)
),
value_box(
showcase = fa_i("dollar-sign"),
"Average bill",
textOutput("average_bill", inline = TRUE)
),
),
layout_columns(
style = "min-height: 450px;",
col_widths = c(6, 6, 12),
# 🔍 Data table
card(
style = "height: 500px;",
card_header("Tips data"),
reactableOutput("table", height = "100%")
),
# 📊 Scatter plot
card(
card_header(
class = "d-flex justify-content-between align-items-center",
"Total bill vs tip",
span(
actionLink(
"interpret_scatter",
icon_explain,
class = "me-3 text-decoration-none",
aria_label = "Explain scatter plot"
),
popover(
title = "Add a color variable", placement = "top",
fa_i("ellipsis"),
radioButtons(
"scatter_color",
NULL,
c("none", "sex", "smoker", "day", "time"),
inline = TRUE
)
)
)
),
plotlyOutput("scatterplot")
),
# 📊 Ridge plot
card(
card_header(
class = "d-flex justify-content-between align-items-center",
"Tip percentages",
span(
actionLink(
"interpret_ridge",
icon_explain,
class = "me-3 text-decoration-none",
aria_label = "Explain ridgeplot"
),
popover(
title = "Split ridgeplot", placement = "top",
fa_i("ellipsis"),
radioButtons(
"tip_perc_y",
"Split by",
c("sex", "smoker", "day", "time"),
"day",
inline = TRUE
)
)
)
),
plotOutput("tip_perc")
),
)
)
server <- function(input, output, session) {
# 🔄 Reactive state/computation --------------------------------------------
current_title <- reactiveVal(NULL)
current_query <- reactiveVal("")
# This object must always be passed as the `.ctx` argument to query(), so that
# tool functions can access the context they need to do their jobs; in this
# case, the database connection that query() needs.
ctx <- list(conn = conn)
# The reactive data frame. Either returns the entire dataset, or filtered by
# whatever Sidebot decided.
tips_data <- reactive({
sql <- current_query()
if (is.null(sql) || sql == "") {
sql <- "SELECT * FROM tips;"
}
dbGetQuery(conn, sql)
})
# 🏷️ Header outputs --------------------------------------------------------
output$show_title <- renderText({
current_title()
})
output$show_query <- renderText({
current_query()
})
# 🎯 Value box outputs -----------------------------------------------------
output$total_tippers <- renderText({
nrow(tips_data())
})
output$average_tip <- renderText({
x <- mean(tips_data()$tip / tips_data()$total_bill) * 100
paste0(formatC(x, format = "f", digits = 1, big.mark = ","), "%")
})
output$average_bill <- renderText({
x <- mean(tips_data()$total_bill)
paste0("$", formatC(x, format = "f", digits = 2, big.mark = ","))
})
# 🔍 Data table ------------------------------------------------------------
output$table <- renderReactable({
reactable(tips_data(),
pagination = FALSE, compact = TRUE
)
})
# 📊 Scatter plot ----------------------------------------------------------
scatterplot <- reactive({
req(nrow(tips_data()) > 0)
color <- input$scatter_color
data <- tips_data()
p <- plot_ly(data, x = ~total_bill, y = ~tip, type = "scatter", mode = "markers")
if (color != "none") {
p <- plot_ly(data,
x = ~total_bill, y = ~tip, color = as.formula(paste0("~", color)),
type = "scatter", mode = "markers"
)
}
p <- p |> add_lines(
x = ~total_bill, y = fitted(loess(tip ~ total_bill, data = data)),
line = list(color = "rgba(255, 0, 0, 0.5)"),
name = "LOESS", inherit = FALSE
)
p <- p |> layout(showlegend = FALSE)
return(p)
})
output$scatterplot <- renderPlotly({
scatterplot()
})
observeEvent(input$interpret_scatter, {
explain_plot(chat, scatterplot(), model = openai_model, .ctx = ctx)
})
# 📊 Ridge plot ------------------------------------------------------------
tip_perc <- reactive({
req(nrow(tips_data()) > 0)
df <- tips_data() |> mutate(percent = tip / total_bill)
ggplot(df, aes_string(x = "percent", y = input$tip_perc_y, fill = input$tip_perc_y)) +
geom_density_ridges(scale = 3, rel_min_height = 0.01, alpha = 0.6) +
scale_fill_viridis_d() +
theme_ridges() +
labs(x = "Percent", y = NULL, title = "Tip Percentages by Day") +
theme(legend.position = "none")
})
output$tip_perc <- renderPlot({
tip_perc()
})
observeEvent(input$interpret_ridge, {
explain_plot(chat, tip_perc(), model = openai_model, .ctx = ctx)
})
# ✨ Sidebot ✨ -------------------------------------------------------------
append_output <- function(...) {
txt <- paste0(...)
shinychat::chat_append_message(
"chat",
list(role = "assistant", content = txt),
chunk = TRUE,
operation = "append",
session = session
)
}
#' Modifies the data presented in the data dashboard, based on the given SQL
#' query, and also updates the title.
#' @param query A DuckDB SQL query; must be a SELECT statement.
#' @param title A title to display at the top of the data dashboard,
#' summarizing the intent of the SQL query.
update_dashboard <- function(query, title) {
append_output("\n```sql\n", query, "\n```\n\n")
tryCatch(
{
# Try it to see if it errors; if so, the LLM will see the error
dbGetQuery(conn, query)
},
error = function(err) {
append_output("> Error: ", conditionMessage(err), "\n\n")
stop(err)
}
)
if (!is.null(query)) {
current_query(query)
}
if (!is.null(title)) {
current_title(title)
}
}
#' Perform a SQL query on the data, and return the results as JSON.
#' @param query A DuckDB SQL query; must be a SELECT statement.
#' @return The results of the query as a JSON string.
query <- function(query) {
# Do this before query, in case it errors
append_output("\n```sql\n", query, "\n```\n\n")
tryCatch(
{
df <- dbGetQuery(conn, query)
},
error = function(e) {
append_output("> Error: ", conditionMessage(e), "\n\n")
stop(e)
}
)
tbl_html <- df_to_html(df, maxrows = 5)
append_output(tbl_html, "\n\n")
df |> jsonlite::toJSON(auto_unbox = TRUE)
}
# Preload the conversation with the system prompt. These are instructions for
# the chat model, and must not be shown to the end user.
chat <- chat_openai(model = openai_model, system_prompt = system_prompt_str)
chat$register_tool(tool(
update_dashboard,
"Modifies the data presented in the data dashboard, based on the given SQL query, and also updates the title.",
query = type_string("A DuckDB SQL query; must be a SELECT statement."),
title = type_string("A title to display at the top of the data dashboard, summarizing the intent of the SQL query.")
))
chat$register_tool(tool(
query,
"Perform a SQL query on the data, and return the results as JSON.",
query = type_string("A DuckDB SQL query; must be a SELECT statement.")
))
# Prepopulate the chat UI with a welcome message that appears to be from the
# chat model (but is actually hard-coded). This is just for the user, not for
# the chat model to see.
chat_append("chat", greeting)
# Handle user input
observeEvent(input$chat_user_input, {
# Add user message to the chat history
chat_append("chat", chat$stream_async(input$chat_user_input)) %...>% {
# print(chat)
}
})
}
df_to_html <- function(df, maxrows = 5) {
df_short <- if (nrow(df) > 10) head(df, maxrows) else df
tbl_html <- capture.output(
df_short |>
xtable::xtable() |>
print(type = "html", include.rownames = FALSE, html.table.attributes = NULL)
) |> paste(collapse = "\n")
if (nrow(df_short) != nrow(df)) {
rows_notice <- glue::glue("\n\n(Showing only the first {maxrows} rows out of {nrow(df)}.)\n")
} else {
rows_notice <- ""
}
paste0(tbl_html, "\n", rows_notice)
}
shinyApp(ui, server)