rformat_file

Format R File

Description

Format an R file in place or write to a new file.

Usage

1rformat_file(
2  path,
3  output = NULL,
4  dry_run = FALSE,
5  indent = 4L,
6  wrap = "paren",
7  expand_if = FALSE,
8  brace_style = "kr"
9)

Arguments

  • path: Path to R file.
  • output: Optional output path. If NULL, overwrites input file.
  • dry_run: If TRUE, return formatted code 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).
  • wrap: Continuation style for long function signatures: "paren" (default) aligns to opening parenthesis, "fixed" uses 8-space indent.
  • expand_if: Expand inline if-else to multi-line (default FALSE).
  • brace_style: Brace placement for function definitions: "kr" (default) puts opening brace on same line as ) {, "allman" puts it on a new line.

Value

Invisibly returns formatted code.

Examples

1# Format a file (dry run to see result without writing)
2f <- tempfile(fileext = ".R")
3writeLines("x<-1+2", f)
4rformat_file(f, dry_run = TRUE)
5
6# Format and overwrite
7rformat_file(f)
8readLines(f)
9unlink(f)