Skip to main content

CLI Commands

This page documents the eight {baler} subcommands: what each one does, when you’d reach for it, and what its flags actually change. It’s written for people using {baler} to manage {box} modules, not for people working on {baler}’s own source.

Every subcommand exits non-zero and prints error: ... to stderr on failure, so you can rely on that in scripts or CI without parsing output.

Initialization and Scaffolding

Through baler init command, this will set up a module project. It works in two ways, depending on whether you give it a name:

  1. When you supplied the <name>, it scaffolds a new module project from scratch, in a new directory.

    baler init <name> [--dir-name <dir>] [--native <lang>] [--backend <backend>]
  2. Without one, it initializes the directory you are already in, like uv init or cargo init.

    baler init [--name <name>] [--src <dir>] [--native <lang>] [--backend <backend>]

Either way, baler.toml is the most required file to interact with {baler}. And then this following table summarizes how baler init behaves:

Argument Description
<name> The module’s name. Used as the default project directory name (<name>-proj), the default source directory name, and the name field written into baler.toml. Leave it out to initialize the current directory instead.
--dir-name <dir> Only with <name>. Use a different project directory name instead of <name>-proj.
--name <name> Only without <name>. The module’s name, instead of the one worked out from the current directory (see The current directory). Anything goes; it isn’t checked.
--src <dir> Only without <name>. The directory holding the module’s source, relative to the current directory. Written as src in baler.toml, unless it is the module’s own name, which is the default. It must be an existing directory inside the project.
--native <lang> Scaffold a module with compiled code instead of pure R: c for C, cpp for C++. Sets up a starter source file, build configuration, and the loader code that runs dyn.load() automatically. Leave this out for a plain R module.
--backend <backend> Only relevant with --native cpp. Which C++ binding library to scaffold: rcpp (the default) or cpp11.

When --native is not passed, this will ignore --backend parameter, and won’t generate the files under the chosen language, just their pure R versions.

A new project

Once executed with a <name>, the following files:

  1. baler.toml
  2. README.md
  3. And R source files under a folder containing the source code of the module

are generated. It only fails without changing anything if the target directory already exists, so it’s safe to run and see the error rather than risk overwriting something.

Here’s an example:

baler init stats_native

The current directory

Without a <name>, baler init sets up the directory you are in. It always writes baler.toml there. If baler.toml already exists, it stops with an error and writes nothing.

There are two cases of what else it does depends on the directory:

  1. When the directory is empty (not counting the hidden entries such as .git), it also adds README.md and an example module, in a folder named after the module. This is the same layout as a new project, only in place. --native and --backend apply here.

  2. Otherwise, when it is not empty, i.e. you already have the source code files and/or anything, it writes baler.toml and nothing else. Your files are left as they are, and no example module or README is added. --native is rejected here, because scaffolding native code into a project that already has files could overwrite them. Add the native code yourself instead; it is detected automatically.

The module’s name

The module’s name is the directory’s name, as is. For example, when the current directory you initialized is named weather/ will give weather, and tidyverse-module/ gives tidyverse-module. baler init do not strip or check anything. That’s because a name that isn’t syntactic in R, such as my-module, still works, since box::use() accepts it in backticks. To use a different name, pass --name or edit name in baler.toml.

The source directory

src in baler.toml says where the module’s source lives. By default it is a folder named after the module. If your module is in a different folder, pass --src <dir> or edit src in baler.toml.

If the source directory doesn’t exist, baler init prints a warning and still writes baler.toml. Keep in mind that baler bundle needs an __init__.r in that directory, which baler init doesn’t create for code that is already there.

Examples

Here are three common situations.

  1. You are in an empty directory called weather/. Running baler init gives you a module named weather, plus README.md and an example module in weather/weather/. The same goes for a directory called tidyverse-module/, where the module is named tidyverse-module.

  2. You are in an existing weather/ project whose source code is in weather-module/:

    └── weather/
        ├── LICENSE.md
        └── weather-module/
            ├── __init__.r
            └── submodule/
                ├── __init__.r
                └── source.r

    The module is named weather, but the source isn’t in a folder with that name, so point baler init at it:

    baler init --src weather-module

    baler.toml is the only new file, and it records the source directory as src = "weather-module".

  3. You are in tidyverse-module/, but you want a different module name. Pass --name:

    baler init --name tidyverse

