R: Reformat R code while preserving blank lines and comments
tidy_source
R Documentation
Reformat R code while preserving blank lines and comments
Description
This function returns reformatted source code; it tries to preserve blank
lines and comments, which is different with parse and
deparse. It can also replace = with <- where
= means assignments, and reindent code by a specified number of spaces
(default is 4).
a character string: location of the source code (default to be
the clipboard; this means we can copy the code to clipboard and use
tidy_source() without specifying the argument source)
comment
whether to keep comments (TRUE by default)
blank
whether to keep blank lines (TRUE by default)
arrow
whether to replace the assign operator = with <-
brace.newline
whether to put the left brace { to a new line
(default FALSE)
indent
number of spaces to indent the code (default 4)
output
output to the console or a file using cat?
text
an alternative way to specify the input: if it is NULL,
the function will read the source code from the source argument;
alternatively, if text is a character vector containing the source
code, it will be used as the input and the source argument will be
ignored
width.cutoff
passed to deparse: integer in [20, 500]
determining the cutoff at which line-breaking is tried (default to be
getOption("width"))
...
other arguments passed to cat, e.g. file
(this can be useful for batch-processing R scripts, e.g.
tidy_source(source = 'input.R', file = 'output.R'))
Value
A list with components
text.tidy
the reformatted code as a
character vector
text.mask
the code containing comments, which are
masked in assignments or with the weird operator
Note
Be sure to read the reference to know other limitations.