Skip to content

Commit

Permalink
Merge pull request PSLmodels#328 from PSLmodels/pr-parameterize-exami…
Browse files Browse the repository at this point in the history
…ne-reports

PR parameterize examine reports
  • Loading branch information
donboyd5 authored Dec 30, 2024
2 parents 96930d1 + f434ce4 commit 9a5d729
Show file tree
Hide file tree
Showing 30 changed files with 264 additions and 2,872 deletions.
3 changes: 2 additions & 1 deletion tmd/areas/weights/examine/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# folders to ignore
.Rproj.user/
/.quarto/
_examine/
_cd/
_state/
_freeze/
site_libs/

Expand Down
12 changes: 9 additions & 3 deletions tmd/areas/weights/examine/R/functions_constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# functions_constants.R
get_constants <- function(area_type) {
# Validate input
valid_area_types <- c("state", "cd", "test")
valid_area_types <- c("state", "cd")
if (!area_type %in% valid_area_types) {
stop("area_type must be one of: ", paste(valid_area_types, collapse = ", "))
}
Expand All @@ -36,14 +36,20 @@ get_constants <- function(area_type) {
WEIGHTS_DIR = "/mnt/g/.shortcut-targets-by-id/1pEdofaxeQgEeDLM8NOpo0vOGL1jT8Qa1/AFPI_2024/Phase 6/states/",
RAW_DIR = fs::path(constants$TMDAREAS, "targets", "prepare", "prepare_states", "data", "data_raw"),
TARGETS_DIR = fs::path(constants$TMDAREAS, "targets", "prepare", "prepare_states", "data", "intermediate"),
OUTPUT_DIR = here::here("data_state")
OUTPUT_DIR = here::here("data_state"),
LONG_NAME = "states",
AREAS = c("AK", "MN", "NJ", "NM", "VA", "SC") |>
stringr::str_to_lower()
),
"cd" = list(
WEIGHTS_DIR = "/mnt/g/.shortcut-targets-by-id/1pEdofaxeQgEeDLM8NOpo0vOGL1jT8Qa1/AFPI_2024/Phase 6/cds/",
RAW_DIR = fs::path(constants$TMDAREAS, "targets", "prepare", "prepare_cds", "data", "data_raw"),
TARGETS_DIR = fs::path(constants$TMDAREAS, "targets", "prepare", "prepare_cds", "data", "intermediate"),
OUTPUT_DIR = here::here("data_cd"),
SESSION = 118
SESSION = 118,
LONG_NAME = "Congressional Districts",
AREAS = c("AK00", "DE00", "ID01", "ID02", "ME02", "MT00", "ND00", "PA08", "SD00", "WY00") |>
stringr::str_to_lower()
)
)

