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