
Turn an Excel sheet into R: code that reads the data and recreates the formulas
Source:R/hal-excel.R
hal_excel.RdReads an .xlsx, treats columns without formulas as data, and translates
each formula column into a tidyverse expression via the active hal backend.
Returns a runnable R script – a line that reads the input columns from the
workbook plus a mutate() pipeline that recreates the formula columns – so
you can replace the spreadsheet (and the hal_excel() call) with the code.
Usage
hal_excel(path, sheet = 1, model = NULL, tolerance = NULL, retries = NULL)
# S3 method for class 'hal_excel_code'
print(x, ...)Arguments
- path
Path to an
.xlsxworkbook.- sheet
Sheet name, or 1-based index (default
1).- model
Optional model id; defaults to the session/configured model.
- tolerance
Numeric tolerance for float comparison (default
hal.xl_tolerance, or1e-9).- retries
Max self-correction attempts per column on mismatch (default
hal.xl_retries, thenhal.do_retries, then2).- x
A
hal_excel_codeobject (the generated R script).- ...
Ignored.
Value
A hal_excel_code object (a character string of R code) that prints as
the generated script. The per-column report is attached as
attr(result, "hal_excel"). Save it with writeLines(result, "out.R").
Details
Works like hal_do() under the hood (disposable worker, code extraction,
denylist, timed eval, retry loop), with one addition: each translation is run
and checked against the values Excel cached, so only columns that match Excel
row-for-row go into the live pipeline; the rest are emitted as commented
stubs to review.
Each formula column is assumed to hold one formula filled down the column
(a vectorised expression, e.g. =A2*B2), so its first formula is translated
once. A workbook with no cached values is still translated but reported
unverifiable.
Examples
if (FALSE) { # \dontrun{
hal_excel("model.xlsx") # prints the read + mutate script
code <- hal_excel("model.xlsx")
writeLines(code, "model.R") # or paste it in place of the call
attr(code, "hal_excel") # per-column verification report
} # }