Skip to content

Commit abb3582

Browse files
committed
reactive plots
1 parent c91fb35 commit abb3582

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

app.R

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
library(shiny)
22
library(bslib)
3+
library(htmltools)
34
library(markdown)
4-
library(shinychat)
5-
library(mapgl)
6-
library(dplyr)
7-
library(ggplot2)
8-
library(duckdbfs)
95
library(fontawesome)
106
library(bsicons)
117
library(gt)
12-
library(htmltools)
8+
library(glue)
9+
library(ggplot2)
10+
11+
library(mapgl)
12+
library(dplyr)
13+
library(duckdbfs)
1314

1415
duckdbfs::load_spatial()
1516

@@ -86,10 +87,12 @@ ui <- page_sidebar(
8687

8788

8889

89-
repo <- ""
90-
pmtiles <- ""
91-
parquet <- "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.parquet"
92-
svi <- open_dataset(parquet, tblname = "svi")
90+
repo <- "https://data.source.coop/cboettig/social-vulnerability"
91+
pmtiles <- glue("{repo}/svi2020_us_tract.pmtiles")
92+
parquet <- glue("{repo}/svi2020_us_tract.parquet")
93+
svi <- open_dataset(parquet, tblname = "svi") |>
94+
filter(RPL_THEMES > 0)
95+
9396

9497
con <- duckdbfs::cached_connection()
9598
schema <- DBI::dbGetQuery(con, "PRAGMA table_info(svi)")
@@ -100,7 +103,10 @@ Your task is to translate the users question into a SQL query that will be run
100103
against the "svi" table in a duckdb database. The duckdb database has a
101104
spatial extension which understands PostGIS operations as well.
102105
Include semantically meaningful columns like COUNTY and STATE name.
103-
106+
107+
In the data, each row represents an individual census tract. If asked for
108+
county or state level statistics, be sure to aggregate across all the tracts
109+
in that county or state.
104110
105111
The table schema is <schema>
106112
@@ -134,9 +140,20 @@ filter_column <- function(full_data, filtered_data, id_col = "FIPS") {
134140
list("in", list("get", id_col), list("literal", values))
135141
}
136142

143+
chart1_data <- svi |>
144+
group_by(COUNTY) |>
145+
summarise(mean_svi = mean(RPL_THEMES)) |>
146+
collect()
147+
148+
chart1 <- chart1_data |>
149+
ggplot(aes(mean_svi)) + geom_density(fill="darkred") +
150+
ggtitle("County-level vulnerability nation-wide")
151+
152+
137153
# Define the server
138154
server <- function(input, output, session) {
139155
data <- reactiveValues(df = tibble())
156+
output$chart1 <- renderPlot(chart1)
140157

141158
observeEvent(input$user_msg, {
142159
stream <- chat$chat(input$chat)
@@ -156,14 +173,27 @@ server <- function(input, output, session) {
156173
df <- df |> select(-any_of("Shape"))
157174
output$table <- render_gt(df, height = 300)
158175

176+
177+
y_axis <- colnames(df)[!colnames(df) %in% colnames(svi)]
178+
chart2 <- df |>
179+
rename(social_vulnerability = y_axis) |>
180+
ggplot(aes(social_vulnerability)) +
181+
geom_density(fill="darkred") +
182+
xlim(c(0,1)) +
183+
ggtitle("Vulnerability of selected areas")
184+
185+
output$chart2 <- renderPlot(chart2)
186+
159187
# We need to somehow trigger this df to update the map.
160188
data$df <- df
161189

162190
})
163191

192+
193+
164194
output$map <- renderMaplibre({
165-
m <- maplibre(center = c(-92.9, 41.3), zoom = 3, height = "400")
166195

196+
m <- maplibre(center = c(-92.9, 41.3), zoom = 3, height = "400")
167197
if (input$redlines) {
168198
m <- m |>
169199
add_fill_layer(
@@ -200,7 +230,7 @@ server <- function(input, output, session) {
200230
add_fill_layer(
201231
id = "svi_layer",
202232
source = list(type = "vector",
203-
url = paste0("pmtiles://", "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract.pmtiles")),
233+
url = paste0("pmtiles://", pmtiles)),
204234
source_layer = "SVI2000_US_tract",
205235
filter = filter_column(svi, data$df, "FIPS"),
206236
fill_opacity = 0.5,
@@ -213,16 +243,6 @@ server <- function(input, output, session) {
213243
m})
214244

215245

216-
chart1 <-
217-
svi |>
218-
filter(RPL_THEMES > 0) |>
219-
group_by(COUNTY) |>
220-
summarise(mean_svi = mean(RPL_THEMES)) |>
221-
collect() |>
222-
ggplot(aes(mean_svi)) + geom_density()
223-
224-
output$chart1 <- renderPlot(chart1)
225-
output$chart2 <- renderPlot(chart1)
226246

227247
}
228248

0 commit comments

Comments
 (0)