Native compiled code compilation

As what the official documentation of {box} R package said, one module can handle native compiled codes. {baler} gives you an alternative approach to manage module’s native code (C, C++, …) right where the source lives, for quick local iteration while developing. It never deletes the source afterward, which is what sets it apart from the compile step that happens automatically during install. For this one, use baler compile and here’s the following anatomy:

baler compile [<path>] [--clean] [--rebuild]

The following table summarizes the following bracketed “parameters” above:

Argument Description
<path> The project’s root directory. If you leave this out, it defaults to the current directory, so plain baler compile works from inside a project.
--clean Deletes this module’s compiled output and anything cached from previous builds, then stops without compiling anything. Use this when you just want a clean slate, not a new build.
--rebuild Throws away anything cached from a previous build first, then compiles again for real, even if nothing in the source actually changed. Useful when you’ve upgraded your compiler or toolchain and want to confirm the module still builds correctly, since an unchanged build would otherwise just reuse the old result.

A plain baler compile, with neither flag, only recompiles when the source has actually changed since the last build; otherwise it reuses the previous result to save time. --clean and --rebuild do opposite things and can’t be used together, passing both is an error.

Bundling and Packaging

Similar to pkgbuild::build() (or devtools::build()) and an analogue for R CMD build, baler bundle now bundles a module’s source, creating into a single .tar.gz archive that can be installed elsewhere, shared with someone else, or attached to a GitHub release. Here’s the anatomy of the baler bundle command:

# If `<path>` isn't supplied
# Then `.` argument is passed by default
baler bundle [<path>] [--locked] [--merge <archive>...]
Argument Description
<path> The project’s root directory, wherever baler.toml lives. Defaults to ..
--locked Fail instead of warning when baler.lock is missing, unreadable, or doesn’t match baler.toml. Like cargo publish --locked. The lock ships inside the bundle, so a stale one breaks installs of it. Without the flag, bundle only warns. A module dependency that declares a source must also be pinned in a lock that holds modules.
--merge <archive>... Also bring in the binaries of bundles of the same module that were made on other platforms (see Bundling for more than one platform). Needs binary = true under [compiled-code]. If a path is given, put it before the flag: baler bundle . --merge a.tar.gz b.tar.gz.

What goes into the archive is decided by [compiled-code] in baler.toml, not by flags:

  • The native source is always in the archive, so a machine that can’t use the shipped binary builds it on install.
  • When binary = true is declared under [compiled-code], {baler} also compiles the native code in the module first and ships the binary built for the machine that ran baler bundle. The platform it was built for is recorded next to it, and install checks that against the machine (see Binary bundles).

The --binary and --keep-source flags of {carrier} are gone, and passing one is now a command-line error. There is no keep_source setting either, because the native source is always shipped.

The resulting archive is named <name>_<version>.tar.gz (<name> is what you set as name under [project] in baler.toml), and written to whatever directory you ran the command from. The execution fails if the source directory is empty, or if baler.toml is missing or can’t be parsed.

Bundling for more than one platform

A binary only runs on the platform and R version it was built for, and the archive stores each one in its own .lib/<platform>/ folder. To make one archive that has a binary for every platform you support, there are two ways:

  1. Run baler bundle on each machine, one after another, in a directory that already holds the archive. The archive keeps the binaries of the other platforms and adds the one built here. This only works while the module’s files are the same. If they changed, {baler} stops and tells you to delete the archive and bundle again, since the old binaries may not belong to the new source.

  2. Bundle on each machine separately, collect the archives in one place, and merge them:

    baler bundle . --merge linux_pkg.tar.gz windows_pkg.tar.gz

    The bundle is compiled for the current platform as usual, and the binaries of the listed archives are added. Each one has to come from the same files as the project, and two archives can’t both hold a binary for the same platform. Like a normal bundle, the result is written to <name>_<version>.tar.gz in the current directory and replaces an archive already there, so list that archive too if you want to keep what it holds.

