| Title: | Format R Code with Arity |
|---|---|
| Description: | Provides R bindings to the deterministic, rule-based formatter from 'arity'. Format R source text or individual files using a style based on the tidyverse R style guide. |
| Authors: | Johan Larsson [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-4029-5945>) |
| Maintainer: | Johan Larsson <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-07 22:25:57 UTC |
| Source: | https://github.com/jolars/arity-r |
Formats selected R code in the source editor, or the whole document when
nothing is selected. The addin discovers arity.toml from the document's
directory, or the working directory for untitled buffers, following the
configuration rules of format_text(). Syntax verification is enabled.
format_addin()format_addin()
Install arity to make Format with arity available in RStudio's Addins menu. You can assign a keyboard shortcut through Tools > Modify Keyboard Shortcuts.
Whole-document formatting supports R scripts, .Rprofile files, and untitled
R buffers. In other documents, such as R Markdown or Quarto files, select
complete R expressions in the source editor. Each selection is formatted
independently, preserving its initial indentation. If any selection cannot be
formatted, the document is left unchanged.
Changes are applied to the editor buffer without saving the file. When formatting a whole document, the cursor is restored to its original row and column, or the closest position if the document becomes shorter.
Invisibly returns TRUE when the buffer changed and FALSE otherwise.
format_text() formats one R source string and returns the result.
format_file() formats one UTF-8 file in place and reports invisibly whether
its contents changed. Both functions use the formatter embedded in the
arity-formatter Rust crate.
format_text( text, line_width = NULL, indent_width = NULL, line_ending = NULL, roxygen_markdown = FALSE, verify = TRUE, config = TRUE ) format_file( path, line_width = NULL, indent_width = NULL, line_ending = NULL, roxygen_markdown = FALSE, verify = TRUE, config = TRUE )format_text( text, line_width = NULL, indent_width = NULL, line_ending = NULL, roxygen_markdown = FALSE, verify = TRUE, config = TRUE ) format_file( path, line_width = NULL, indent_width = NULL, line_ending = NULL, roxygen_markdown = FALSE, verify = TRUE, config = TRUE )
text |
A non-missing character scalar containing valid UTF-8 R source code. Strings with a declared encoding are converted to UTF-8. |
line_width |
Maximum output line width, from 1 through 1000. |
indent_width |
Number of spaces per indentation level, from 1 through
1000. |
line_ending |
Output line-ending policy. |
roxygen_markdown |
Whether roxygen blocks without an explicit |
verify |
Whether to verify syntax preservation, ordinary comment preservation, and idempotence after formatting. |
config |
|
path |
A non-missing character scalar naming a UTF-8 file. The contents are treated as R source regardless of the file extension. |
By default, formatting uses the nearest arity.toml, searching upward from
the file's directory for format_file() or the working directory for
format_text(). The search includes the repository root, then stops at a
.git file or directory, or at the filesystem root. If no project config is
found, a nonempty ARITY_CONFIG environment variable supplies a fallback
config path. Relative explicit and fallback paths use the working directory.
Configuration files are not merged. Missing settings use built-in defaults,
and explicit formatting arguments override the selected configuration.
The package reads line-width, indent-width, and line-ending from the
[format] table. Settings for other CLI operations, including exclusions,
have no effect. Invalid formatting configuration raises an error before any
source is changed.
format_text() returns a character scalar. format_file() invisibly returns
TRUE when the file changed and FALSE otherwise.
format_text("x<-(1+2)*3^4\n") path <- tempfile(fileext = ".R") writeLines("x<-1", path) format_file(path) readLines(path) unlink(path)format_text("x<-(1+2)*3^4\n") path <- tempfile(fileext = ".R") writeLines("x<-1", path) format_file(path) readLines(path) unlink(path)