Compare commits
17
Commits
2c7c9a6dbc
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b0a7b618a7
|
||
|
|
119aa7aa0f
|
||
|
|
f3ec052144
|
||
|
|
a2a0fce265
|
||
|
|
7bc547a995
|
||
|
|
c16e6cec19
|
||
|
|
dab960aaf1
|
||
|
|
dfcad3d0b8
|
||
|
|
0561df4221
|
||
|
|
d6e3ba2fb4
|
||
|
|
4f697e2d52
|
||
|
|
76aebe2b49
|
||
|
|
446843bc86
|
||
|
|
04a5ee958b
|
||
|
|
cd1af67a3a
|
||
|
|
496b9ed9ac
|
||
|
|
7cad98b434
|
No files matched your search
Whitespace-only changes.
@@ -2,9 +2,9 @@ name: R-CMD-check
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [master]
|
branches: [main]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [master]
|
branches: [main]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
|
|||||||
+11
-4
@@ -1,4 +1,11 @@
|
|||||||
.Rproj.user
|
.Rproj.user
|
||||||
.Rhistory
|
.Rhistory
|
||||||
.RData
|
.RData
|
||||||
.Ruserdata
|
.Ruserdata
|
||||||
|
.compass-cache/
|
||||||
|
# roborev snapshots
|
||||||
|
/.roborev/
|
||||||
|
|
||||||
|
# Test run debris
|
||||||
|
tests/testthat/Rplots.pdf
|
||||||
|
tests/testthat/_problems/
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
# roborev configuration, initialised by compass.
|
||||||
|
# Reviews are queued to a background daemon -- they never block a commit.
|
||||||
|
|
||||||
|
post_commit_review = 'commit'
|
||||||
|
# Scoped conventional commits (`chore(packaging):`) do not contain `chore:`,
|
||||||
|
# and this repo's cadence rule mandates the scope -- so both forms are listed.
|
||||||
|
excluded_commit_patterns = ['WIP', 'chore:', 'chore(', 'docs:', 'docs(', 'Merge ']
|
||||||
|
|
||||||
|
review_guidelines = '''
|
||||||
|
# --- compass:begin (generated -- edit the sources, not this) ---
|
||||||
|
- Prefer returning new values to mutating arguments in place. A function that edits
|
||||||
|
its caller's object is a bug waiting for a second caller.
|
||||||
|
- Validate at system boundaries -- user input, API responses, file contents, config.
|
||||||
|
Fail fast with a message naming the field and the file.
|
||||||
|
- Never swallow an error. Handle it or let it propagate; a bare catch that continues
|
||||||
|
is worse than a crash.
|
||||||
|
- No hardcoded secrets, tokens, or credentials, and no secrets in log output or error
|
||||||
|
messages.
|
||||||
|
- Parameterise every query. String-built SQL is a defect even when the input looks safe.
|
||||||
|
- Keep functions under roughly 50 lines and files under roughly 400. Flag nesting
|
||||||
|
deeper than four levels.
|
||||||
|
- No magic numbers or hardcoded paths -- name them as constants or read them from config.
|
||||||
|
- New behaviour needs a test. A bug fix needs a test that fails without the fix.
|
||||||
|
- Prose a person reads -- an issue title or body, a journal entry, a decision record,
|
||||||
|
the narrative on the status board -- names the action or the thing, not the shape of
|
||||||
|
the machinery. Flag "gate", "seam", "surface area", "load-bearing", "first-class",
|
||||||
|
"primitive", "blast radius". A project's own defined vocabulary is not the target.
|
||||||
|
- Use the native pipe `|>`, not magrittr `%>%`.
|
||||||
|
- snake_case for objects and functions; UPPER_SNAKE for constants. Never use `.` as a
|
||||||
|
word separator in a function name -- it collides with S3 dispatch.
|
||||||
|
- Validate arguments at the top of exported functions with `stopifnot()` or an explicit
|
||||||
|
check, and say which argument was wrong.
|
||||||
|
- Never `setDT()`, `set()`, or otherwise modify by reference a data.table the caller
|
||||||
|
still owns. `as.data.table()` copies; use it.
|
||||||
|
- Prefer `vapply()` to `sapply()` -- `sapply()` silently returns a list when the type
|
||||||
|
varies, which turns a type error into a downstream mystery.
|
||||||
|
- Use `seq_len(n)` / `seq_along(x)`, never `1:n`, which iterates backwards when n is 0.
|
||||||
|
- Compare strings with `==` only after checking for NA; use `identical()` for scalars
|
||||||
|
where NA would be wrong.
|
||||||
|
- Do not call `library()` inside package or module files; attach packages in scripts and
|
||||||
|
test helpers only.
|
||||||
|
- Namespace-qualify calls into other packages (`stats::sd`) in code that is sourced.
|
||||||
|
- Every exported function needs roxygen with `@param` for each argument (type, meaning,
|
||||||
|
and why the default is what it is) and `@return`. Add `@examples` for exported API.
|
||||||
|
- Declare dependencies in DESCRIPTION. Prefer base R or an existing dependency over
|
||||||
|
adding a new one; a package with zero hard deps is worth keeping that way.
|
||||||
|
- Signal errors with `stop()` carrying a condition class, so callers can catch the kind
|
||||||
|
rather than matching on message text.
|
||||||
|
- Keep internals internal. Export only what a user needs; an accidentally exported
|
||||||
|
helper becomes an API you have to keep.
|
||||||
|
- Tests use testthat edition 3. Each test is self-sufficient -- no reliance on state
|
||||||
|
left by an earlier test or on a fixture built elsewhere in the file.
|
||||||
|
- Prefer duplication in tests over a helper that hides what is being asserted.
|
||||||
|
- Brand colors come from `civilytics_colors`; never write a hex literal in theme, scale, logo, or table code.
|
||||||
|
- No tidyverse dependency. Imports is base R plus ggplot2, grid/gridExtra, png/jpeg, stringr, stringdist, showtext/sysfonts, jsonlite. Reject dplyr, purrr, magrittr, tibble, and data.table; use base R idioms.
|
||||||
|
- NAMESPACE is roxygen-generated. Declare imports with `@importFrom` in the roxygen block and re-run roxygen; never hand-edit NAMESPACE.
|
||||||
|
- Themes default to a transparent background (`paper_bg = FALSE`) so plots composite onto any Quarto, Reveal, or Typst background. Making an opaque background the default is a regression, not a preference.
|
||||||
|
- Theme functions thread `ink`/`paper`/`accent` into ggplot2 4.0's base-theme parameters rather than setting element colors ad hoc. ggplot2 >= 4.0 is a hard dependency, so use S7 `@` property access on plot and theme objects, not `$`.
|
||||||
|
- User-facing progress goes through `message()` so callers can suppress it. No `cat()` or `print()` in package code.
|
||||||
|
- Nothing personal or client-identifying is vendored into `inst/` -- brand assets only. Version 0.3.2 removed headshots for exactly this reason.
|
||||||
|
- The camelCase exports (`countCleanr`, `dbSafeNames`, `simpleCap`, `waldInterval`, `countDots`, `countNA`, `findDots`, `nvals`) are frozen public API; do not rename them. New functions are snake_case.
|
||||||
|
- `R/theme.R` and `R/logo.R` are long by design -- one file per surface, with dense roxygen. File length is not a finding in this package; flag a single function over roughly 80 lines instead.
|
||||||
|
# --- compass:end ---
|
||||||
|
'''
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
# AGENTS.md
|
||||||
|
|
||||||
|
`civilytics` is the house R package for Civilytics Consulting: a ggplot2 brand theme
|
||||||
|
system, curated palettes, logo composition, Quarto/Typst/Reveal templates, and
|
||||||
|
data-wrangling helpers for public-sector analysis. It is a library other projects
|
||||||
|
depend on, so a breaking change here breaks reports that are already published.
|
||||||
|
|
||||||
|
## Project tracking
|
||||||
|
|
||||||
|
This repo is managed with compass. `pm/compass.toml`
|
||||||
|
defines the workstreams; `pm/JOURNAL.md` records sessions; `pm/decisions/` holds
|
||||||
|
numbered, immutable decision records. Ask compass where things stand rather than
|
||||||
|
reading the config back.
|
||||||
|
|
||||||
|
Workstreams (the `ws/` label on every issue):
|
||||||
|
|
||||||
|
| `ws/` | covers |
|
||||||
|
|---|---|
|
||||||
|
| `theme` | `R/theme.R`, `R/colors.R`, `R/fonts.R` — themes, palettes, font loading |
|
||||||
|
| `logo` | `R/logo.R`, `R/flextable.R`, `inst/img/` — logo and branded output |
|
||||||
|
| `quarto` | `R/quarto.R`, `inst/quarto/` — HTML, PDF, Typst, Reveal templates |
|
||||||
|
| `helpers` | `R/utils.R`, `R/prop_conf.R`, `R/join_utilities.R`, `R/db.R`, `R/notifications.R` |
|
||||||
|
| `packaging` | `DESCRIPTION`, `NAMESPACE`, `Makefile`, `Dockerfile`, `.gitea/workflows/` |
|
||||||
|
|
||||||
|
## Commit cadence
|
||||||
|
|
||||||
|
One coherent unit per commit, subject line:
|
||||||
|
|
||||||
|
```
|
||||||
|
type(ws): subject (#N)
|
||||||
|
```
|
||||||
|
|
||||||
|
`type` is one of `feat`, `fix`, `refactor`, `docs`, `test`, `chore`, `perf`, `ci`.
|
||||||
|
`ws` is a workstream id from the table above. `#N` is the issue, when there is one.
|
||||||
|
|
||||||
|
## Conventions
|
||||||
|
|
||||||
|
`pm/compass.toml` is authoritative for these rules. Its `[roborev]
|
||||||
|
project_guidelines` are composed into `.roborev.toml`, so change them there and
|
||||||
|
re-run compass rather than editing `.roborev.toml` by hand. The prose below is the
|
||||||
|
same rules stated for a human reader; if the two ever disagree, `pm/compass.toml`
|
||||||
|
wins.
|
||||||
|
|
||||||
|
**Dependencies.** Base R plus ggplot2 (>= 4.0), grid/gridExtra, png/jpeg, stringr,
|
||||||
|
stringdist, showtext/sysfonts, jsonlite. No tidyverse: no dplyr, purrr, magrittr,
|
||||||
|
tibble, or data.table. Prefer an existing dependency or base R over adding one —
|
||||||
|
this package is installed into other people's environments.
|
||||||
|
|
||||||
|
**Brand colors** live in `civilytics_colors` and nowhere else. Never write a hex
|
||||||
|
literal in theme, scale, logo, or table code.
|
||||||
|
|
||||||
|
**Themes** default to a transparent background (`paper_bg = FALSE`) so plots
|
||||||
|
composite onto any Quarto, Reveal, or Typst background. Thread `ink`, `paper`, and
|
||||||
|
`accent` into ggplot2 4.0's base-theme parameters rather than setting element colors
|
||||||
|
one at a time. ggplot2 >= 4.0 is a hard dependency, so use S7 `@` property access on
|
||||||
|
plot and theme objects, not `$`.
|
||||||
|
|
||||||
|
**Naming.** New functions are snake_case. The camelCase exports (`countCleanr`,
|
||||||
|
`dbSafeNames`, `simpleCap`, `waldInterval`, `countDots`, `countNA`, `findDots`,
|
||||||
|
`nvals`) are frozen public API — do not rename them.
|
||||||
|
|
||||||
|
**Documentation.** Roxygen generates both `man/` and `NAMESPACE`. Declare imports
|
||||||
|
with `@importFrom` in the roxygen block; never hand-edit `NAMESPACE`. Every exported
|
||||||
|
function needs `@param` for each argument and `@return`; exported API needs
|
||||||
|
`@examples`. Re-run roxygen in the same commit as the code change — compass files
|
||||||
|
documentation debt when code moves and its `man/` pages do not.
|
||||||
|
|
||||||
|
**Output.** User-facing progress goes through `message()` so callers can suppress it.
|
||||||
|
No `cat()` or `print()` in package code.
|
||||||
|
|
||||||
|
**Assets.** Nothing personal or client-identifying is vendored into `inst/` — brand
|
||||||
|
assets only. Version 0.3.2 removed headshots for exactly this reason.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
testthat edition 3, one file per source file (`tests/testthat/test_theme.R` etc.).
|
||||||
|
Tests are self-sufficient: no reliance on state left by an earlier test. New
|
||||||
|
behaviour needs a test; a bug fix needs a test that fails without the fix.
|
||||||
|
|
||||||
|
```zsh
|
||||||
|
make check # R CMD check --no-manual against a built tarball
|
||||||
|
Rscript -e 'devtools::test()'
|
||||||
|
```
|
||||||
|
|
||||||
|
CI runs `R CMD check` on `rocker/r-ver:4.6` via `.gitea/workflows/check.yaml` for
|
||||||
|
every push and PR to `main`.
|
||||||
|
|
||||||
|
## Release
|
||||||
|
|
||||||
|
Bump `Version` in `DESCRIPTION` and add a `NEWS.md` entry under **New features**,
|
||||||
|
**Bug fixes**, or **Internal**. `NEWS.md` is the changelog; compass tracks the work
|
||||||
|
that led to it, not the release itself.
|
||||||
+4
-2
@@ -1,7 +1,7 @@
|
|||||||
Package: civilytics
|
Package: civilytics
|
||||||
Type: Package
|
Type: Package
|
||||||
Title: Brand Themes, Color Palettes, and Utility Functions for Civilytics
|
Title: Brand Themes, Color Palettes, and Utility Functions for Civilytics
|
||||||
Version: 0.3.1
|
Version: 0.3.2
|
||||||
Authors@R:
|
Authors@R:
|
||||||
person("Jared", "E. Knowles", email = "jared@civilytics.com",
|
person("Jared", "E. Knowles", email = "jared@civilytics.com",
|
||||||
role = c("aut", "cre"))
|
role = c("aut", "cre"))
|
||||||
@@ -34,7 +34,9 @@ Suggests:
|
|||||||
quarto,
|
quarto,
|
||||||
flextable,
|
flextable,
|
||||||
officer,
|
officer,
|
||||||
ragg
|
patchwork,
|
||||||
|
ragg,
|
||||||
|
withr
|
||||||
Config/testthat/edition: 3
|
Config/testthat/edition: 3
|
||||||
Config/roxygen2/version: 8.0.0
|
Config/roxygen2/version: 8.0.0
|
||||||
RoxygenNote: 7.3.3
|
RoxygenNote: 7.3.3
|
||||||
@@ -1,11 +1,41 @@
|
|||||||
# civilytics 0.3.2
|
# civilytics 0.3.2
|
||||||
|
|
||||||
## New features
|
## New features
|
||||||
|
- `civilytics_logo()` and `add_logo()` gain `logo_height` (inches, default `0.5`),
|
||||||
|
the height of a band reserved for the logo.
|
||||||
|
- `civilytics_load_fonts()` gains `showtext` (default `getOption("civilytics.showtext", TRUE)`), so ragg
|
||||||
|
users can register the brand fonts without turning on showtext, which draws text
|
||||||
|
at a third of its size on a 300 dpi ragg device. The default is unchanged (#29).
|
||||||
|
- `patchwork` is now a suggested package, used when a patchwork composition is
|
||||||
|
passed to `civilytics_logo()`.
|
||||||
- Added comprehensive test coverage for the `prop_conf` module, including
|
- Added comprehensive test coverage for the `prop_conf` module, including
|
||||||
correctness checks against `binom.test()` for Clopper-Pearson intervals and
|
correctness checks against `binom.test()` for Clopper-Pearson intervals and
|
||||||
formula-based verification for Wald and Agresti-Coull intervals.
|
formula-based verification for Wald and Agresti-Coull intervals.
|
||||||
|
|
||||||
## Bug fixes
|
## Bug fixes
|
||||||
|
- `civilytics_logo()` / `add_logo()` now render every panel and the
|
||||||
|
`plot_annotation()` of a patchwork composition. They previously drew only the
|
||||||
|
last panel (#20).
|
||||||
|
- The logo now sits in its own reserved band instead of pulling the plot down with
|
||||||
|
negative margins, so captions are no longer clipped at the bottom edge and the
|
||||||
|
layout no longer depends on whether showtext is active (#20).
|
||||||
|
- `add_logo_ga()` gets the same reserved logo band (`logo_height`) and no longer
|
||||||
|
pulls the plots down with a negative margin. `margin_param` still overrides the
|
||||||
|
bottom margin when given (#28).
|
||||||
|
- The logo keeps its own aspect ratio on any canvas. `make_logo_grob()` used to
|
||||||
|
stretch it to a fixed fraction of the plot width, so its shape changed with
|
||||||
|
the canvas and it was already about 1.5 times too wide at 11 by 7 inches (#20).
|
||||||
|
- `font_scale` now defaults to `1` in `civilytics_logo()`, `add_logo()` and
|
||||||
|
`add_logo_ga()`. The old default of `1.1` enlarged the base text by 10% whenever
|
||||||
|
a logo was added. Pass `font_scale = 1.1` to keep the old sizes (#20).
|
||||||
|
- `style_flextable_civilytics()` defaults now come from `civilytics_colors`
|
||||||
|
instead of Bootstrap hex values: header `navy_700`, zebra `paper_2`, outer
|
||||||
|
border `rule_strong`, inner border `rule`, footer text `ink_3`. Tables built
|
||||||
|
with the defaults change appearance (#21).
|
||||||
|
- `civilytics-reveal`: PDF output keeps slide margins, ordered-list numbers no
|
||||||
|
longer clip at the left edge, and Quarto column widths (`.column width="60%"`)
|
||||||
|
are honored. Columns without a width stay equal, and the default gap is now
|
||||||
|
`1.5em` (#25, #26, #27).
|
||||||
- Removed vendored headshot images (`Knowles_Headshot_2019_good.jpg`,
|
- Removed vendored headshot images (`Knowles_Headshot_2019_good.jpg`,
|
||||||
`Knowles_Headshot_2019_prisma.jpg`) from `inst/img/` to prevent personal
|
`Knowles_Headshot_2019_prisma.jpg`) from `inst/img/` to prevent personal
|
||||||
photos from being distributed with the package. The `plot_jpeg()` example
|
photos from being distributed with the package. The `plot_jpeg()` example
|
||||||
|
|||||||
+12
-10
@@ -15,7 +15,8 @@
|
|||||||
#' `flextable` is unavailable.
|
#' `flextable` is unavailable.
|
||||||
#'
|
#'
|
||||||
#' @param ft A [flextable::flextable()] object.
|
#' @param ft A [flextable::flextable()] object.
|
||||||
#' @param header_bg Character. Header background fill. Default `"#2c3e50"`.
|
#' @param header_bg Character. Header background fill. Default
|
||||||
|
#' `civilytics_colors[["navy_700"]]`.
|
||||||
#' @param header_color Character. Header text color. Default `"white"`.
|
#' @param header_color Character. Header text color. Default `"white"`.
|
||||||
#' @param title_fontsize Numeric. Font size for the first header line (the
|
#' @param title_fontsize Numeric. Font size for the first header line (the
|
||||||
#' title row, `i = 1`). Default `14`.
|
#' title row, `i = 1`). Default `14`.
|
||||||
@@ -25,17 +26,18 @@
|
|||||||
#' @param zebra Logical. Apply alternating-row striping to even body rows.
|
#' @param zebra Logical. Apply alternating-row striping to even body rows.
|
||||||
#' Default `TRUE`. Safely skipped for tables with fewer than two body rows.
|
#' Default `TRUE`. Safely skipped for tables with fewer than two body rows.
|
||||||
#' @param zebra_bg Character. Fill color for striped (even) body rows.
|
#' @param zebra_bg Character. Fill color for striped (even) body rows.
|
||||||
#' Default `"#f0f0eb"`.
|
#' Default `civilytics_colors[["paper_2"]]`.
|
||||||
#' @param outer_border_color Character. Outer border color. Default
|
#' @param outer_border_color Character. Outer border color. Default
|
||||||
#' `"#888888"`.
|
#' `civilytics_colors[["rule_strong"]]`.
|
||||||
#' @param outer_border_width Numeric. Outer border width. Default `1`.
|
#' @param outer_border_width Numeric. Outer border width. Default `1`.
|
||||||
#' @param inner_border_color Character. Inner horizontal border color (body).
|
#' @param inner_border_color Character. Inner horizontal border color (body).
|
||||||
#' Default `"#cccccc"`.
|
#' Default `civilytics_colors[["rule"]]`.
|
||||||
#' @param inner_border_width Numeric. Inner horizontal border width. Default
|
#' @param inner_border_width Numeric. Inner horizontal border width. Default
|
||||||
#' `0.5`.
|
#' `0.5`.
|
||||||
#' @param footer_fontsize Numeric. Footer font size (applied only if a footer
|
#' @param footer_fontsize Numeric. Footer font size (applied only if a footer
|
||||||
#' part exists). Default `9`.
|
#' part exists). Default `9`.
|
||||||
#' @param footer_color Character. Footer text color. Default `"#555555"`.
|
#' @param footer_color Character. Footer text color. Default
|
||||||
|
#' `civilytics_colors[["ink_3"]]`.
|
||||||
#'
|
#'
|
||||||
#' @return The styled [flextable::flextable()] object.
|
#' @return The styled [flextable::flextable()] object.
|
||||||
#' @export
|
#' @export
|
||||||
@@ -50,19 +52,19 @@
|
|||||||
#' style_flextable_civilytics()
|
#' style_flextable_civilytics()
|
||||||
#' }
|
#' }
|
||||||
style_flextable_civilytics <- function(ft,
|
style_flextable_civilytics <- function(ft,
|
||||||
header_bg = "#2c3e50",
|
header_bg = civilytics_colors[["navy_700"]],
|
||||||
header_color = "white",
|
header_color = "white",
|
||||||
title_fontsize = 14,
|
title_fontsize = 14,
|
||||||
body_fontsize = 11,
|
body_fontsize = 11,
|
||||||
font_name = "Arial",
|
font_name = "Arial",
|
||||||
zebra = TRUE,
|
zebra = TRUE,
|
||||||
zebra_bg = "#f0f0eb",
|
zebra_bg = civilytics_colors[["paper_2"]],
|
||||||
outer_border_color = "#888888",
|
outer_border_color = civilytics_colors[["rule_strong"]],
|
||||||
outer_border_width = 1,
|
outer_border_width = 1,
|
||||||
inner_border_color = "#cccccc",
|
inner_border_color = civilytics_colors[["rule"]],
|
||||||
inner_border_width = 0.5,
|
inner_border_width = 0.5,
|
||||||
footer_fontsize = 9,
|
footer_fontsize = 9,
|
||||||
footer_color = "#555555") {
|
footer_color = civilytics_colors[["ink_3"]]) {
|
||||||
if (!requireNamespace("flextable", quietly = TRUE)) {
|
if (!requireNamespace("flextable", quietly = TRUE)) {
|
||||||
stop("Install 'flextable' to use this function.", call. = FALSE)
|
stop("Install 'flextable' to use this function.", call. = FALSE)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,16 +14,26 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts
|
|||||||
#' Load Civilytics brand fonts
|
#' Load Civilytics brand fonts
|
||||||
#'
|
#'
|
||||||
#' Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
#' Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
||||||
#' Google Fonts via [sysfonts::font_add_google()], then calls
|
#' Google Fonts via [sysfonts::font_add_google()], then, unless
|
||||||
#' [showtext::showtext_auto()] so that all graphics devices render text with
|
#' `showtext = FALSE`, calls [showtext::showtext_auto()] so that all graphics
|
||||||
#' those fonts. This is called automatically when the package loads; use this
|
#' devices render text with those fonts. This is called automatically when the
|
||||||
#' function to retry if the initial load failed (e.g., the machine was offline
|
#' package loads; use this function to retry if the initial load failed (e.g.,
|
||||||
#' at load time).
|
#' the machine was offline at load time).
|
||||||
#'
|
#'
|
||||||
#' Subsequent calls within the same session are no-ops unless `force = TRUE`.
|
#' Subsequent calls within the same session are no-ops unless `force = TRUE`.
|
||||||
#'
|
#'
|
||||||
|
#' **Using ragg.** [showtext::showtext_auto()] draws text at 96 dpi whatever the
|
||||||
|
#' device resolution, so on a `ragg::agg_png(res = 300)` device text comes out
|
||||||
|
#' at about a third of its requested size. For ragg, set
|
||||||
|
#' `options(civilytics.showtext = FALSE)` before loading the package, or call
|
||||||
|
#' `showtext::showtext_auto(FALSE)` afterwards. The brand fonts must then be
|
||||||
|
#' installed on the system, because ragg reads system fonts rather than the
|
||||||
|
#' ones registered through sysfonts.
|
||||||
|
#'
|
||||||
#' @param force Logical. If `TRUE`, reload fonts even if they were already
|
#' @param force Logical. If `TRUE`, reload fonts even if they were already
|
||||||
#' loaded in this session. Default `FALSE`.
|
#' loaded in this session. Default `FALSE`.
|
||||||
|
#' @param showtext Logical. Turn on [showtext::showtext_auto()] after
|
||||||
|
#' registering the fonts. Default is `getOption("civilytics.showtext", TRUE)`.
|
||||||
#'
|
#'
|
||||||
#' @return Invisibly returns `TRUE` if fonts were loaded, `FALSE` if skipped
|
#' @return Invisibly returns `TRUE` if fonts were loaded, `FALSE` if skipped
|
||||||
#' (already loaded and `force = FALSE`).
|
#' (already loaded and `force = FALSE`).
|
||||||
@@ -32,15 +42,22 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts
|
|||||||
#' @examples
|
#' @examples
|
||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
#' civilytics_load_fonts()
|
#' civilytics_load_fonts()
|
||||||
|
#'
|
||||||
|
#' # For ragg devices: register the fonts but leave showtext off
|
||||||
|
#' civilytics_load_fonts(force = TRUE, showtext = FALSE)
|
||||||
#' }
|
#' }
|
||||||
civilytics_load_fonts <- function(force = FALSE) {
|
civilytics_load_fonts <- function(force = FALSE,
|
||||||
|
showtext = getOption("civilytics.showtext", TRUE)) {
|
||||||
|
if (!is.logical(showtext) || length(showtext) != 1L || is.na(showtext)) {
|
||||||
|
stop("`showtext` must be a single TRUE or FALSE.", call. = FALSE)
|
||||||
|
}
|
||||||
if (.cv_state$fonts_loaded && !force) return(invisible(FALSE))
|
if (.cv_state$fonts_loaded && !force) return(invisible(FALSE))
|
||||||
|
|
||||||
sysfonts::font_add_google("Inter", family = "Inter")
|
sysfonts::font_add_google("Inter", family = "Inter")
|
||||||
sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin")
|
sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin")
|
||||||
sysfonts::font_add_google("Source Serif 4", family = "Source Serif 4")
|
sysfonts::font_add_google("Source Serif 4", family = "Source Serif 4")
|
||||||
sysfonts::font_add_google("JetBrains Mono", family = "JetBrains Mono")
|
sysfonts::font_add_google("JetBrains Mono", family = "JetBrains Mono")
|
||||||
showtext::showtext_auto()
|
if (showtext) showtext::showtext_auto()
|
||||||
|
|
||||||
.cv_state$fonts_loaded <- TRUE
|
.cv_state$fonts_loaded <- TRUE
|
||||||
invisible(TRUE)
|
invisible(TRUE)
|
||||||
|
|||||||
@@ -44,65 +44,67 @@ get_png <- function(filename) {
|
|||||||
|
|
||||||
#' Add a logo to a ggplot2 object
|
#' Add a logo to a ggplot2 object
|
||||||
#'
|
#'
|
||||||
#' @param plot a ggplot2 grob
|
#' The logo gets its own band of reserved height (`logo_height`, in inches)
|
||||||
|
#' below or above the plot, so it cannot overlap the plot's caption or push
|
||||||
|
#' anything off the canvas. Patchwork compositions are converted with
|
||||||
|
#' [patchwork::patchworkGrob()] so every panel and the
|
||||||
|
#' [patchwork::plot_annotation()] survive.
|
||||||
|
#'
|
||||||
|
#' @param plot a ggplot2 object, or a patchwork composition
|
||||||
#' @param logo a logo grob created by make_logo_grob()
|
#' @param logo a logo grob created by make_logo_grob()
|
||||||
#' @param margin_param a numeric specifying what margin to add or subtract to align the logo
|
#' @param margin_param Optional numeric. Replaces the plot margin (in points)
|
||||||
#' @param font_scale Numeric. Multiplicative scaling factor applied to text
|
#' on the side next to the logo. `NULL` (default) leaves the plot's margins
|
||||||
#' sizes before composing the plot with the logo. Default `1.1` inflates
|
#' alone.
|
||||||
#' text by ~10 \% to compensate for the viewport shrinkage caused by
|
#' @param font_scale Numeric. Multiplicative scaling factor applied to the
|
||||||
#' [gridExtra::arrangeGrob()]. Set to `1` to disable.
|
#' base text size before composing. Default `1` leaves typography unchanged.
|
||||||
#' @param position Character. Corner placement for the logo: `"bottom-right"`
|
#' @param position Character. Corner placement for the logo: `"bottom-right"`
|
||||||
#' (default), `"bottom-left"`, `"top-right"`, or `"top-left"`. Controls
|
#' (default), `"bottom-left"`, `"top-right"`, or `"top-left"`. Controls
|
||||||
#' whether the logo is placed above or below the plot.
|
#' whether the logo is placed above or below the plot.
|
||||||
|
#' @param logo_height Numeric. Height in inches of the band reserved for the
|
||||||
|
#' logo. Default `0.5`.
|
||||||
#'
|
#'
|
||||||
#' @return a grob with a logo attached to it ready to plot
|
#' @return a grob with a logo attached to it ready to plot
|
||||||
#' @importFrom ggplot2 theme
|
#' @importFrom ggplot2 theme
|
||||||
#' @importFrom gridExtra arrangeGrob
|
#' @importFrom gridExtra arrangeGrob
|
||||||
#' @export
|
#' @export
|
||||||
add_logo <- function(plot, logo, margin_param = NULL, font_scale = 1.1,
|
add_logo <- function(plot, logo, margin_param = NULL, font_scale = 1,
|
||||||
position = c("bottom-right", "bottom-left",
|
position = c("bottom-right", "bottom-left",
|
||||||
"top-right", "top-left")) {
|
"top-right", "top-left"),
|
||||||
|
logo_height = 0.5) {
|
||||||
position <- match.arg(position)
|
position <- match.arg(position)
|
||||||
at_top <- grepl("top", position)
|
at_top <- grepl("top", position)
|
||||||
|
is_patchwork <- inherits(plot, "patchwork")
|
||||||
|
|
||||||
# Capture the plot's background color so we can fill the entire composed
|
# Capture the plot's background color so we can fill the entire composed
|
||||||
# grob with it. The logo grob must stay transparent (theme_void) so that
|
# grob with it. The logo grob stays transparent (theme_void).
|
||||||
# caption/axis text overlapping via negative margins remains visible.
|
|
||||||
bg_fill <- plot$theme$plot.background$fill
|
bg_fill <- plot$theme$plot.background$fill
|
||||||
|
|
||||||
# Inflate text sizes to compensate for arrangeGrob viewport shrinkage.
|
|
||||||
# All theme text elements use rel() sizing, so scaling the root 'text'
|
|
||||||
# element cascades to titles, axis labels, legends, captions, and strips.
|
|
||||||
if (!is.null(font_scale) && font_scale != 1) {
|
if (!is.null(font_scale) && font_scale != 1) {
|
||||||
base_size <- plot$theme$text$size %||% 14
|
base_size <- plot$theme$text$size
|
||||||
plot <- plot + theme(
|
if (is.null(base_size)) base_size <- 14
|
||||||
text = element_text(size = base_size * font_scale)
|
scaled <- theme(text = ggplot2::element_text(size = base_size * font_scale))
|
||||||
)
|
plot <- if (is_patchwork) plot & scaled else plot + scaled
|
||||||
}
|
}
|
||||||
|
|
||||||
if (at_top) {
|
if (!is.null(margin_param)) {
|
||||||
# For top placement, tighten the top margin of the plot
|
side <- if (at_top) c(margin_param, 7, 7, 7) else c(7, 7, margin_param, 7)
|
||||||
if (!is.null(margin_param)) {
|
margin <- theme(plot.margin = grid::unit(side, "pt"))
|
||||||
plot <- plot + theme(plot.margin = unit(c(margin_param, 7, 7, 7), "pt"))
|
plot <- if (is_patchwork) plot & margin else plot + margin
|
||||||
} else {
|
}
|
||||||
plot <- plot + theme(plot.margin = unit(c(-14, 7, 7, 7), "pt"))
|
|
||||||
}
|
# ggplot_build() on a patchwork builds only its last panel, so convert it
|
||||||
composed <- arrangeGrob(logo, plot, heights = c(0.1, 0.93),
|
# explicitly before handing it to arrangeGrob().
|
||||||
padding = unit(0.1, "line"))
|
plot_grob <- if (is_patchwork) patchwork::patchworkGrob(plot) else plot
|
||||||
|
|
||||||
|
plot_height <- grid::unit(1, "null")
|
||||||
|
band_height <- grid::unit(logo_height, "in")
|
||||||
|
composed <- if (at_top) {
|
||||||
|
arrangeGrob(logo, plot_grob, heights = grid::unit.c(band_height, plot_height),
|
||||||
|
padding = grid::unit(0.1, "line"))
|
||||||
} else {
|
} else {
|
||||||
# Bottom placement (original behaviour)
|
arrangeGrob(plot_grob, logo,
|
||||||
if(has_caption(plot)) {
|
heights = grid::unit.c(plot_height, band_height),
|
||||||
cap_lines <- measure_caption(plot)
|
padding = grid::unit(0.1, "line"))
|
||||||
if (!is.null(margin_param)) {
|
|
||||||
plot <- plot + theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
|
||||||
} else {
|
|
||||||
plot <- plot + theme(plot.margin = unit(c(7, 7, cap_lines * -52, 7), "pt"))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
plot <- plot + theme(plot.margin = unit(c(7, 7, -14, 7), "pt"))
|
|
||||||
}
|
|
||||||
composed <- arrangeGrob(plot, logo, heights = c(0.93, 0.1),
|
|
||||||
padding = unit(0.1, "line"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Wrap with a full-bleed background rect so any transparent areas (the
|
# Wrap with a full-bleed background rect so any transparent areas (the
|
||||||
@@ -158,9 +160,12 @@ has_caption <- function(gg) {
|
|||||||
#' @param logo a grob containing the logo created with `make_logo_grob`
|
#' @param logo a grob containing the logo created with `make_logo_grob`
|
||||||
#' @param nrow an integer, default = 1, for the number of rows to align the plots in
|
#' @param nrow an integer, default = 1, for the number of rows to align the plots in
|
||||||
#' @param widths an optional vector the same length as plot_list with the widths for each plot
|
#' @param widths an optional vector the same length as plot_list with the widths for each plot
|
||||||
#' @param margin_param a number giving the adjustment up or down to help manually align logo and captions
|
#' @param margin_param Optional number. Replaces the bottom plot margin (in points).
|
||||||
|
#' `NULL` (default) leaves plot margins alone.
|
||||||
#' @param font_scale Numeric. Multiplicative scaling factor applied to text
|
#' @param font_scale Numeric. Multiplicative scaling factor applied to text
|
||||||
#' sizes before composing. Default `1.1`. See [add_logo()] for details.
|
#' sizes before composing. Default `1`. See [add_logo()] for details.
|
||||||
|
#' @param logo_height Numeric. Height in inches of the band reserved for the
|
||||||
|
#' logo below the plots. Default `0.5`.
|
||||||
#'
|
#'
|
||||||
#' @return a grid object
|
#' @return a grid object
|
||||||
#' @note The resulting object needs to be drawn to the screen using grid.draw()
|
#' @note The resulting object needs to be drawn to the screen using grid.draw()
|
||||||
@@ -179,11 +184,11 @@ has_caption <- function(gg) {
|
|||||||
#' dev.off()
|
#' dev.off()
|
||||||
#' }
|
#' }
|
||||||
add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL,
|
add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL,
|
||||||
margin_param = NULL, font_scale = 1.1) {
|
margin_param = NULL, font_scale = 1,
|
||||||
|
logo_height = 0.5) {
|
||||||
# Capture the background color from the first plot
|
# Capture the background color from the first plot
|
||||||
bg_fill <- plot_list[[1]]$theme$plot.background$fill
|
bg_fill <- plot_list[[1]]$theme$plot.background$fill
|
||||||
|
|
||||||
# Inflate text sizes to compensate for viewport shrinkage
|
|
||||||
if (!is.null(font_scale) && font_scale != 1) {
|
if (!is.null(font_scale) && font_scale != 1) {
|
||||||
plot_list <- lapply(plot_list, function(p) {
|
plot_list <- lapply(plot_list, function(p) {
|
||||||
base_size <- p$theme$text$size %||% 14
|
base_size <- p$theme$text$size %||% 14
|
||||||
@@ -193,20 +198,16 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
# Change position of logo depending on if plot has a caption
|
# The logo has its own band below the plots, so no margin adjustment is
|
||||||
|
# needed. `margin_param` still replaces the bottom margin when given.
|
||||||
if (!is.null(margin_param)) {
|
if (!is.null(margin_param)) {
|
||||||
margin <- theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
margin <- theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
||||||
} else if (any(unlist(lapply(plot_list, has_caption)))) {
|
if (nrow > 1) {
|
||||||
cap_lines <- max(sapply(plot_list, measure_caption))
|
len <- length(plot_list)
|
||||||
margin <- theme(plot.margin = unit(c(7, 7, -7 * sqrt(cap_lines), 7), "pt"))
|
plot_list[[len]] <- plot_list[[len]] + margin
|
||||||
} else {
|
} else {
|
||||||
margin <- theme(plot.margin = unit(c(7, 7, 7, 7), "pt"))
|
plot_list <- lapply(plot_list, "+", margin)
|
||||||
}
|
}
|
||||||
if (nrow > 1) {
|
|
||||||
len <- length(plot_list)
|
|
||||||
plot_list[[len]] <- plot_list[[len]] + margin
|
|
||||||
} else {
|
|
||||||
plot_list <- lapply(plot_list, "+", margin)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nrow != 1) {
|
if (nrow != 1) {
|
||||||
@@ -216,7 +217,10 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL,
|
|||||||
hold <- arrangeGrob(grobs = plot_list, nrow = 1, ncol = length(plot_list), widths = widths)
|
hold <- arrangeGrob(grobs = plot_list, nrow = 1, ncol = length(plot_list), widths = widths)
|
||||||
}
|
}
|
||||||
|
|
||||||
composed <- arrangeGrob(hold, logo, heights = c(0.93, .07))
|
composed <- arrangeGrob(
|
||||||
|
hold, logo,
|
||||||
|
heights = grid::unit.c(grid::unit(1, "null"), grid::unit(logo_height, "in"))
|
||||||
|
)
|
||||||
|
|
||||||
if (!is.null(bg_fill) && !is.na(bg_fill)) {
|
if (!is.null(bg_fill) && !is.na(bg_fill)) {
|
||||||
bg_rect <- grid::rectGrob(gp = grid::gpar(fill = bg_fill, col = NA))
|
bg_rect <- grid::rectGrob(gp = grid::gpar(fill = bg_fill, col = NA))
|
||||||
@@ -268,20 +272,20 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
|||||||
|
|
||||||
align_left <- grepl("left", position)
|
align_left <- grepl("left", position)
|
||||||
|
|
||||||
if (align_left) {
|
# Give the raster a height only, so grid derives its width from the image's
|
||||||
xmin <- 0
|
# own aspect ratio, then pin it to the corner. The logo is the same shape on
|
||||||
xmax <- if (type == "mark") 0.07 else 0.35
|
# any canvas width.
|
||||||
} else {
|
logo <- grid::rasterGrob(
|
||||||
xmin <- if (type == "mark") 0.93 else 0.65
|
png::readPNG(system.file("img", img_file, package = "civilytics")),
|
||||||
xmax <- 1
|
x = if (align_left) grid::unit(7, "pt") else grid::unit(1, "npc") - grid::unit(7, "pt"),
|
||||||
}
|
just = if (align_left) "left" else "right",
|
||||||
|
height = grid::unit(1, "npc"),
|
||||||
|
interpolate = TRUE
|
||||||
|
)
|
||||||
|
|
||||||
ggplot() +
|
ggplot() +
|
||||||
theme_void() +
|
theme_void() +
|
||||||
annotation_custom(
|
annotation_custom(logo, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
|
||||||
get_png(system.file("img", img_file, package = "civilytics")),
|
|
||||||
xmin = xmin, xmax = xmax
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Add a Civilytics logo to a ggplot (pipe-friendly)
|
#' Add a Civilytics logo to a ggplot (pipe-friendly)
|
||||||
@@ -308,9 +312,10 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
|||||||
#' (default), `"bottom-left"`, `"top-right"`, or `"top-left"`.
|
#' (default), `"bottom-left"`, `"top-right"`, or `"top-left"`.
|
||||||
#' @param margin_param Numeric or `NULL`. Manual margin adjustment passed to
|
#' @param margin_param Numeric or `NULL`. Manual margin adjustment passed to
|
||||||
#' [add_logo()].
|
#' [add_logo()].
|
||||||
#' @param font_scale Numeric. Inflate text sizes by this factor to compensate
|
#' @param font_scale Numeric. Multiplies the plot's base text size before
|
||||||
#' for viewport shrinkage when composing with [gridExtra::arrangeGrob()].
|
#' composing. Default `1` leaves typography unchanged.
|
||||||
#' Default `1.1` (~10 \% inflation). Set to `1` to disable.
|
#' @param logo_height Numeric. Height in inches of the band reserved for the
|
||||||
|
#' logo. Default `0.5`.
|
||||||
#'
|
#'
|
||||||
#' @return A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
#' @return A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
||||||
#' [grid::grid.draw()].
|
#' [grid::grid.draw()].
|
||||||
@@ -355,11 +360,12 @@ civilytics_logo <- function(plot,
|
|||||||
position = c("bottom-right", "bottom-left",
|
position = c("bottom-right", "bottom-left",
|
||||||
"top-right", "top-left"),
|
"top-right", "top-left"),
|
||||||
margin_param = NULL,
|
margin_param = NULL,
|
||||||
font_scale = 1.1) {
|
font_scale = 1,
|
||||||
|
logo_height = 0.5) {
|
||||||
position <- match.arg(position)
|
position <- match.arg(position)
|
||||||
logo <- make_logo_grob(type = type, variant = variant, position = position)
|
logo <- make_logo_grob(type = type, variant = variant, position = position)
|
||||||
add_logo(plot, logo, margin_param = margin_param, font_scale = font_scale,
|
add_logo(plot, logo, margin_param = margin_param, font_scale = font_scale,
|
||||||
position = position)
|
position = position, logo_height = logo_height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -209,7 +209,11 @@ $code-color: #923D00 !default;
|
|||||||
height: 2px;
|
height: 2px;
|
||||||
background: var(--accent-600);
|
background: var(--accent-600);
|
||||||
}
|
}
|
||||||
.reveal ol { list-style: decimal; }
|
.reveal ol {
|
||||||
|
list-style: decimal;
|
||||||
|
/* Room for two-digit markers in the bold mono face (#27) */
|
||||||
|
padding-left: 1.6em;
|
||||||
|
}
|
||||||
.reveal ol > li::marker {
|
.reveal ol > li::marker {
|
||||||
color: var(--accent-600);
|
color: var(--accent-600);
|
||||||
font-family: var(--font-mono);
|
font-family: var(--font-mono);
|
||||||
@@ -747,7 +751,7 @@ img.civilytics-wordmark,
|
|||||||
----------------------------------------------------------- */
|
----------------------------------------------------------- */
|
||||||
.reveal .columns {
|
.reveal .columns {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 3em;
|
gap: 1.5em;
|
||||||
align-items: flex-start;
|
align-items: flex-start;
|
||||||
margin: 0.25em 0;
|
margin: 0.25em 0;
|
||||||
}
|
}
|
||||||
@@ -755,6 +759,11 @@ img.civilytics-wordmark,
|
|||||||
flex: 1;
|
flex: 1;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
}
|
}
|
||||||
|
/* Quarto writes width="60%" as an inline style; honor it (#26). Columns
|
||||||
|
without a width stay equal. */
|
||||||
|
.reveal .column[style*="width"] {
|
||||||
|
flex: 0 1 auto;
|
||||||
|
}
|
||||||
.reveal .column > :first-child { margin-top: 0; }
|
.reveal .column > :first-child { margin-top: 0; }
|
||||||
|
|
||||||
/* Hand-rolled two-col for pandoc (non-Quarto) users */
|
/* Hand-rolled two-col for pandoc (non-Quarto) users */
|
||||||
@@ -1040,6 +1049,15 @@ body.civilytics-hide-chrome .slide-number {
|
|||||||
.reveal section { break-inside: avoid; }
|
.reveal section { break-inside: avoid; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* reveal.js print-pdf zeroes section padding with !important, so the screen
|
||||||
|
padding is lost. Restore a smaller print value; 48px 72px pushed dense
|
||||||
|
slides onto extra pages (#25). */
|
||||||
|
html.print-pdf .reveal .slides section:not(.full-bleed):not(.stack),
|
||||||
|
html.reveal-print .reveal .slides section:not(.full-bleed):not(.stack) {
|
||||||
|
padding: 32px 56px !important;
|
||||||
|
box-sizing: border-box !important;
|
||||||
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------
|
/* -----------------------------------------------------------
|
||||||
SMALL-SCREEN GUARDRAILS
|
SMALL-SCREEN GUARDRAILS
|
||||||
Reveal scales slides, but the narrowest keep-legible margins.
|
Reveal scales slides, but the narrowest keep-legible margins.
|
||||||
|
|||||||
+17
-9
@@ -8,29 +8,37 @@ add_logo(
|
|||||||
plot,
|
plot,
|
||||||
logo,
|
logo,
|
||||||
margin_param = NULL,
|
margin_param = NULL,
|
||||||
font_scale = 1.1,
|
font_scale = 1,
|
||||||
position = c("bottom-right", "bottom-left", "top-right", "top-left")
|
position = c("bottom-right", "bottom-left", "top-right", "top-left"),
|
||||||
|
logo_height = 0.5
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{plot}{a ggplot2 grob}
|
\item{plot}{a ggplot2 object, or a patchwork composition}
|
||||||
|
|
||||||
\item{logo}{a logo grob created by make_logo_grob()}
|
\item{logo}{a logo grob created by make_logo_grob()}
|
||||||
|
|
||||||
\item{margin_param}{a numeric specifying what margin to add or subtract to align the logo}
|
\item{margin_param}{Optional numeric. Replaces the plot margin (in points)
|
||||||
|
on the side next to the logo. `NULL` (default) leaves the plot's margins
|
||||||
|
alone.}
|
||||||
|
|
||||||
\item{font_scale}{Numeric. Multiplicative scaling factor applied to text
|
\item{font_scale}{Numeric. Multiplicative scaling factor applied to the
|
||||||
sizes before composing the plot with the logo. Default `1.1` inflates
|
base text size before composing. Default `1` leaves typography unchanged.}
|
||||||
text by ~10 \% to compensate for the viewport shrinkage caused by
|
|
||||||
[gridExtra::arrangeGrob()]. Set to `1` to disable.}
|
|
||||||
|
|
||||||
\item{position}{Character. Corner placement for the logo: `"bottom-right"`
|
\item{position}{Character. Corner placement for the logo: `"bottom-right"`
|
||||||
(default), `"bottom-left"`, `"top-right"`, or `"top-left"`. Controls
|
(default), `"bottom-left"`, `"top-right"`, or `"top-left"`. Controls
|
||||||
whether the logo is placed above or below the plot.}
|
whether the logo is placed above or below the plot.}
|
||||||
|
|
||||||
|
\item{logo_height}{Numeric. Height in inches of the band reserved for the
|
||||||
|
logo. Default `0.5`.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
a grob with a logo attached to it ready to plot
|
a grob with a logo attached to it ready to plot
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Add a logo to a ggplot2 object
|
The logo gets its own band of reserved height (`logo_height`, in inches)
|
||||||
|
below or above the plot, so it cannot overlap the plot's caption or push
|
||||||
|
anything off the canvas. Patchwork compositions are converted with
|
||||||
|
[patchwork::patchworkGrob()] so every panel and the
|
||||||
|
[patchwork::plot_annotation()] survive.
|
||||||
}
|
}
|
||||||
+8
-3
@@ -10,7 +10,8 @@ add_logo_ga(
|
|||||||
nrow = 1,
|
nrow = 1,
|
||||||
widths = NULL,
|
widths = NULL,
|
||||||
margin_param = NULL,
|
margin_param = NULL,
|
||||||
font_scale = 1.1
|
font_scale = 1,
|
||||||
|
logo_height = 0.5
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
@@ -22,10 +23,14 @@ add_logo_ga(
|
|||||||
|
|
||||||
\item{widths}{an optional vector the same length as plot_list with the widths for each plot}
|
\item{widths}{an optional vector the same length as plot_list with the widths for each plot}
|
||||||
|
|
||||||
\item{margin_param}{a number giving the adjustment up or down to help manually align logo and captions}
|
\item{margin_param}{Optional number. Replaces the bottom plot margin (in points).
|
||||||
|
`NULL` (default) leaves plot margins alone.}
|
||||||
|
|
||||||
\item{font_scale}{Numeric. Multiplicative scaling factor applied to text
|
\item{font_scale}{Numeric. Multiplicative scaling factor applied to text
|
||||||
sizes before composing. Default `1.1`. See [add_logo()] for details.}
|
sizes before composing. Default `1`. See [add_logo()] for details.}
|
||||||
|
|
||||||
|
\item{logo_height}{Numeric. Height in inches of the band reserved for the
|
||||||
|
logo below the plots. Default `0.5`.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
a grid object
|
a grid object
|
||||||
|
|||||||
@@ -4,28 +4,46 @@
|
|||||||
\alias{civilytics_load_fonts}
|
\alias{civilytics_load_fonts}
|
||||||
\title{Load Civilytics brand fonts}
|
\title{Load Civilytics brand fonts}
|
||||||
\usage{
|
\usage{
|
||||||
civilytics_load_fonts(force = FALSE)
|
civilytics_load_fonts(
|
||||||
|
force = FALSE,
|
||||||
|
showtext = getOption("civilytics.showtext", TRUE)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{force}{Logical. If \code{TRUE}, reload fonts even if they were already
|
\item{force}{Logical. If \code{TRUE}, reload fonts even if they were already
|
||||||
loaded in this session. Default \code{FALSE}.}
|
loaded in this session. Default \code{FALSE}.}
|
||||||
|
|
||||||
|
\item{showtext}{Logical. Turn on \code{showtext::showtext_auto()} after
|
||||||
|
registering the fonts. Default is \code{getOption("civilytics.showtext", TRUE)}.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
Invisibly returns \code{TRUE} if fonts were loaded, \code{FALSE} if skipped
|
Invisibly returns \code{TRUE} if fonts were loaded, \code{FALSE} if skipped
|
||||||
(already loaded and \code{force = FALSE}).
|
(already loaded and \code{force = FALSE}).
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
||||||
Google Fonts via \code{sysfonts::font_add_google()}, then calls
|
Google Fonts via \code{sysfonts::font_add_google()}, then, unless
|
||||||
\code{showtext::showtext_auto()} so that all graphics devices render text with
|
\code{showtext = FALSE}, calls \code{showtext::showtext_auto()} so that all graphics
|
||||||
those fonts. This is called automatically when the package loads; use this
|
devices render text with those fonts. This is called automatically when the
|
||||||
function to retry if the initial load failed (e.g., the machine was offline
|
package loads; use this function to retry if the initial load failed (e.g.,
|
||||||
at load time).
|
the machine was offline at load time).
|
||||||
|
}
|
||||||
|
\details{
|
||||||
Subsequent calls within the same session are no-ops unless \code{force = TRUE}.
|
Subsequent calls within the same session are no-ops unless \code{force = TRUE}.
|
||||||
|
|
||||||
|
\strong{Using ragg.} \code{showtext::showtext_auto()} draws text at 96 dpi whatever the
|
||||||
|
device resolution, so on a \code{ragg::agg_png(res = 300)} device text comes out
|
||||||
|
at about a third of its requested size. For ragg, set
|
||||||
|
\code{options(civilytics.showtext = FALSE)} before loading the package, or call
|
||||||
|
\code{showtext::showtext_auto(FALSE)} afterwards. The brand fonts must then be
|
||||||
|
installed on the system, because ragg reads system fonts rather than the
|
||||||
|
ones registered through sysfonts.
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
\dontrun{
|
\dontrun{
|
||||||
civilytics_load_fonts()
|
civilytics_load_fonts()
|
||||||
|
|
||||||
|
# For ragg devices: register the fonts but leave showtext off
|
||||||
|
civilytics_load_fonts(force = TRUE, showtext = FALSE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,8 @@ civilytics_logo(
|
|||||||
variant = c("light", "dark"),
|
variant = c("light", "dark"),
|
||||||
position = c("bottom-right", "bottom-left", "top-right", "top-left"),
|
position = c("bottom-right", "bottom-left", "top-right", "top-left"),
|
||||||
margin_param = NULL,
|
margin_param = NULL,
|
||||||
font_scale = 1.1
|
font_scale = 1,
|
||||||
|
logo_height = 0.5
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
@@ -28,9 +29,11 @@ civilytics_logo(
|
|||||||
\item{margin_param}{Numeric or `NULL`. Manual margin adjustment passed to
|
\item{margin_param}{Numeric or `NULL`. Manual margin adjustment passed to
|
||||||
[add_logo()].}
|
[add_logo()].}
|
||||||
|
|
||||||
\item{font_scale}{Numeric. Inflate text sizes by this factor to compensate
|
\item{font_scale}{Numeric. Multiplies the plot's base text size before
|
||||||
for viewport shrinkage when composing with [gridExtra::arrangeGrob()].
|
composing. Default `1` leaves typography unchanged.}
|
||||||
Default `1.1` (~10 \% inflation). Set to `1` to disable.}
|
|
||||||
|
\item{logo_height}{Numeric. Height in inches of the band reserved for the
|
||||||
|
logo. Default `0.5`.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
||||||
|
|||||||
@@ -6,25 +6,26 @@
|
|||||||
\usage{
|
\usage{
|
||||||
style_flextable_civilytics(
|
style_flextable_civilytics(
|
||||||
ft,
|
ft,
|
||||||
header_bg = "#2c3e50",
|
header_bg = civilytics_colors[["navy_700"]],
|
||||||
header_color = "white",
|
header_color = "white",
|
||||||
title_fontsize = 14,
|
title_fontsize = 14,
|
||||||
body_fontsize = 11,
|
body_fontsize = 11,
|
||||||
font_name = "Arial",
|
font_name = "Arial",
|
||||||
zebra = TRUE,
|
zebra = TRUE,
|
||||||
zebra_bg = "#f0f0eb",
|
zebra_bg = civilytics_colors[["paper_2"]],
|
||||||
outer_border_color = "#888888",
|
outer_border_color = civilytics_colors[["rule_strong"]],
|
||||||
outer_border_width = 1,
|
outer_border_width = 1,
|
||||||
inner_border_color = "#cccccc",
|
inner_border_color = civilytics_colors[["rule"]],
|
||||||
inner_border_width = 0.5,
|
inner_border_width = 0.5,
|
||||||
footer_fontsize = 9,
|
footer_fontsize = 9,
|
||||||
footer_color = "#555555"
|
footer_color = civilytics_colors[["ink_3"]]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{ft}{A [flextable::flextable()] object.}
|
\item{ft}{A [flextable::flextable()] object.}
|
||||||
|
|
||||||
\item{header_bg}{Character. Header background fill. Default `"#2c3e50"`.}
|
\item{header_bg}{Character. Header background fill. Default
|
||||||
|
`civilytics_colors[["navy_700"]]`.}
|
||||||
|
|
||||||
\item{header_color}{Character. Header text color. Default `"white"`.}
|
\item{header_color}{Character. Header text color. Default `"white"`.}
|
||||||
|
|
||||||
@@ -40,15 +41,15 @@ title row, `i = 1`). Default `14`.}
|
|||||||
Default `TRUE`. Safely skipped for tables with fewer than two body rows.}
|
Default `TRUE`. Safely skipped for tables with fewer than two body rows.}
|
||||||
|
|
||||||
\item{zebra_bg}{Character. Fill color for striped (even) body rows.
|
\item{zebra_bg}{Character. Fill color for striped (even) body rows.
|
||||||
Default `"#f0f0eb"`.}
|
Default `civilytics_colors[["paper_2"]]`.}
|
||||||
|
|
||||||
\item{outer_border_color}{Character. Outer border color. Default
|
\item{outer_border_color}{Character. Outer border color. Default
|
||||||
`"#888888"`.}
|
`civilytics_colors[["rule_strong"]]`.}
|
||||||
|
|
||||||
\item{outer_border_width}{Numeric. Outer border width. Default `1`.}
|
\item{outer_border_width}{Numeric. Outer border width. Default `1`.}
|
||||||
|
|
||||||
\item{inner_border_color}{Character. Inner horizontal border color (body).
|
\item{inner_border_color}{Character. Inner horizontal border color (body).
|
||||||
Default `"#cccccc"`.}
|
Default `civilytics_colors[["rule"]]`.}
|
||||||
|
|
||||||
\item{inner_border_width}{Numeric. Inner horizontal border width. Default
|
\item{inner_border_width}{Numeric. Inner horizontal border width. Default
|
||||||
`0.5`.}
|
`0.5`.}
|
||||||
@@ -56,7 +57,8 @@ Default `"#cccccc"`.}
|
|||||||
\item{footer_fontsize}{Numeric. Footer font size (applied only if a footer
|
\item{footer_fontsize}{Numeric. Footer font size (applied only if a footer
|
||||||
part exists). Default `9`.}
|
part exists). Default `9`.}
|
||||||
|
|
||||||
\item{footer_color}{Character. Footer text color. Default `"#555555"`.}
|
\item{footer_color}{Character. Footer text color. Default
|
||||||
|
`civilytics_colors[["ink_3"]]`.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
The styled [flextable::flextable()] object.
|
The styled [flextable::flextable()] object.
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
# Project journal
|
||||||
|
|
||||||
|
Append-only, newest first. **Entries are never edited** — the value of this file is
|
||||||
|
that it records what was believed at the time, including the parts that turned out
|
||||||
|
wrong. Where things stand *today* is in `STATUS.md`, which is generated.
|
||||||
|
|
||||||
|
Four lines per entry. The analysis belongs in the issue or the decision record; this
|
||||||
|
file carries the reasoning and the pointers.
|
||||||
|
|
||||||
|
- **Why** — the driver. The one line git cannot reconstruct later.
|
||||||
|
- **Obligates** — issues this change created elsewhere. Numbers, not prose.
|
||||||
|
- **Refs** — commits, issues, decision records.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2026-10-15 · packaging · default branch renamed to main; add_logo_ga band and showtext switch
|
||||||
|
|
||||||
|
**Why:** `Closes` keywords only fire on the default branch, so the rename came first; #28 and #29 finished the logo work, and #29 took the opt-out switch because changing the showtext default would alter published reports we cannot see from here.
|
||||||
|
**Obligates:** none; turning showtext off by default is a future decision if consumers allow it
|
||||||
|
**Refs:** #28, #29, f3ec052, 119aa7a, 7bc547a
|
||||||
|
|
||||||
|
## 2026-10-15 · logo · logo band, patchwork panels, brand table colors, reveal print fixes
|
||||||
|
|
||||||
|
**Why:** figures with captions were shipping clipped and patchwork figures lost panels (#20); the table default and reveal print/column bugs were off-brand or broken in published decks. Default changes (font_scale 1, brand table colors) alter existing output, so this is 0.3.2.
|
||||||
|
**Obligates:** PDF check of #25/#27 in another project (never rendered here)
|
||||||
|
**Refs:** #20, #21, #22, #24, #25, #26, #27, 4f697e2, d6e3ba2, 0561df4, dfcad3d
|
||||||
|
|
||||||
|
## 2026-08-24 · packaging · roborev exclusion patterns fixed; init review triaged
|
||||||
|
|
||||||
|
**Why:** roborev reviewed the init commit because `excluded_commit_patterns` are
|
||||||
|
substring matches and `chore:` never matches the `chore(packaging):` form the
|
||||||
|
cadence rule mandates — the commit convention was silently defeating its own
|
||||||
|
review filter. Two low findings came back; one was half-right, one rested on a
|
||||||
|
generator it could not see.
|
||||||
|
**Obligates:** —
|
||||||
|
**Refs:** cd1af67, roborev job 15
|
||||||
|
|
||||||
|
## 2026-08-23 · packaging · compass initialised, five workstreams defined
|
||||||
|
|
||||||
|
**Why:** the package had no tracking at all — one open issue, a NEWS changelog, and
|
||||||
|
no record of why anything was decided. Workstream boundaries follow how the code
|
||||||
|
actually goes stale, not the directory listing: colors stay with theme because the
|
||||||
|
scales are consumed by the themes, and packaging is its own stream so CI and
|
||||||
|
dependency drift file against something.
|
||||||
|
**Obligates:** #21, #22
|
||||||
|
**Refs:** 7cad98b, #20
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
# Project status
|
||||||
|
|
||||||
|
> Between the compass markers is generated. Edit the sources, not this.
|
||||||
|
|
||||||
|
<!-- compass:begin -->
|
||||||
|
<!-- compass:board -->
|
||||||
|
|
||||||
|
## Where this stands
|
||||||
|
|
||||||
|
Every issue that was open is closed, and the default branch is `main`. The 0.3.2 changes are all pushed: the logo and `add_logo_ga()` reserve their own band so captions are not clipped, patchwork figures keep every panel, flextable defaults use brand colors, and `civilytics_load_fonts()` has a switch for ragg users who do not want showtext. The showtext default is unchanged, so turning it off by default is a separate decision that depends on which reports rely on it. The reveal print fixes still need a look in a rendered PDF, which another project will do.
|
||||||
|
|
||||||
|
## Ready to work on next
|
||||||
|
|
||||||
|
_Nothing is open and unblocked._
|
||||||
|
|
||||||
|
## Workstreams
|
||||||
|
|
||||||
|
| Stream | Commits since | Open | Debt | Owes docs |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| Themes, palettes, and fonts | 1 | 0 | 0 | no |
|
||||||
|
| Logo and branded output composition | 1 | 0 | 0 | no |
|
||||||
|
| Quarto themes and publishing templates | 0 | 0 | 0 | no |
|
||||||
|
| Analysis and workflow helpers | 0 | 0 | 0 | no |
|
||||||
|
| Package infrastructure and release | 2 | 0 | 0 | no |
|
||||||
|
|
||||||
|
## CI
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Dependency graph and detail</summary>
|
||||||
|
|
||||||
|
_Nothing blocks anything else, so there is no graph to draw._
|
||||||
|
|
||||||
|
- Marker: `c16e6cec` (2026-10-15)
|
||||||
|
- Commits since: 4
|
||||||
|
- Open issues: 0
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<!-- compass:end -->
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
[project]
|
||||||
|
name = "civilytics"
|
||||||
|
forge = "Civilytics/civilyticsR"
|
||||||
|
|
||||||
|
[[workstream]]
|
||||||
|
id = "theme"
|
||||||
|
title = "Themes, palettes, and fonts"
|
||||||
|
paths = [
|
||||||
|
"R/theme.R",
|
||||||
|
"R/colors.R",
|
||||||
|
"R/fonts.R",
|
||||||
|
"tests/testthat/test_theme.R",
|
||||||
|
]
|
||||||
|
docs = [
|
||||||
|
"man/theme_civilytics*.Rd",
|
||||||
|
"man/scale_color_civilytics.Rd",
|
||||||
|
"man/scale_fill_civilytics.Rd",
|
||||||
|
"man/civilytics_pal*.Rd",
|
||||||
|
"man/civilytics_colors.Rd",
|
||||||
|
"man/civilytics_load_fonts.Rd",
|
||||||
|
"README.Rmd",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[workstream]]
|
||||||
|
id = "logo"
|
||||||
|
title = "Logo and branded output composition"
|
||||||
|
paths = [
|
||||||
|
"R/logo.R",
|
||||||
|
"R/flextable.R",
|
||||||
|
"inst/img/**",
|
||||||
|
"tests/testthat/test_logo.R",
|
||||||
|
"tests/testthat/test_flextable.R",
|
||||||
|
]
|
||||||
|
docs = [
|
||||||
|
"man/*logo*.Rd",
|
||||||
|
"man/*flextable*.Rd",
|
||||||
|
"man/plot_jpeg.Rd",
|
||||||
|
"man/get_png.Rd",
|
||||||
|
"man/has_caption.Rd",
|
||||||
|
"man/measure_caption.Rd",
|
||||||
|
"README.Rmd",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[workstream]]
|
||||||
|
id = "quarto"
|
||||||
|
title = "Quarto themes and publishing templates"
|
||||||
|
paths = [
|
||||||
|
"R/quarto.R",
|
||||||
|
"inst/quarto/**",
|
||||||
|
]
|
||||||
|
docs = [
|
||||||
|
"man/use_civilytics_*.Rd",
|
||||||
|
"man/quarto-helpers.Rd",
|
||||||
|
"inst/quarto/examples/*.qmd",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[workstream]]
|
||||||
|
id = "helpers"
|
||||||
|
title = "Analysis and workflow helpers"
|
||||||
|
paths = [
|
||||||
|
"R/utils.R",
|
||||||
|
"R/prop_conf.R",
|
||||||
|
"R/join_utilities.R",
|
||||||
|
"R/db.R",
|
||||||
|
"R/notifications.R",
|
||||||
|
"tests/testthat/test_utils.R",
|
||||||
|
"tests/testthat/test_propint.R",
|
||||||
|
"tests/testthat/test_joins.R",
|
||||||
|
"tests/testthat/test_db.R",
|
||||||
|
"tests/testthat/test_notifications.R",
|
||||||
|
]
|
||||||
|
docs = [
|
||||||
|
"README.Rmd",
|
||||||
|
"NEWS.md",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[workstream]]
|
||||||
|
id = "packaging"
|
||||||
|
title = "Package infrastructure and release"
|
||||||
|
paths = [
|
||||||
|
"DESCRIPTION",
|
||||||
|
"NAMESPACE",
|
||||||
|
"Makefile",
|
||||||
|
"Dockerfile",
|
||||||
|
".Rbuildignore",
|
||||||
|
".gitea/workflows/**",
|
||||||
|
"tests/testthat.R",
|
||||||
|
"AGENTS.md",
|
||||||
|
".roborev.toml",
|
||||||
|
]
|
||||||
|
docs = [
|
||||||
|
"NEWS.md",
|
||||||
|
"README.Rmd",
|
||||||
|
]
|
||||||
|
|
||||||
|
[roborev]
|
||||||
|
project_guidelines = [
|
||||||
|
"Brand colors come from `civilytics_colors`; never write a hex literal in theme, scale, logo, or table code.",
|
||||||
|
"No tidyverse dependency. Imports is base R plus ggplot2, grid/gridExtra, png/jpeg, stringr, stringdist, showtext/sysfonts, jsonlite. Reject dplyr, purrr, magrittr, tibble, and data.table; use base R idioms.",
|
||||||
|
"NAMESPACE is roxygen-generated. Declare imports with `@importFrom` in the roxygen block and re-run roxygen; never hand-edit NAMESPACE.",
|
||||||
|
"Themes default to a transparent background (`paper_bg = FALSE`) so plots composite onto any Quarto, Reveal, or Typst background. Making an opaque background the default is a regression, not a preference.",
|
||||||
|
"Theme functions thread `ink`/`paper`/`accent` into ggplot2 4.0's base-theme parameters rather than setting element colors ad hoc. ggplot2 >= 4.0 is a hard dependency, so use S7 `@` property access on plot and theme objects, not `$`.",
|
||||||
|
"User-facing progress goes through `message()` so callers can suppress it. No `cat()` or `print()` in package code.",
|
||||||
|
"Nothing personal or client-identifying is vendored into `inst/` -- brand assets only. Version 0.3.2 removed headshots for exactly this reason.",
|
||||||
|
"The camelCase exports (`countCleanr`, `dbSafeNames`, `simpleCap`, `waldInterval`, `countDots`, `countNA`, `findDots`, `nvals`) are frozen public API; do not rename them. New functions are snake_case.",
|
||||||
|
"`R/theme.R` and `R/logo.R` are long by design -- one file per surface, with dense roxygen. File length is not a finding in this package; flag a single function over roughly 80 lines instead.",
|
||||||
|
]
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# Decisions
|
||||||
|
|
||||||
|
One file per decision, numbered and immutable. A decision that changes is superseded
|
||||||
|
by a new record, never edited in place — the old reasoning is the point.
|
||||||
|
|
||||||
|
The table below is **generated** by `compass:decide`. Do not hand-edit it.
|
||||||
|
|
||||||
|
<!-- compass:begin decisions -->
|
||||||
|
| # | Date | Decision | Status |
|
||||||
|
|---|---|---|---|
|
||||||
|
| — | — | *No decisions recorded yet.* | — |
|
||||||
|
<!-- compass:end decisions -->
|
||||||
Binary file not shown.
@@ -87,3 +87,21 @@ test_that("extra_height increases the exported PNG height", {
|
|||||||
|
|
||||||
expect_gt(dim(png::readPNG(path_big))[1], dim(png::readPNG(path_small))[1])
|
expect_gt(dim(png::readPNG(path_big))[1], dim(png::readPNG(path_small))[1])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test_that("style_flextable_civilytics defaults come from civilytics_colors (#21)", {
|
||||||
|
defaults <- formals(style_flextable_civilytics)
|
||||||
|
expect_identical(eval(defaults$header_bg), civilytics_colors[["navy_700"]])
|
||||||
|
expect_identical(eval(defaults$zebra_bg), civilytics_colors[["paper_2"]])
|
||||||
|
expect_identical(eval(defaults$outer_border_color), civilytics_colors[["rule_strong"]])
|
||||||
|
expect_identical(eval(defaults$inner_border_color), civilytics_colors[["rule"]])
|
||||||
|
expect_identical(eval(defaults$footer_color), civilytics_colors[["ink_3"]])
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("style_flextable_civilytics paints the header with brand navy", {
|
||||||
|
skip_if_not_installed("flextable")
|
||||||
|
skip_if_not_installed("officer")
|
||||||
|
|
||||||
|
styled <- style_flextable_civilytics(flextable::flextable(head(mtcars, 3)))
|
||||||
|
header_fill <- unique(as.vector(styled$header$styles$cells$background.color$data))
|
||||||
|
expect_identical(toupper(header_fill), toupper(civilytics_colors[["navy_700"]]))
|
||||||
|
})
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
# civilytics_load_fonts() must not touch the network or global devices in tests:
|
||||||
|
# both sysfonts::font_add_google() and showtext::showtext_auto() are mocked.
|
||||||
|
with_font_mocks <- function(code, env = parent.frame()) {
|
||||||
|
calls <- new.env()
|
||||||
|
calls$showtext <- 0L
|
||||||
|
calls$google <- character()
|
||||||
|
old <- .cv_state$fonts_loaded
|
||||||
|
.cv_state$fonts_loaded <- FALSE
|
||||||
|
withr::defer(.cv_state$fonts_loaded <- old, envir = env)
|
||||||
|
testthat::local_mocked_bindings(
|
||||||
|
font_add_google = function(name, family = name, ...) {
|
||||||
|
calls$google <- c(calls$google, family)
|
||||||
|
},
|
||||||
|
.package = "sysfonts", .env = env
|
||||||
|
)
|
||||||
|
testthat::local_mocked_bindings(
|
||||||
|
showtext_auto = function(enable = TRUE) calls$showtext <- calls$showtext + 1L,
|
||||||
|
.package = "showtext", .env = env
|
||||||
|
)
|
||||||
|
calls
|
||||||
|
}
|
||||||
|
|
||||||
|
test_that("civilytics_load_fonts turns showtext on by default (#29)", {
|
||||||
|
skip_if_not_installed("withr")
|
||||||
|
calls <- with_font_mocks()
|
||||||
|
expect_true(civilytics_load_fonts())
|
||||||
|
expect_length(calls$google, 4)
|
||||||
|
expect_equal(calls$showtext, 1L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("showtext = FALSE registers fonts but leaves showtext off (#29)", {
|
||||||
|
skip_if_not_installed("withr")
|
||||||
|
calls <- with_font_mocks()
|
||||||
|
expect_true(civilytics_load_fonts(showtext = FALSE))
|
||||||
|
expect_length(calls$google, 4)
|
||||||
|
expect_equal(calls$showtext, 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("options(civilytics.showtext = FALSE) sets the default (#29)", {
|
||||||
|
skip_if_not_installed("withr")
|
||||||
|
calls <- with_font_mocks()
|
||||||
|
withr::local_options(civilytics.showtext = FALSE)
|
||||||
|
civilytics_load_fonts()
|
||||||
|
expect_equal(calls$showtext, 0L)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("showtext must be a single TRUE or FALSE", {
|
||||||
|
expect_error(civilytics_load_fonts(showtext = NA), "showtext")
|
||||||
|
expect_error(civilytics_load_fonts(showtext = c(TRUE, FALSE)), "showtext")
|
||||||
|
})
|
||||||
@@ -28,3 +28,111 @@ test_that("stamp_logo_png validates its inputs", {
|
|||||||
# invalid type is rejected by match.arg() before the file is touched
|
# invalid type is rejected by match.arg() before the file is touched
|
||||||
expect_error(stamp_logo_png(tempfile(fileext = ".png"), type = "banner"))
|
expect_error(stamp_logo_png(tempfile(fileext = ".png"), type = "banner"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# --- civilytics_logo() layout (#20) ------------------------------------------
|
||||||
|
|
||||||
|
caption_plot <- function() {
|
||||||
|
ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) +
|
||||||
|
ggplot2::geom_point() +
|
||||||
|
ggplot2::labs(title = "Title",
|
||||||
|
caption = "Source: a reasonably long attribution line, 2021-22\nSecond line") +
|
||||||
|
theme_civilytics()
|
||||||
|
}
|
||||||
|
|
||||||
|
# The composed grob is a gtable, wrapped in a grobTree when the plot has a
|
||||||
|
# background fill.
|
||||||
|
composed_table <- function(composed) {
|
||||||
|
if (inherits(composed, "gtable")) composed else composed$children[[2]]
|
||||||
|
}
|
||||||
|
|
||||||
|
bottom_ink <- function(grob, width, height, rows = 2) {
|
||||||
|
path <- tempfile(fileext = ".png")
|
||||||
|
grDevices::png(path, width = width, height = height, units = "in", res = 100)
|
||||||
|
grid::grid.newpage()
|
||||||
|
grid::grid.draw(grob)
|
||||||
|
grDevices::dev.off()
|
||||||
|
img <- png::readPNG(path)
|
||||||
|
n <- dim(img)[1]
|
||||||
|
edge <- img[(n - rows + 1):n, , 1:3, drop = FALSE]
|
||||||
|
sum(edge < 0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
test_that("font_scale defaults to 1 everywhere", {
|
||||||
|
expect_identical(formals(add_logo)$font_scale, 1)
|
||||||
|
expect_identical(formals(add_logo_ga)$font_scale, 1)
|
||||||
|
expect_identical(formals(civilytics_logo)$font_scale, 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("the logo band is reserved space that sums to the canvas", {
|
||||||
|
p <- caption_plot()
|
||||||
|
for (pos in c("bottom-right", "top-left")) {
|
||||||
|
composed <- suppressWarnings(civilytics_logo(p, position = pos, logo_height = 0.6))
|
||||||
|
heights <- composed_table(composed)$heights
|
||||||
|
expect_equal(length(heights), 2)
|
||||||
|
expect_true(any(grepl("null", as.character(heights))))
|
||||||
|
expect_true(any(grepl("0.6", as.character(heights))))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("a caption is not clipped at the bottom edge at several canvas sizes", {
|
||||||
|
skip_on_cran()
|
||||||
|
p <- caption_plot()
|
||||||
|
for (w in c(8, 11, 16, 24)) {
|
||||||
|
h <- w * 0.64
|
||||||
|
ink <- bottom_ink(suppressWarnings(civilytics_logo(p, position = "top-right")), w, h)
|
||||||
|
expect_equal(ink, 0, info = paste("width", w))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("patchwork compositions keep every panel", {
|
||||||
|
skip_if_not_installed("patchwork")
|
||||||
|
p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point()
|
||||||
|
p2 <- ggplot2::ggplot(mtcars, ggplot2::aes(hp, wt)) + ggplot2::geom_point()
|
||||||
|
pw <- p1 + p2 + patchwork::plot_annotation(title = "Both panels")
|
||||||
|
composed <- suppressWarnings(civilytics_logo(pw))
|
||||||
|
panel_grob <- composed_table(composed)$grobs[[1]]
|
||||||
|
n_panels <- sum(grepl("^panel", panel_grob$layout$name))
|
||||||
|
expect_gte(n_panels, 2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("the logo keeps its aspect ratio at any canvas width (#20)", {
|
||||||
|
skip_if_not_installed("ragg")
|
||||||
|
ink_aspect <- function(width, type) {
|
||||||
|
path <- tempfile(fileext = ".png")
|
||||||
|
ragg::agg_png(path, width = width, height = 0.5, units = "in", res = 200,
|
||||||
|
background = "transparent")
|
||||||
|
grid::grid.draw(ggplot2::ggplotGrob(make_logo_grob(type = type)))
|
||||||
|
grDevices::dev.off()
|
||||||
|
ink <- which(png::readPNG(path)[, , 4] > 0.05, arr.ind = TRUE)
|
||||||
|
diff(range(ink[, 2])) / diff(range(ink[, 1]))
|
||||||
|
}
|
||||||
|
for (type in c("wordmark", "mark")) {
|
||||||
|
aspects <- vapply(c(6, 11, 24), ink_aspect, numeric(1), type = type)
|
||||||
|
expect_lt(diff(range(aspects)) / mean(aspects), 0.03, label = type)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
# --- add_logo_ga() layout (#28) ----------------------------------------------
|
||||||
|
|
||||||
|
test_that("add_logo_ga reserves a logo band instead of using negative margins", {
|
||||||
|
logo <- make_logo_grob()
|
||||||
|
composed <- add_logo_ga(list(caption_plot(), caption_plot()), logo, logo_height = 0.6)
|
||||||
|
heights <- composed_table(composed)$heights
|
||||||
|
expect_equal(length(heights), 2)
|
||||||
|
expect_true(any(grepl("null", as.character(heights))))
|
||||||
|
expect_true(any(grepl("0.6", as.character(heights))))
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("add_logo_ga does not clip captions at several canvas sizes", {
|
||||||
|
skip_on_cran()
|
||||||
|
logo <- make_logo_grob()
|
||||||
|
for (nrow in c(1, 2)) {
|
||||||
|
for (w in c(8, 11, 16, 24)) {
|
||||||
|
composed <- suppressWarnings(
|
||||||
|
add_logo_ga(list(caption_plot(), caption_plot()), logo, nrow = nrow)
|
||||||
|
)
|
||||||
|
ink <- bottom_ink(composed, w, w * 0.64)
|
||||||
|
expect_equal(ink, 0, info = paste("nrow", nrow, "width", w))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in new issue
Block a user