1
1
library(shiny )
2
2
library(bslib )
3
+ library(htmltools )
3
4
library(markdown )
4
- library(shinychat )
5
- library(mapgl )
6
- library(dplyr )
7
- library(ggplot2 )
8
- library(duckdbfs )
9
5
library(fontawesome )
10
6
library(bsicons )
11
7
library(gt )
12
- library(htmltools )
8
+ library(glue )
9
+ library(ggplot2 )
10
+
11
+ library(mapgl )
12
+ library(dplyr )
13
+ library(duckdbfs )
13
14
14
15
duckdbfs :: load_spatial()
15
16
@@ -86,10 +87,12 @@ ui <- page_sidebar(
86
87
87
88
88
89
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
+
93
96
94
97
con <- duckdbfs :: cached_connection()
95
98
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
100
103
against the "svi" table in a duckdb database. The duckdb database has a
101
104
spatial extension which understands PostGIS operations as well.
102
105
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.
104
110
105
111
The table schema is <schema>
106
112
@@ -134,9 +140,20 @@ filter_column <- function(full_data, filtered_data, id_col = "FIPS") {
134
140
list (" in" , list (" get" , id_col ), list (" literal" , values ))
135
141
}
136
142
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
+
137
153
# Define the server
138
154
server <- function (input , output , session ) {
139
155
data <- reactiveValues(df = tibble())
156
+ output $ chart1 <- renderPlot(chart1 )
140
157
141
158
observeEvent(input $ user_msg , {
142
159
stream <- chat $ chat(input $ chat )
@@ -156,14 +173,27 @@ server <- function(input, output, session) {
156
173
df <- df | > select(- any_of(" Shape" ))
157
174
output $ table <- render_gt(df , height = 300 )
158
175
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
+
159
187
# We need to somehow trigger this df to update the map.
160
188
data $ df <- df
161
189
162
190
})
163
191
192
+
193
+
164
194
output $ map <- renderMaplibre({
165
- m <- maplibre(center = c(- 92.9 , 41.3 ), zoom = 3 , height = " 400" )
166
195
196
+ m <- maplibre(center = c(- 92.9 , 41.3 ), zoom = 3 , height = " 400" )
167
197
if (input $ redlines ) {
168
198
m <- m | >
169
199
add_fill_layer(
@@ -200,7 +230,7 @@ server <- function(input, output, session) {
200
230
add_fill_layer(
201
231
id = " svi_layer" ,
202
232
source = list (type = " vector" ,
203
- url = paste0(" pmtiles://" , " https://data.source.coop/cboettig/social-vulnerability/svi2020_us_tract. pmtiles" )),
233
+ url = paste0(" pmtiles://" , pmtiles )),
204
234
source_layer = " SVI2000_US_tract" ,
205
235
filter = filter_column(svi , data $ df , " FIPS" ),
206
236
fill_opacity = 0.5 ,
@@ -213,16 +243,6 @@ server <- function(input, output, session) {
213
243
m })
214
244
215
245
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 )
226
246
227
247
}
228
248
0 commit comments