Skip to content

Robustness fixes for the R package (#318): version/input validation, case-insensitive variables, year filtering, httr2#7

Open
Yicheng-Zhao1999 wants to merge 2 commits into
KMueller-Lab:mainfrom
Yicheng-Zhao1999:fix/robustness-318
Open

Robustness fixes for the R package (#318): version/input validation, case-insensitive variables, year filtering, httr2#7
Yicheng-Zhao1999 wants to merge 2 commits into
KMueller-Lab:mainfrom
Yicheng-Zhao1999:fix/robustness-318

Conversation

@Yicheng-Zhao1999

Copy link
Copy Markdown

Summary

Robustness fixes for the R globalmacrodata package from the cross-language audit (#318; Stata #412, Python forthcoming). All changes were live-verified with R 4.5.3 against the live S3 backend; R CMD check is Status: OK (0 ERROR/WARNING/NOTE).

Two commits:

  1. Robustness fixes Update and fix the raw option bug #2Robustness fixes for the R package (#318): version/input validation, case-insensitive variables, year filtering, httr2 #7 + partial 20260616/#413 double check errors in r #8 — the bug fixes, tests, and safe quality cleanup.
  2. Migrate httr → httr2 — kept as a separate, revertible commit.

Bug fixes

# Issue Fix
1 Bundled inst/versions.csv out of sync with S3 Synced to all 9 served vintages (added 2025_05, 2025_08). Re-verified: all 9 .dta now return HTTP 200. Also added a clearer message when a listed version's file can't be retrieved.
2 gmd(version=NA) → cryptic "missing value where TRUE/FALSE needed" Validate version is a single non-NA character (or NULL) before any download.
3 Doubled Error: prefix (stop() already adds one) Stripped the literal Error: prefix from all stop()/sprintf strings.
4 iso=TRUE/vars=TRUE + other args silently discarded the args (warning only) Now stop()s; also covers sources/cite.
5 variables case-sensitive while country/sources are not Case-insensitive matching, normalized to canonical casing (e.g. rgdprGDP).
6 Empty character vectors downloaded the full ~21 MB .dta before failing Reject empty country/variables before fetching.
7 No start_year/end_year filtering Added both params + an apply_year_filter() helper applied across the sources/raw/main paths.

Quality cleanup (#8)

  • DESCRIPTION: added Depends: R (>= 3.5.0) and version floors (readr (>= 2.0.0) is required by show_col_types/col_types; haven (>= 2.0.0)).
  • HTTP-status logging in .gmd_safe_get so 404 vs timeout are distinguishable (speculative case-insensitive source lookups stay quiet).
  • col_types on the fixed-schema CSV reads (versions/varlist/isomapping/source_list/bib), pinning only accessed columns so an extra S3 column is fine; the dynamic raw-variable CSV intentionally keeps inference.
  • sources path now has the same zero-row guard as the main/raw paths.
  • httr → httr2 migration: .gmd_safe_get uses httr2::request/req_perform with req_error() disabled to preserve the original "non-200 → NULL" behavior; content(as="raw")resp_body_raw(), content(as="text")resp_body_string(encoding="UTF-8").

Not done (deferred): no other #8 items remain beyond the above.

Tests

Added 7 testthat tests (5 offline validation + 2 online behavior) covering #2#7, and retargeted the "specific version" test to a pinned older vintage now that S3 serves them.

Verification

  • R CMD check: Status OK (suggests forced off where testthat isn't installed locally).
  • Live repros of every bug fix pass against the S3 backend (all 9 versions load; case-insensitive variables; year-window filtering; httr2 network paths incl. .dta and CSV bodies; 404 status logging).
  • Independent adversarial diff-audit + completeness reviews: no collateral changes; each fix has code + test.

🤖 Generated with Claude Code

Yicheng-Zhao1999 and others added 2 commits June 16, 2026 14:03
… (floors, status logging, col_types, sources guard)

Fixes from issue #318 robustness audit:
- KMueller-Lab#2 validate version is single non-NA string before any download
- KMueller-Lab#3 strip doubled 'Error:' prefixes from stop() messages
- KMueller-Lab#4 iso/vars + other inputs now errors instead of silently discarding
- KMueller-Lab#5 case-insensitive variable matching, normalized to canonical casing
- KMueller-Lab#6 reject empty character vectors before fetching the dataset
- KMueller-Lab#7 add start_year/end_year filtering across sources/raw/main paths
- KMueller-Lab#1 sync bundled versions.csv to the 9 served vintages
- KMueller-Lab#8 DESCRIPTION dependency floors, HTTP-status logging in .gmd_safe_get,
     col_types on fixed-schema CSV reads, sources-path zero-row guard

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Replace the superseded httr package with httr2 (issue #318, item KMueller-Lab#8):
- .gmd_safe_get now uses httr2::request/req_perform with req_error() disabled
  so non-200 responses return NULL (with status logging) exactly as before,
  while genuine network failures are still caught and return NULL.
- httr::content(as='raw')  -> httr2::resp_body_raw()  (haven::read_dta of .dta)
- httr::content(as='text') -> httr2::resp_body_string(encoding='UTF-8') (read_csv)
- DESCRIPTION: httr (>= 1.4.0) -> httr2 (>= 1.0.0)

Behavior-preserving: all network paths and bug-fix repros verified against the
live S3 backend; R CMD check Status OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Yicheng-Zhao1999 added a commit to Yicheng-Zhao1999/Global-Macro-Database-R that referenced this pull request Jun 29, 2026
- Trim whitespace consistently with Python/Stata; blank tokens dropped and an
  empty/all-blank value becomes NULL ("no filter") instead of erroring (KMueller-Lab#4)
- Validate an invalid version before honoring cite (KMueller-Lab#7)
- Add print_option (APA citation) for parity with Python/Stata
- Add fast (local dataset cache under R_user_dir) for parity with Python/Stata

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant