R Formatter is a VS Code extension that formats R source files using the styler package. It integrates with VS Code's built-in format-on-save and Format Document commands.
Features
- Format
.R,.r,.Rmd, and.rmdfiles usingstyler::style_file(). - Checks for R and styler on startup and offers to install styler if missing.
- Configurable formatting scope: spaces, indentation, line breaks, or full token-level changes.
- Configurable indentation width, line length, strict mode, roxygen example formatting, and comment spacing.
- Works with VS Code's Format Document, Format on Save, and format-on-type features.
- Supports a custom
Rscriptpath for non-standard R installations.
Requirements
- R must be installed and
Rscriptmust be onPATH(or configured via ther-formatter.rscriptPathsetting). - The styler R package must be installed. If it is missing, the extension will prompt you to install it automatically on startup.
To install styler manually, run in R:
install.packages("styler")
Settings
| Setting | Default | Description |
|---|---|---|
r-formatter.rscriptPath |
"Rscript" |
Path to the Rscript executable. |
r-formatter.scope |
"tokens" |
How invasive the formatter is: none, spaces, indention, line_breaks, or tokens. |
r-formatter.strict |
true |
Forces exactly one space or line break where required. |
r-formatter.indentBy |
2 |
Number of spaces per indentation level. |
r-formatter.lineLength |
80 |
Target line length (sets R's width option before formatting). |
r-formatter.includeRoxygenExamples |
true |
Format code inside @examples roxygen blocks. |
r-formatter.startCommentsWithOneSpace |
false |
Enforce a single space after # in comments. |
Installation
The extension is available on the VS Code Marketplace.
For development, testing, and packaging instructions, see DEVELOPMENT.md.
