feat: package skeleton — DESCRIPTION, NAMESPACE, session/manifest/cache/views
Minimal skeleton for uscogdata v0.1. Internal session layer with lazy cog_open(), manifest fetch+validate+cache, view registration placeholder. Depends on DuckDB >=1.0, httr2, jsonlite. inst/schemas/provenance-v1.json ships the structured provenance JSON Schema.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
^.*\.Rproj$
|
||||
^\.Rproj\.user$
|
||||
^_pkgdown\.yml$
|
||||
^docs$
|
||||
^pkgdown$
|
||||
^\.github$
|
||||
^LICENSE\.md$
|
||||
^\.git$
|
||||
^\.gitignore$
|
||||
\.gitkeep$
|
||||
^vignettes$
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
.Rproj.user
|
||||
.Rhistory
|
||||
.RData
|
||||
.Ruserdata
|
||||
*.Rproj
|
||||
inst/doc
|
||||
docs/
|
||||
/doc/
|
||||
/Meta/
|
||||
.DS_Store
|
||||
/.quarto/
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
Package: uscogdata
|
||||
Type: Package
|
||||
Title: Curated Reader for the Civilytics US Census of Governments Finance Corpus
|
||||
Version: 0.1.0
|
||||
Authors@R:
|
||||
person("Civilytics", , , "jknowles@gmail.com", role = c("aut", "cre"))
|
||||
Description: Curated R verbs over the Civilytics US Census of Governments
|
||||
finance corpus. Provides unit-level financial profiles, geographic
|
||||
rollups, and peer comparisons with auditable provenance and built-in
|
||||
cross-vintage correctness.
|
||||
License: MIT + file LICENSE
|
||||
Encoding: UTF-8
|
||||
LazyData: false
|
||||
Depends: R (>= 4.1)
|
||||
Imports:
|
||||
DBI (>= 1.1.0),
|
||||
dbplyr (>= 2.4.0),
|
||||
duckdb (>= 1.0.0),
|
||||
dplyr (>= 1.1.0),
|
||||
tibble,
|
||||
cli,
|
||||
jsonlite,
|
||||
httr2,
|
||||
digest
|
||||
Suggests:
|
||||
testthat (>= 3.0.0),
|
||||
withr,
|
||||
knitr,
|
||||
rmarkdown,
|
||||
pkgdown,
|
||||
ggplot2
|
||||
Config/testthat/edition: 3
|
||||
VignetteBuilder: knitr
|
||||
RoxygenNote: 7.3.3
|
||||
MinCorpusSchema: 2
|
||||
MaxCorpusSchema: 2
|
||||
@@ -0,0 +1,6 @@
|
||||
# R/cache.R
|
||||
# Local partition cache; SHA-based invalidation.
|
||||
# Phase N v0.1 implementation: DuckDB httpfs handles actual reads directly
|
||||
# from Nextcloud; cache_dir holds only manifest.json. Richer partition
|
||||
# caching (pre-fetch hot partitions) is a v0.2 feature.
|
||||
# Stub here for cog_mirror to compose against.
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# R/config.R
|
||||
|
||||
#' Package-private mutable state
|
||||
#' @noRd
|
||||
.uscogdata_env <- new.env(parent = emptyenv())
|
||||
|
||||
.uscogdata_defaults <- list(
|
||||
url = "https://cloud.civilytics.org/s/REPLACE_WITH_SHARE_TOKEN/download/",
|
||||
cache_dir = NULL,
|
||||
manifest_ttl_secs = 3600L
|
||||
)
|
||||
|
||||
#' Resolve a config value: env var > option > default
|
||||
#' @noRd
|
||||
.cfg <- function(key) {
|
||||
env_var <- paste0("USCOGDATA_", toupper(key))
|
||||
v <- Sys.getenv(env_var, unset = NA)
|
||||
if (!is.na(v) && nzchar(v)) return(v)
|
||||
opt <- getOption(paste0("uscogdata.", key), default = NULL)
|
||||
if (!is.null(opt)) return(opt)
|
||||
.uscogdata_defaults[[key]]
|
||||
}
|
||||
|
||||
.resolve_url <- function() .cfg("url")
|
||||
|
||||
.resolve_cache_dir <- function() {
|
||||
v <- .cfg("cache_dir")
|
||||
if (is.null(v)) tools::R_user_dir("uscogdata", "cache") else v
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
# R/manifest.R
|
||||
|
||||
#' Fetch manifest.json from URL, cache locally, validate TTL.
|
||||
#' @noRd
|
||||
.fetch_or_cache_manifest <- function(url, cache_dir) {
|
||||
cache_path <- file.path(cache_dir, "manifest.json")
|
||||
ttl <- as.integer(.cfg("manifest_ttl_secs"))
|
||||
|
||||
needs_fetch <- !file.exists(cache_path) ||
|
||||
difftime(Sys.time(), file.info(cache_path)$mtime, units = "secs") > ttl
|
||||
|
||||
if (needs_fetch) {
|
||||
resp <- httr2::request(paste0(url, "manifest.json")) |>
|
||||
httr2::req_error(is_error = function(r) httr2::resp_status(r) >= 400) |>
|
||||
httr2::req_perform()
|
||||
writeLines(httr2::resp_body_string(resp), cache_path)
|
||||
}
|
||||
|
||||
jsonlite::fromJSON(cache_path, simplifyVector = FALSE)
|
||||
}
|
||||
|
||||
#' @noRd
|
||||
.validate_schema <- function(manifest, expected_version) {
|
||||
if (manifest$schema_version != expected_version) {
|
||||
cli::cli_abort(c(
|
||||
"Corpus schema version mismatch.",
|
||||
x = "Package expects schema_version = {expected_version}; corpus has {manifest$schema_version}.",
|
||||
i = "Update uscogdata (install.packages or pak::pkg_install) or re-publish corpus."
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#' @noRd
|
||||
.validate_scope <- function(manifest) {
|
||||
included <- manifest$scope$gov_types_included
|
||||
.uscogdata_env$scope_included <- included
|
||||
invisible(NULL)
|
||||
}
|
||||
|
||||
`%||%` <- function(a, b) if (is.null(a) || (length(a) == 1 && is.na(a))) b else a
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
# R/session.R
|
||||
|
||||
#' Internal: open session, register views, cache manifest.
|
||||
#' Not exported. Called lazily by verbs via .ensure_session().
|
||||
#' @noRd
|
||||
cog_open <- function(url = .resolve_url(),
|
||||
cache_dir = .resolve_cache_dir()) {
|
||||
if (!dir.exists(cache_dir)) dir.create(cache_dir, recursive = TRUE)
|
||||
|
||||
con <- DBI::dbConnect(duckdb::duckdb())
|
||||
DBI::dbExecute(con, "INSTALL httpfs; LOAD httpfs;")
|
||||
|
||||
manifest <- .fetch_or_cache_manifest(url, cache_dir)
|
||||
.validate_schema(manifest, expected_version = 2L)
|
||||
.validate_scope(manifest)
|
||||
|
||||
.register_views(con, url, manifest)
|
||||
|
||||
.uscogdata_env$con <- con
|
||||
.uscogdata_env$manifest <- manifest
|
||||
.uscogdata_env$url <- url
|
||||
.uscogdata_env$cache_dir <- cache_dir
|
||||
|
||||
invisible(con)
|
||||
}
|
||||
|
||||
#' @noRd
|
||||
.ensure_session <- function() {
|
||||
if (is.null(.uscogdata_env$con) ||
|
||||
!DBI::dbIsValid(.uscogdata_env$con)) {
|
||||
cog_open()
|
||||
}
|
||||
.uscogdata_env$con
|
||||
}
|
||||
|
||||
#' @noRd
|
||||
cog_close <- function() {
|
||||
if (!is.null(.uscogdata_env$con) && DBI::dbIsValid(.uscogdata_env$con)) {
|
||||
DBI::dbDisconnect(.uscogdata_env$con, shutdown = TRUE)
|
||||
}
|
||||
.uscogdata_env$con <- NULL
|
||||
.uscogdata_env$manifest <- NULL
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
# R/views.R
|
||||
|
||||
#' Register DuckDB views from inst/sql/ SQL files
|
||||
#' @noRd
|
||||
.register_views <- function(con, url, manifest) {
|
||||
sql_dir <- system.file("sql", package = "uscogdata")
|
||||
files <- list.files(sql_dir, pattern = "\\.sql$", full.names = TRUE)
|
||||
for (f in files) {
|
||||
sql <- paste(readLines(f, warn = FALSE), collapse = "\n")
|
||||
sql <- gsub("\\{url\\}", url, sql, fixed = FALSE)
|
||||
DBI::dbExecute(con, sql)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# R/zzz.R
|
||||
|
||||
.onLoad <- function(libname, pkgname) {
|
||||
invisible(NULL)
|
||||
}
|
||||
|
||||
.onUnload <- function(libpath) {
|
||||
cog_close()
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
# uscogdata
|
||||
|
||||
Curated R reader for the Civilytics US Census of Governments finance corpus.
|
||||
|
||||
Provides unit-level financial profiles, geographic rollups, and peer comparisons
|
||||
with auditable provenance and built-in cross-vintage correctness. Reads the
|
||||
published corpus (Hive-partitioned parquet + manifest.json) directly from
|
||||
Nextcloud via DuckDB httpfs — no local bulk downloads required.
|
||||
|
||||
## Status
|
||||
|
||||
Under active development (Phase 2 of the cog_pipeline project). See
|
||||
`../cog_pipeline/docs/reader-specification.md` for the reader contract this
|
||||
package implements.
|
||||
|
||||
## Installation
|
||||
|
||||
```r
|
||||
# pak::pkg_install("gitea.civilytics.org/Civilytics/uscogdata")
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
- `USCOGDATA_URL` — corpus root URL (public Nextcloud share, trailing slash)
|
||||
- `USCOGDATA_CACHE_DIR` — optional override for the manifest cache directory
|
||||
- `USCOGDATA_MANIFEST_TTL_SECS` — optional manifest re-fetch TTL (default 3600)
|
||||
@@ -0,0 +1,13 @@
|
||||
url: ~
|
||||
template:
|
||||
bootstrap: 5
|
||||
|
||||
reference:
|
||||
- title: Session
|
||||
contents:
|
||||
- has_keyword("internal")
|
||||
|
||||
articles:
|
||||
- title: Getting started
|
||||
navbar: ~
|
||||
contents: []
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://civilytics.org/schemas/uscogdata/provenance-v1.json",
|
||||
"title": "uscogdata provenance v1",
|
||||
"type": "object",
|
||||
"required": ["verb", "target", "years", "scope", "manifest", "sql_query"],
|
||||
"properties": {
|
||||
"verb": { "type": "string" },
|
||||
"call": { "type": "string" },
|
||||
"target": { "type": "object" },
|
||||
"years": { "type": "array", "items": { "type": "integer" } },
|
||||
"category": { "type": ["string", "array", "null"] },
|
||||
"scope": { "type": "object" },
|
||||
"codes_summed": { "type": "object" },
|
||||
"aggregate_fallback": { "type": ["object", "null"] },
|
||||
"transformations":{ "type": "object" },
|
||||
"series_break_refs": { "type": "array", "items": { "type": "string" } },
|
||||
"manifest": { "type": "object" },
|
||||
"sql_query": { "type": "string" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
library(testthat)
|
||||
library(uscogdata)
|
||||
|
||||
test_check("uscogdata")
|
||||
@@ -0,0 +1,6 @@
|
||||
# tests/testthat/helper-fixture.R
|
||||
skip_if_no_corpus <- function() {
|
||||
testthat::skip_if(Sys.getenv("USCOGDATA_FIXTURE_URL", "") == "" &&
|
||||
!file.exists("~/.cache/R/uscogdata/manifest.json"),
|
||||
"No fixture corpus available")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# tests/testthat/setup.R
|
||||
# Point tests at a fixture corpus URL if provided.
|
||||
if (Sys.getenv("USCOGDATA_FIXTURE_URL", "") != "") {
|
||||
options(uscogdata.url = Sys.getenv("USCOGDATA_FIXTURE_URL"))
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
test_that(".cfg resolves defaults, options, and env vars in priority order", {
|
||||
withr::with_options(list(uscogdata.manifest_ttl_secs = NULL), {
|
||||
withr::with_envvar(c(USCOGDATA_MANIFEST_TTL_SECS = NA), {
|
||||
expect_equal(uscogdata:::.cfg("manifest_ttl_secs"), 3600L)
|
||||
})
|
||||
})
|
||||
|
||||
withr::with_options(list(uscogdata.url = "https://opt.example/"), {
|
||||
withr::with_envvar(c(USCOGDATA_URL = NA), {
|
||||
expect_equal(uscogdata:::.cfg("url"), "https://opt.example/")
|
||||
})
|
||||
})
|
||||
|
||||
withr::with_envvar(c(USCOGDATA_URL = "https://env.example/"), {
|
||||
withr::with_options(list(uscogdata.url = "https://opt.example/"), {
|
||||
expect_equal(uscogdata:::.cfg("url"), "https://env.example/")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test_that(".resolve_cache_dir falls back to R_user_dir", {
|
||||
withr::with_envvar(c(USCOGDATA_CACHE_DIR = NA), {
|
||||
withr::with_options(list(uscogdata.cache_dir = NULL), {
|
||||
expect_equal(uscogdata:::.resolve_cache_dir(),
|
||||
tools::R_user_dir("uscogdata", "cache"))
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user