Skip to content

Commit

Permalink
button
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Jan 3, 2025
1 parent 2b73487 commit b15dfab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ FROM rocker/geospatial:latest
WORKDIR /code

RUN install2.r --error \
bsicons \
bslib \
duckdbfs \
fontawesome \
gt \
markdown \
shiny \
shinychat \
tidyverse \
duckdbfs \
markdown
tidyverse

RUN installGithub.r cboettig/mapgl tidyverse/ellmer

Expand Down
41 changes: 30 additions & 11 deletions app.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ library(shinychat)
library(mapgl)
library(tidyverse)
library(duckdbfs)
library(fontawesome)
library(bsicons)
library(gt)
duckdbfs::load_spatial()

pmtiles <- "https://data.source.coop/cboettig/us-boundaries/mappinginequality.pmtiles"

Expand All @@ -13,21 +17,30 @@ ui <- page_sidebar(

sidebar = sidebar(

textAreaInput("chat",
"Ask me a question!",
value = "Which state has the highest average social vulnerability?",
textAreaInput("chat",
span(bs_icon("robot", size = "1.5em"), "Ask me a question!"),
value = "Which county has the highest average social vulnerability?",
width = "100%",
height = 100
),

verbatimTextOutput("sql_code"),
textOutput("explanation"),
actionButton("user_msg", "Go!", icon = icon("paper-plane"), width = "50%"),


accordion(
open = FALSE,
accordion_panel("generated SQL Code",
verbatimTextOutput("sql_code"),
),
accordion_panel("Explanation",
textOutput("explanation"),
)
),
input_switch("redlines", "Redlined Areas"),
input_switch("svi", "Social Vulnerability", value = TRUE),
input_switch("richness", "Biodiversity Richness"),
input_switch("rsr", "Biodiversity Range Size Rarity"),
width = 300,
width = 350,
),
titlePanel("Demo App"),

Expand All @@ -41,8 +54,9 @@ ui <- page_sidebar(
col_widths = c(8,4)
),

tableOutput("table")
gt_output("table"),

theme = bs_theme(version = "5")
)

svi <- "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.parquet" |>
Expand All @@ -56,7 +70,10 @@ system_prompt = glue::glue('
You are a helpful agent who always replies strictly in JSON-formatted text.
Your task is to translate the users question into a SQL query that will be run
against the "svi" table in a duckdb database. The duckdb database has a
spatial extension which understands PostGIS operations as well.
spatial extension which understands PostGIS operations as well.
Be careful to limit any return to no more than 50 rows.
The table schema is <schema>
Expand All @@ -80,17 +97,19 @@ server <- function(input, output, session) {
system_prompt = system_prompt
)

observeEvent(input$chat, {
observeEvent(input$user_msg, {
stream <- chat$chat(input$chat)

chat_append("chat", stream)
response <- jsonlite::fromJSON(stream)

output$sql_code <- renderText({response$query})
output$sql_code <- renderText({stringr::str_wrap(response$query, width=40)})
output$explanation <- renderText({response$explanation})

df <- DBI::dbGetQuery(con, response$query)
output$table <- renderTable(df, striped = TRUE)

df <- df |> select(-any_of("Shape"))
output$table <- render_gt(df, height=300)

})

Expand Down

0 comments on commit b15dfab

Please sign in to comment.