Uses the {styler}
and {box.linters}
packages to automatically format R sources. As with
styler
, carefully examine the results after running this function.
Details
The code is formatted according to the styler::tidyverse_style
guide with one adjustment:
spacing around math operators is not modified to avoid conflicts with box::use()
statements.
If available, box::use()
calls are reformatted by styling functions provided by
{box.linters}
. These include:
Separating
box::use()
calls for packages and local modulesAlphabetically sorting packages, modules, and functions.
Adding trailing commas
box.linters::style_*
functions require the treesitter
and treesitter.r
packages. These, in
turn, require R >= 4.3.0. format_r()
will continue to operate without these but will not
perform box::use()
call styling.
For more information on box::use()
call styling please refer to the {box.linters}
styling
functions
documentation.
Examples
if (interactive()) {
# Format a single file.
format_r("app/main.R")
# Format all files in a directory.
format_r("app/view")
}