rformat_dir

Format R Files in Directory

Description

Format all R files in a directory.

Usage

1rformat_dir(path = ".", recursive = TRUE, dry_run = FALSE, indent = 4L,
2            line_limit = 80L, wrap = "paren", brace_style = "kr",
3            control_braces = FALSE, expand_if = FALSE, else_same_line = TRUE,
4            function_space = FALSE, join_else = TRUE)

Arguments

  • path: Path to directory.
  • recursive: If TRUE, process subdirectories.
  • dry_run: If TRUE, report changes without writing.
  • indent: Indentation per level: integer for spaces (default 4), or character string for literal indent (e.g., "\t\t" for vintage R Core style).
  • line_limit: Maximum line length before wrapping (default 80).
  • wrap: Continuation style for long function signatures: "paren" (default) aligns to opening parenthesis, "fixed" uses 8-space indent.
  • brace_style: Brace placement for function definitions: "kr" (default) puts opening brace on same line as ) {, "allman" puts it on a new line.
  • control_braces: If TRUE, add braces to bare one-line control flow bodies. Default FALSE matches R Core majority style.
  • expand_if: Expand inline if-else to multi-line (default FALSE).
  • else_same_line: If TRUE (default), repair top-level }\nelse (which is a parse error in R) by joining to } else before formatting.
  • function_space: If TRUE, add space before ( in function definitions: function (x) instead of function(x). Default FALSE matches 96% of R Core source code.
  • join_else: If TRUE (default), move else to the same line as the preceding }.

Value

Invisibly returns vector of modified file paths.

Examples

1# Format all R files in a directory (dry run)
2d <- tempfile()
3dir.create(d)
4writeLines("x<-1", file.path(d, "test.R"))
5rformat_dir(d, dry_run = TRUE)
6
7# Format and overwrite
8rformat_dir(d)
9unlink(d, recursive = TRUE)