format_r: Format R

View source: R/tools.R

format_rR Documentation

Format R

Description

Uses the {styler} and {box.linters} packages to automatically format R sources. As with styler, carefully examine the results after running this function.

Usage

format_r(paths, exclude_files = NULL)

Arguments

paths

Character vector of files and directories to format.

exclude_files

Character vector with regular expressions of files that should be excluded from styling.

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 modules

  • Alphabetically 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.

Value

None. This function is called for side effects.

Examples

if (interactive()) {
  # Format a single file.
  format_r("app/main.R")

  # Format all files in a directory.
  format_r("app/view")
}

Appsilon/rhino documentation built on Sept. 27, 2024, 7:01 p.m.