The summary printed at the end names every platform the bundle now holds a binary for.

Module installation

As long as the {box} module is configured by {baler}, you can install that {box} module so that other machines and/or projects can load it with box::use(). This is {baler}’s equivalent of install.packages().

baler install <name> --repo <url> [--version <constraint>] [--install-deps [--extras]]
baler install --path <dir-or-tar.gz> [--install-deps [--extras]]
baler install --git <url> [--branch <ref> | --tag <ref> | --rev <ref>] [--module-dir <dir>] [--install-deps [--extras]]
# Always look up at tarball source only
baler install --url <tarball> [--install-deps [--extras]]

Use exactly one of <name>, --path, --git, or --url to say where to install from. Using more than one, or none, is an error.

Form What it does
<name> (positional) A bare module name for a registry lookup, nothing else. Never guessed as a path, mirrors cargo install <crate>/pip install <pkg> exactly. Needs --repo or it errors, telling you to use --repo, --path, --git, or --url instead.
--path <dir-or-tar.gz> A directory containing baler.toml, or a .tar.gz produced by baler bundle. The only way to install from a local source, mirrors cargo install --path.
--git <url> A GitHub repository URL, e.g. https://github.com/user/repo. Fetches through GitHub’s tarball API, not generic git, so a GitLab or self-hosted URL won’t work despite the flag’s name. Add --branch, --tag, or --rev to pin a ref (pick one), and --module-dir <dir> if the repo holds more than one module.
--url <tarball> Installs directly from an already-bundled archive URL, such as a GitHub Release asset from baler bundle. Must end in .tar.gz, checked up front.
--repo <url> Looks up <name> in a module registry at <url>. Registries aren’t built yet, so this currently just fails with a clear “not implemented” error. Add --version <constraint> once registries exist.

The extra parameter, --install-deps, downloads and installs the module’s dependencies, whether R packages (from CRAN by default, from another CRAN-like repository with repo, or from GitHub with git) or box-bale modules. Without it, {baler} still shows what it would install, but doesn’t fetch anything. Installing a module that’s already installed under the same name replaces the old copy.

Modules are installed from the source their entry declares under [dependencies.baler], at the commit baler.lock pins when it has one for that source. A module that is already installed is left alone. Before installing one, {baler} checks that the source is the module the entry names and that the version it declares satisfies the entry’s constraint, and stops with an error if either is wrong.

--extras also installs the R packages (and {box} modules) the module declares under [extras], such as test frameworks or development tooling. It only works together with --install-deps, since extras are R packages installed from CRAN, and passing it alone is an error. If the module declares nothing under [extras], {baler} says so and installs nothing extra. When the module has a baler.lock, the extras are installed at the versions pinned there, transitive packages included, and a missing or outdated extras pin is an error telling you to run baler lock. Without a lock, [extras] is resolved together with the regular dependencies, and a package declared in both with different version constraints is an error. Without --extras, extras-only pins are never installed.

baler install never writes baler.lock. If the lock doesn’t match baler.toml, the install fails and points you to baler lock. On the other hand, with no lock at all, it resolves fresh.

R packages declared with git are fetched from GitHub’s API as well. They send GITHUB_TOKEN (or GH_TOKEN) when one is set, so set it in CI to stay clear of the same rate limit and to reach private repositories. See R packages from GitHub.

Installing via --git needs internet access to reach GitHub. Requests to GitHub’s API send GITHUB_TOKEN (or GH_TOKEN) when one is set, and only to api.github.com over HTTPS, never to a host given with --url. Set the token in CI to stay clear of GitHub’s rate limit for anonymous requests and to reach private repositories. When GitHub answers 404, 403 or 429 and no token is set, the error says so and names the variable to set.

