This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
What this is
rb3 is an R package (rOpenSci, CRAN) that downloads public market data from B3 (Brazilian exchange), parses it, and stores it as Arrow/Parquet datasets in a local cache. Package version 0.1.0 is a full rewrite around a YAML template framework; legacy/ holds the old code and is excluded from the build.
Commands
All development goes through devtools from the repo root:
devtools::load_all() # load package for interactive work
devtools::document() # regenerate NAMESPACE and man/ from roxygen (run after changing exports/docs)
devtools::test() # run all tests
testthat::test_file("tests/testthat/test-futures.R") # run one test file
devtools::check() # full R CMD check; aim for 0 errors/warnings (CI runs this on 5 platforms)Shell equivalents: Rscript -e 'devtools::test()', etc.
Docs and site (Makefile):
make # README.R -> README.RData -> README.md (README.md is generated; edit README.Rmd)
make site # pkgdown site
make vignettes # runs vignettes/save_*.R to build data_*.RData, then renders the .Rmd filesTest setup gotchas
-
.RenvironsetsNOT_CRAN=true, soskip_on_cran()does not skip locally. Most test files hit the B3 website and are guarded byskip_if_offline(). Some usevcrcassettes intests/fixtures/. -
tests/testthat/setup.Rpointsrb3.cachedirat a temp folder, silencescli, and loads extra test-only templates fromtests/testthat/testdata/*.yamlinto the same registry as the built-in ones. - Test helpers:
helper-help.R(copy_file_to_temp) andhelper-vcr.R.
Architecture
Template-driven pipeline
Every data source is a YAML file in inst/extdata/templates/ (id, downloader, reader, writers, fields). At library(rb3) (.onAttach in R/zzz.R), load_template_files() parses each YAML into a template object and stores it in the template_registry singleton (R/registry.R). Function names inside the YAML (downloader.function, reader.function, writers.staging.function) are resolved with getFromNamespace(..., "rb3"), so they must be real functions in this package:
- downloaders live in
R/downloaders.R(datetime_download,sprintf_download,curve_download,settlement_prices_download,stock_indexes_*_download, …) - readers live in
R/readers.R(fwf_read_file,csv_read_file,curve_read,settlement_prices_read,stock_indexes_json_reader,pricereport_reader) - staging processors and public
*_get()accessors live inR/scraper-*.R(process_cotahist,process_futures,process_yc,process_indexes_*)
If a template has no reader, the reader is derived from filetype (<filetype>_read_file). Field type strings such as date(format="%d/%m/%Y") are parsed by type_parse() against VALID_TYPES in R/types.R; R/fields.R turns fields into readr collectors, Arrow schemas and fixed widths.
Adding a data source therefore means: a YAML template, a downloader function, a reader function, optionally a staging process_* function, and a *_get() accessor. R/template.R handles loading, template_retrieve(), template_schema(), template_db_folder() and template_dataset().
fetch -> download -> read -> staging
fetch_marketdata(template, ...) (R/fetch-marketdata.R) is the user entry point:
-
expand.grid(...)of the arguments gives onemetaper combination (create_meta_list). -
download_market_filescallsdownload_marketdata(meta)(R/download-marketdata.R) for each meta not yet downloaded (or whenforce_download = TRUE). The file is unzipped if needed, gzipped, and stored as<cachedir>/raw/<md5>.gz. -
process_input_layercallsread_marketdata(meta)(R/read-marketdata.R), which runs the template reader and writes an Arrow dataset to<cachedir>/db/input/<template-id>/, partitioned perwriters.input.partition. - If the template has a
writers.stagingblock and the input layer changed (orreprocess = TRUE),create_staging_layeropens the input dataset, applies the stagingprocess_*function, and writes<cachedir>/db/staging/<template-id>/.
*_get() accessors call template_dataset(template, layer) and return a lazy arrow dataset; callers filter with dplyr and collect().
meta: the download ticket
R/meta.R stores one row per (template, download args, extra_arg) in <cachedir>/meta.sqlite. The primary key download_checksum is a digest of those values. Flags is_downloaded, is_processed, is_valid drive skipping in fetch_marketdata. Every meta_set_*<- / meta_add_download<- call writes through to SQLite immediately. template_meta_create_or_load() is the normal way to obtain a meta.
Cache directory and registries
rb3_bootstrap() (R/rb3-package.R) reads getOption("rb3.cachedir") (falls back to tempdir()), creates raw/ and db/, and stores the paths in the rb3_registry singleton. It runs at attach time, so users must set options(rb3.cachedir = ...) before library(rb3). Registries are S3 objects that support $ / [[ get and set (R/registry.R).
Conventions
-
DESCRIPTIONhas an explicitCollate:list. A newR/*.Rfile must be added there or it will not be sourced byR CMD build. - Roxygen with markdown;
NAMESPACEandman/are generated, never hand-edited. - User-facing messages use
cli; errors carry classes (error_download_fail,error_meta_not_found,error_template_missing_args, …) that callers catch withtryCatch/inherits. -
examples/,legacy/,b3-docs/,pkgdown/and the root*.mdoverview files (PROJECT_OVERVIEW.md,ARCHITECTURE_DIAGRAMS.md,QUICK_REFERENCE.md,DOCUMENTATION_INDEX.md) are excluded from the package build via.Rbuildignore; the overview files are generated summaries, not the source of truth. - Business-day calendars come from
bizdays("Brazil/ANBIMA","Brazil/BMF","Brazil/B3"); futures maturity codes are decoded bymaturitycode2date()/code2month()inR/scraper-futures.R.
