Import
Import .rh header files to share definitions,
macros, and preflight checks across your project.
Two Ways to Import
Use #> import in your .R or
.rh file:
#> import macros.rhOr use the -import CLI flag:
builder -import macros.rh -input srcr -output RWhat Can .rh
Files Contain?
Header files support all builder directives:
#> define- Constants#> macro/#> endmacro- Macro definitions#> preflight/#> endflight- Validation checks (executed during build)#> ifdef/#> ifndef/#> endif- Conditional compilation#> import- Nested imports
Local Import
Import a local .rh file:
#> import utils.rh
foo <- function() {
LOG("hello")
}Sharing Headers via Packages
You can distribute header files in R packages for reuse across projects.
Package Structure
Place .rh files in your package’s
inst/ directory:
mypkg/
├── DESCRIPTION
├── inst/
│ └── macros.rh
└── R/
└── ...
Importing from Packages
Use the pkg::path syntax to import from an
installed package:
#> import mypkg::macros.rhBuilder resolves this to the installed package’s
inst/ directory (e.g.,
/path/to/library/mypkg/macros.rh).
Subdirectories
You can organize headers in subdirectories:
inst/
├── macros/
│ ├── logging.rh
│ └── validation.rh
└── config.rh
#> import mypkg::macros/logging.rh
#> import mypkg::config.rhUsage
Definitions from package imports work the same as local imports:
#> import mypkg::macros.rh
foo <- function() {
LOG("hello")
}Nested Imports
Header files can import other headers:
# base.rh
#> define VERSION 1.0.0# utils.rh
#> import base.rh
#> macro
LOG <- function(x) {
message(paste0("[v", VERSION, "] ", .x))
}
#> endmacroPreflight in Headers
Shared validation checks:
# checks.rh
#> preflight
if(!requireNamespace("rlang", quietly = TRUE)) {
stop("rlang is required")
}
#> endflightProcessing Order
- CLI imports are processed first (in order specified)
- Inline
#> importdirectives are processed as encountered - Duplicate imports are automatically skipped
- All imports are processed before source files
Summary
.rhfiles support all directives (not just macros)- Local imports:
#> import file.rh - Package imports:
#> import pkg::file.rh(file atinst/file.rh) - Nested imports are supported and deduplicated