Expand Down
File renamed without changes.
61 changes: 61 additions & 0 deletions tmd/areas/weights/examine/R/render_all.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Create one local web site with examination results for each area type (state,
# cd, or both) passed to the function render_area_reports(). The most efficient
# and effective way to do this appears to be to create a single site (in the
# _site folder) and then copy it to an appropriately named folder.

# Optionally deploy the web site to netlify.

# To run, source this file and then run render_area_reports() from the console.

library(quarto)
library(fs)
library(stringr)
library(tidyverse)

generate_quarto_yaml <- function(book_title) {
template <- readLines("_quarto_template.yml")
rendered <- stringr::str_replace(template, coll("{{book_title}}"), book_title)
writeLines(rendered, "_quarto.yml")
}

render_area_reports <- function(area_types = c("state", "cd"), eval_data = TRUE, deploy = FALSE) {

for(area_type in area_types) {
print(paste0("Area type: ", area_type))

output_dir <- paste0("_", area_type)
fs::dir_create(output_dir, recurse = TRUE)

if(area_type == "state") {suffix <- "states"} else
if(area_type == "cd") {suffix <- "Congressional Districts"}

book_title <- paste0("Examination report for ", suffix)
generate_quarto_yaml(book_title)

# Render entire project with parameters
quarto::quarto_render(
as_job = FALSE, # Ensures synchronous execution
execute_params = list(
area_type = area_type,
eval_data = eval_data
)
)

# Move all generated files
fs::dir_copy("_site", output_dir, overwrite = TRUE)
fs::dir_delete("_site")

# Conditionally deploy to Netlify
if(deploy){
siteid <- case_when(area_type=="state" ~ "4842eca7-3a3b-4183-8b73-5635ad95101d",
area_type == "cd" ~ "573ad544-144b-4535-88cb-f2c41792fe84",
.default = "ERROR")
system2("netlify",
args = c("deploy",
"--prod",
paste0("--dir=", output_dir),
paste0(" --site=", siteid)))
}
}
}

22 changes: 0 additions & 22 deletions tmd/areas/weights/examine/_area-template.qmd

This file was deleted.

4 changes: 3 additions & 1 deletion tmd/areas/weights/examine/_area-template_dt.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
areatab <- compfile |>
filter(area == "{{area}}") |>
filter(!((varname=="iitax") & (fstatus > 0 | count ==2 | agistub > 0))) |>
select(-basesoivname) |>
select(-c(adiff, apdiff, target_used)) |>
mutate(across(c(sort, scope, count, fstatus, varname, agistub, agilabel),
\(x) as.factor(x)),
across(c(target, wtdsum, diff),
Expand All @@ -20,7 +22,7 @@ areatab <- compfile |>
autoWidth = TRUE),
filter="top",
escape = FALSE) |>
formatCurrency(columns = c("target", "wtdsum", "diff"), currency="", digits=1) |>
formatCurrency(columns = c("target", "wtdsum", "diff"), currency="", digits=1) |>
formatPercentage(columns = c("pdiff"), digits = 1)
areatab
Expand Down
67 changes: 16 additions & 51 deletions tmd/areas/weights/examine/_quarto.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,25 @@
project:
type: book
output-dir: _examine

# https://prerelease.quarto.org/ # quarto documentation at this link

# publishing with netllify cli:
# open terminal in examine
# quarto render && netlify deploy --prod --dir=_examine

execute:
eval: true
echo: true
output: true
freeze: false # auto: during global project renders, re-render only when source changes
output-dir: _site

# netlify deploy --prod --dir=_state for https://tmd-examine-state.netlify.app/
# netlify deploy --prod --dir=_cd for https://tmd-examine-cds.netlify.app/

book:
title: "Examine results using area weights"
subtitle: "Create csv file"
# author: "Don Boyd"
date: today
date-format: long
chapters:
- index.qmd
- part: "Examine Congressional Districts"
chapters:
- cd_data_preparation.qmd
- cd_summary_analysis.qmd
- cd_individual_reports.qmd
- part: "Examine states"
chapters:
- state_data_preparation.qmd
- state_summary_analysis.qmd
- state_individual_reports.qmd
# netlify deploy --prod --dir=_state --site=4842eca7-3a3b-4183-8b73-5635ad95101d
# netlify deploy --prod --dir=_cd --site=573ad544-144b-4535-88cb-f2c41792fe84

format:
html:
theme: cosmo
code-fold: true

editor_options:
chunk_output_type: console
code-fold: true

# R packages using old 209 libxml
# gt,


# rendering commands
# quarto render
# quarto publish netlify --no-prompt --no-render --no-browser

# possibly use this at start of each doc
# ---
# output: html_document
# editor_options:
# chunk_output_type: console
# ---
book:
title: "Examination report for Congressional Districts"
date: now
date-format: "MMM D, YYYY hh:mm a"
chapters:
- index.qmd
- data_preparation.qmd
- summary_analysis.qmd
- individual_reports.qmd



25 changes: 25 additions & 0 deletions tmd/areas/weights/examine/_quarto_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
project:
type: book
output-dir: _site

# netlify deploy --prod --dir=_state for https://tmd-examine-state.netlify.app/
# netlify deploy --prod --dir=_cd for https://tmd-examine-cds.netlify.app/

# netlify deploy --prod --dir=_state --site=4842eca7-3a3b-4183-8b73-5635ad95101d
# netlify deploy --prod --dir=_cd --site=573ad544-144b-4535-88cb-f2c41792fe84

format:
html:
theme: cosmo
code-fold: true

book:
title: "{{book_title}}"
date: now
date-format: "MMM D, YYYY hh:mm a"
chapters:
- index.qmd
- data_preparation.qmd
- summary_analysis.qmd
- individual_reports.qmd

Loading

0 comments on commit 9a5d729

Please sign in to comment.