Pipe mode: a mid-pipe transformation verb that generates R code, executes it, and returns the result.
Arguments
- .data
In pipe mode: an R object piped in. In standalone mode: the first prompt string.
- ...
Character strings describing the transformation/task.
- .model
Model to use for code generation (default: session model).
- .retries
Integer; max retry attempts when generated code fails to parse or execute (default: 2, configurable via
hal.do_retriesoption). Set to 0 to disable retries. The retry re-prompts the same disposable session with the error message, so the model can self-correct.- .verify
Logical; in pipe mode with a data-frame result, compare the input and output and display a one-line structural report (row/column deltas, class changes, introduced NAs). The full report is attached as
attr(result, "hal_verify"). Report-only: it never affects retries or the returned values, but suspicious patterns (output identical to input, 0-row output) raise a classedhal_do_warning. Default TRUE (configurable via thehal.verifyoption).
Details
Standalone mode: called directly with a string prompt to generate and execute R code in the caller's environment.
Spawns a disposable session – does NOT pollute the main conversation. Governance controls (denylist, credential scanner, timeout) are enforced.
Failure behavior
In an interactive session, a failure (after retries) warns and returns
.data unchanged (pipe) or invisible(NULL) (standalone) – forgiving
for console exploration. In non-interactive contexts (scripts, R
Markdown, targets), it aborts instead: a pipeline silently continuing
with untransformed data is worse than an error. Override either way with
options(hal.do_on_fail = "warn") or "abort". All failures signal
classed conditions (hal_do_error / hal_do_warning), so you can
tryCatch(..., hal_do_error = function(e) ...).
Examples
if (FALSE) { # \dontrun{
# Pipe mode
mtcars |> hal_do("keep only cars with mpg > 25")
iris |> hal_do("add a column for petal area") |> head()
# Standalone mode
hal_do("write a function called greet that takes a name")
greet("world")
# Disable retries
mtcars |> hal_do("normalize mpg", .retries = 0)
} # }
