
Format Long Vectors and Lists as Aligned Tables with Air
Source:R/54_use_air_table_.R
use_air_table.RdAir (\(\geq\) 0.8.0) can align the elements of a call column by column,
giving the same layout as tibble::tribble() or
data.table::fcase(). The feature is switched on by the experimental
# fmt: table comment directive placed right above the call, but air
never re-flows the elements on its own: whatever sits on one source line
stays there.
use_air_table() does the two things air does not do by itself:
runs
air formatonpath, so every long vector and list is laid out with one element per line;inserts the directive above every
c()/list()call that has more thanmin_lengthelements, and re-flows the elements so that each row holdsncolof them. Multi-line definitions are collapsed by deleting the line breaks between the elements of a row; a definition that air kept on a single line is split into rows instead.
A second air format run then pads the cells and aligns the columns.
Arguments
- path
Path to an R file to modify. If
NULLand RStudio is available, the path of the active document is used.- ncol
Number of elements placed on each table row.
- min_length
A vector or list is only converted when it has strictly more than this many elements.
- marker
The comment directive that enables table formatting in air.
- ...
Additional arguments. Currently unused and must be empty.
Details
Requirements and scope:
airmust be installed and on thePATH(seecheck_air_installed()), and the directive requires air 0.8.0 or newer.Only calls whose elements are all unnamed are rewritten. Air expands named elements (
list(a = 1)) one per line no matter what the directive says, so named calls are skipped to avoid a marker that has no effect.Calls that already carry
markeron the preceding line are left untouched, so the function is idempotent.Air only honours the directive on the outermost call of a statement, so a vector or list nested inside another call (for example an argument of
x <- list(a = c(...))) is skipped.
Examples
if (FALSE) { # \dontrun{
tf <- tempfile(fileext = ".R")
writeLines(
'x <- c("alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta")',
tf
)
use_air_table(tf, ncol = 3L, min_length = 6L)
readLines(tf) |> cli::cli_code()
} # }