The patched {box}

{baler} modules are loaded through a patched {box}, and baler box installs and inspects it. The pre-built files come from the {baler} release (see the {box} fork), thus you don’t have to build the source from scratch.

# Installation doesn't require compilation
baler box install [--tag <tag>] [--force]
baler box status [--check]

Install

baler box install asks R for its version and its library path, picks the file on the release that matches, checks it against the .sha256 file next to it, and installs it. Nothing in the library changes unless the download passes the check.

Flag What it does
--tag <tag> Takes {box} from the release with this tag. Without it, the latest release is used, which is the one whose {box} files are kept up to date.
--force Replaces a {box} that is not the baler build, or a box directory that is not an R package, and installs again even when the installed build is already current.

What else to expect:

  1. Without --force, a {box} whose DESCRIPTION has no Config/baler/support field (upstream {box}) is never replaced, because installing the fork deletes the old box directory first.
  2. The commit a build came from is kept in baler-source.txt inside the installed box directory. When it matches the commit on the release, the command says that {box} is already installed and stops.
  3. A file that is gone from the release, or a checksum that does not match, is tried once more before failing. The latest release has its {box} files replaced every few hours, so a download can land in the middle of that.
  4. A missing or malformed .sha256 file, or a release without box-source.txt, fails and installs nothing.
  5. When no build matches your R version and platform, the error lists the R versions the release has, and gives the command to compile the fork from source instead.

Status

baler box status prints the library, the installed version, the baler support level, and the repository, ref and commit the build came from. It reads files only and makes no network request. With --check it exits with an error unless a {box} with {baler} support is installed.

Environment

BALER_R_LIB and R_LIBS_USER choose the library, in that order, as described in Architecture. GITHUB_TOKEN (or GH_TOKEN) is sent to api.github.com only. BALER_RELEASE_API points the release lookup at another API root, such as a mirror, and never changes where the token goes.

Lock and lockfile

This is inspired by uv.lock and Cargo.lock. As long as the dependencies are declared on baler.toml, the command you’ll use, baler lock, will resolve everything a project depends on, both R packages and other modules, and writes the result to baler.lock so future installs use the exact same versions instead of re-resolving from scratch. This doesn’t install anything itself, it just pins what would be installed. Here’s the anatomy of baler lock command:

baler lock [<path>] [--update [<pkg>...]] [--with-rver] [--check] [--remove]

baler lock is idempotent. If baler.lock already matches baler.toml, nothing is written, so running it twice, or on another machine, never shows up in a diff. If baler.toml changed, every pin that still fits is kept, and only what changed is added, dropped or re-resolved.

baler lock also fetches the modules listed under [dependencies.baler] that declare a source, so it needs network access in that case. It locks the R packages those modules need along with your own, and pins each module: a git source to the commit its ref pointed at, a url source to the URL itself. A plain run keeps a pinned commit even if the branch has moved. A module that declares no source is skipped with a warning.

Argument Description
<path> The project’s root directory. Defaults to ..
--update Ignore the existing pins and re-resolve every dependency from scratch, module pins included. Use this when you want to pick up newer versions.
--update <pkg>... Re-resolve only the named packages or modules (e.g. --update purrr cli rcalc) and keep every other pin. A package from GitHub moves to the newest commit of its branch. A name that isn’t in the lock is an error.
--with-rver Also record the R version you currently have on your system inside baler.lock. This is informational only, it’s never enforced or checked against on install. Only allowed when the lock is being created, or together with --update.
--check Never write anything. Exit with an error if baler.lock is missing or doesn’t match baler.toml. Meant for CI. Can’t be combined with --update, --with-rver or --remove.
--remove Delete baler.lock instead of writing one. Safe to run any time, if there’s no lockfile, {baler} just resolves dependencies fresh each time, same as a project that never had one.
Note

