Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg-r/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Imports:
whisker,
xtable
Suggests:
bsicons,
DT,
R6,
RSQLite,
Expand Down
1 change: 1 addition & 0 deletions pkg-r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(create_system_prompt)
export(execute_query)
export(get_db_type)
export(get_schema)
export(querychat_app)
export(querychat_data_source)
export(querychat_init)
export(querychat_server)
Expand Down
2 changes: 2 additions & 0 deletions pkg-r/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
* `querychat_server()` now uses a `shiny::ExtendedTask` for streaming the chat response, which allows the dashboard to update and remain responsive while the chat response is streaming in. (#63)

* querychat now requires `ellmer` version 0.3.0 or later and uses rich tool cards for dashboard updates and database queries. (#65)

* New `querychat_app()` function lets you quickly launch a Shiny app with a querychat chat interface. (#66)
6 changes: 5 additions & 1 deletion pkg-r/R/querychat.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ querychat_server <- function(id, querychat_config) {
chat = chat,
sql = shiny::reactive(current_query()),
title = shiny::reactive(current_title()),
df = filtered_df
df = filtered_df,
update_query = function(query, title = NULL) {
current_query(query)
current_title(title)
}
)
})
}
Expand Down
136 changes: 136 additions & 0 deletions pkg-r/R/querychat_app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#' A Simple App for Chatting with Data
#'
#' Creates a Shiny app that allows users to interact with a data source using
#' natural language queries. The app uses a pre-configured Shiny app built on
#' [querychat_sidebar()] and [querychat_server()] to provide a quick-and-easy
#' way to chat with your data.
#'
#' @examplesIf rlang::is_interactive()
#' # Pass in a data frame to start querychatting
#' querychat_app(mtcars)
#'
#' # Or choose your LLM client using ellmer::chat_*() functions
#' querychat_app(mtcars, client = ellmer::chat_anthropic())
#'
#' @param config A `querychat_config` object or a data source that can be used
#' to create one.
#' @param ... Additional arguments passed to [querychat_init()] if `config` is
#' not already a `querychat_config` object.
#' @inheritParams shinychat::chat_app
#'
#' @return Invisibly returns the `chat` object containing the chat history.
#'
#' @export
querychat_app <- function(config, ..., bookmark_store = "url") {
rlang::check_installed("DT")
rlang::check_installed("bsicons")

if (!inherits(config, "querychat_config")) {
if (inherits(config, "querychat_data_source")) {
data_source <- config
} else {
data_source <- querychat_data_source(
config,
table_name = deparse(substitute(config))
)
}
config <- querychat_init(data_source, ...)
}

ui <- function(req) {
bslib::page_sidebar(
title = shiny::HTML(paste0(
"<span>querychat with <code>",
config$data_source$table_name,
"</code></span>"
)),
sidebar = querychat_sidebar("chat"),
bslib::card(
fill = FALSE,
style = bslib::css(max_height = "33%"),
bslib::card_header(
shiny::div(
class = "hstack",
shiny::div(
bsicons::bs_icon("terminal-fill"),
shiny::textOutput("query_title", inline = TRUE)
),
shiny::div(
class = "ms-auto",
shiny::uiOutput("ui_reset", inline = TRUE)
)
)
),
shiny::uiOutput("sql_output")
),
bslib::card(
bslib::card_header(bsicons::bs_icon("table"), "Data"),
DT::DTOutput("dt")
),
shiny::actionButton(
"close_btn",
label = "",
class = "btn-close",
style = "position: fixed; top: 6px; right: 6px;"
)
)
}

chat <- NULL

server <- function(input, output, session) {
qc <- querychat_server("chat", config)
chat <<- qc$chat

output$query_title <- shiny::renderText({
if (shiny::isTruthy(qc$title())) {
qc$title()
} else {
"SQL Query"
}
})

output$ui_reset <- shiny::renderUI({
shiny::req(qc$sql())

shiny::actionButton(
"reset_query",
label = "Reset Query",
class = "btn btn-outline-danger btn-sm lh-1"
)
})

shiny::observeEvent(input$reset_query, {
qc$update_query("", NULL)
})

output$dt <- DT::renderDT({
DT::datatable(qc$df())
})

output$sql_output <- shiny::renderUI({
sql <- if (shiny::isTruthy(qc$sql())) {
qc$sql()
} else {
paste("SELECT * FROM", config$data_source$table_name)
}

sql_code <- paste(c("```sql", sql, "```"), collapse = "\n")

shinychat::output_markdown_stream(
"sql_code",
content = sql_code,
auto_scroll = FALSE,
width = "100%"
)
})

shiny::observeEvent(input$close_btn, {
shiny::stopApp()
})
}

app <- shiny::shinyApp(ui, server, ..., enableBookmarking = bookmark_store)
tryCatch(shiny::runGadget(app), interrupt = function(cnd) NULL)
invisible(chat)
}
39 changes: 39 additions & 0 deletions pkg-r/man/querychat_app.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg-r/pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ reference:
- querychat_sidebar
- querychat_system_prompt
- querychat_ui
- querychat_app
Loading