Format R File
Description
Format an R file in place or write to a new file.
Usage
1rformat_file(path, output = NULL, 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 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).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} elsebefore formatting.function_space: If TRUE, add space before(in function definitions:function (x)instead offunction(x). Default FALSE matches 96% of R Core source code.join_else: If TRUE (default), moveelseto the same line as the preceding}.
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)