A bare positional argument is a registry name and is never treated as a path or a URL, so it needs --repo <url>. Without --repo, {baler} stops with an error that lists --path, --git and --url instead. --branch, --tag, --rev and --module-dir only apply to --git.

List and show installed modules

baler list and baler show read what {baler} kept when it installed each module: the baler.toml in its .dist-info folder next to the module. They only read, so they are safe to run anywhere.

baler list [<pattern>] [-k <keyword>[,<keyword>...]] [--names-only]
baler show <name>

baler list prints one line for each installed module, sorted by name:

NAME  VERSION  LICENSE  R
rmod  0.2.0    MIT      >=4.0.0

With nothing installed it says so and names the install directory. Modules that are in the directory but were not installed by {baler} have no record, so they are not listed. A module whose record cannot be read is reported on stderr, and the others are still listed.

Narrow down the list

Argument Description
<pattern> Only the modules whose whole name matches. * stands for any run of characters, including none, and ? for exactly one character. Case is ignored.
-k, --keyword <keyword> Only the modules that list this keyword under keywords in their baler.toml. Separate several with commas, or repeat the flag, and a module then needs all of them: -k stats,plots is the same as -k stats -k plots. Case is ignored, and the keyword has to match whole, so stat does not find a module tagged statistics. --tag is the same flag.
--names-only Print the module names alone, one for each line, with no header and no other columns.
baler list 'conv*'
baler list -k stats,plots
baler list '*calc*' --names-only

The pattern is matched against the whole name, so conv finds only a module called conv, and conv* finds convert too. Put it in quotes. Without them the shell may expand the * against the files in the current directory before {baler} sees it.

When the pattern and --keyword are both given, a module has to satisfy both. If nothing matches, baler list says which filters found nothing and exits successfully:

No installed modules match the pattern 'conv*' and the keyword 'stats'.

With --names-only nothing is printed in that case, so a script reading the names gets no lines and no sentence to trip over:

for name in $(baler list --keyword stats --names-only); do
    baler show "$name"
done

Without a pattern, --keyword or --names-only, baler list prints every installed module in the table above, exactly as it always has. An empty pattern or keyword, such as a shell variable that expanded to nothing, is an error and not an empty list. A module whose record cannot be read is reported only when the pattern selects it, so a broken module you did not ask about stays quiet. The keywords live in the record, so a module that cannot be read cannot be matched by --keyword.

Show one module

baler show <name> prints what that module declared and where its files are:

rmod 0.2.0
A short description

License:      MIT
License file: LICENSE
README:       README.md
Authors:      Jane Doe, John Smith <john@example.com>
R:            >=4.0.0
Repository:   https://github.com/user/rmod
Location:     ~/.baler/modules/rmod
Metadata:     ~/.baler/modules/rmod-0.2.0.dist-info
Locked:       yes
Native code:  none

R packages:
  dplyr (*)

baler modules:
  other (>=1.0.0)

License file and README name the files kept in the Metadata folder, and say none for a module installed by {baler} 0.1.0, which kept neither. Locked is whether the module came with a baler.lock. Native code says need compilation when compiled native code was installed with the module, and none when it has none. The module name has to be a plain name, as baler list shows it.

Uninstall a module

The bundle part gives you an archive, under .tar.gz format, of {box}-{baler} module. Then we have the “installation” part that archives the module and placed on the path where ~/.baler/modules lived. {baler} also has an option to uninstall a previously installed module, through baler remove. Here’s the following anatomy:

baler remove <name> [--force]
Argument Description
<name> The name of an installed module to remove.
--force Skip the [y/N] confirmation prompt and remove it immediately.

baler remove deletes the module’s directory and every .dist-info folder {baler} kept for it, whatever the version. {baler} 0.1.0 left that folder behind, and an upgrade left the old version’s one too. Removing the name now cleans those up as well, even when the module itself is already gone.

The name has to be one plain name. ., .. and anything with a / in it are refused, so a typo such as baler remove . cannot point at the install directory and delete every module.

Fails if no module with that name is currently installed, there’s nothing to accidentally remove twice.