tidy_source()
supports the:=
operator now (which is widely used in data.table).
tidy_source()
supports anonymous functions of the form\(args) expr
for R >= 4.1.0 now (thanks, @notPlancha, #98).
-
Added a new argument
pipe
totidy_source()
. Ifpipe = TRUE
, it will convert the magrittr pipe%>%
to the base R pipe operator|>
. -
Added a function
tidy_rstudio()
to reformat R code in the RStudio editor. -
Added a function
tidy_pipe()
to substitute%>%
with|>
(currently works only in RStudio).
-
The right arrow assignment operator
->
is supported now. -
Added a new argument
args.newline
toformatR::tidy_source()
. When set toTRUE
, function arguments can start on a new line, e.g.,shiny::updateSelectizeInput(session, "foo", label = "New Label", selected = c("A", "B"), choices = LETTERS, server = TRUE)
can be reformatted to:
shiny::updateSelectizeInput( session, "foo", label = "New Label", selected = c("A", "B"), choices = LETTERS, server = TRUE )
-
Support the new pipe operator
|>
in R 4.1.0. -
The
width.cutoff
argument works more accurately when comments are indented (thanks, @iqis, #92). Previously,width.cutoff
did not take the indentation or the number of#
characters in a comment when wrapping it, which may lead to wrapped comment lines that are wider thanwidth.cutoff
.
-
Lines will be wrapped after operators
%>%
,%T%
,%$%
, and%<>%
now (thanks, @g4challenge #54, @jzelner #62, @edlee123 #68). -
The argument
width.cutoff
oftidy_source()
used to be the lower bound of line widths. Now if you pass a number wrapped inI()
, it will be treated as the uppper bound, e.g.,tidy_source(width.cutoff = I(60))
. However, please note that the upper bound cannot always be respected, e.g., when the code contains an extremely long string, there is no way to break it into shorter lines automatically (thanks, @krivit @pablo14, #71). -
The value of the argument
width.cutoff
can be specified in the global optionformatR.width
now. By default, the value is still taken from the global optionwidth
like before.
- When the text in the clipboard on macOS does not have a final EOL,
tidy_source()
fails to read the last line (thanks, @edlee123, #54).
-
White spaces on blank lines are removed now (thanks, @nylander, #88).
-
This package requires R >= 3.2.3 now.
tidy_source()
will try to preserve the character encoding of thetext
argument in the output.
- Added a new argument
wrap
totidy_source()
so that users can choose not to wrap comments viatidy_source(..., wrap = FALSE)
, or set the global optionoptions(formatR.wrap = FALSE)
(thanks, @YongLiedu, #73).
-
added a new function
tidy_file()
to format specified R scripts (thanks, @edlee123, #61) -
usage() was re-implemented by @egnha in #66; the major difference with the previous version is that
width
means the maximum width (if possible) instead of the minimum; it also gained two new arguments,indent.by.FUN
andfail
; see ?formatR::usage for details
tidy_source()
can preserve line breaks in character strings in source code
-
the deprecated functions tidy.source(), tidy.dir(), and tidy.eval() have been removed; use tidy_source(), tidy_dir() and tidy_eval() instead
-
comments that begin with
#+
or#-
are no longer wrapped; such comments are treated as knitr chunk options inknitr::spin()
(#52)
-
tidy_source()
should not write an extra space to the last line of code (thanks, @mr-karan, #49) -
long strings (> 1000 characters) in source code can be preserved now (thanks, @jholtman, #50)
-
tidy_source()
might move any lines of code starting withelse
back to the previous lines (thanks, @Auburngrads, #51)
-
tidy_source()
can deal with multibyte characters that cannot represented in the system native encoding now (on Windows) -
usage()
works for functions obtained from::
or:::
now, e.g.usage(formatR::tidy_source)
- the minimal required R version is 3.0.2 now
- added a new argument
output
to usage()
- fixed yihui/knitr#918: when code is NULL, parse() will hang (with a question mark waiting for input)
- added a function tidy_app() to replace tidy.gui() in previous versions: tidy_app() launches a Shiny app in the browser to reformat R code. The gWidgets interface (e.g. GTK+) is no longer supported. See https://yihui.shinyapps.io/formatR/ for a live demo.
- the shebang #! is no longer treated as an R comment (thanks, Mirko Ebert, #36)
-
three functions were renamed (from the
foo.bar
style tofoo_bar
):tidy.source()
(tidy_source()
),tidy.dir()
(tidy_dir()
), andtidy.eval()
(tidy_eval()
) -
the arguments of tidy_source() were renamed:
keep.comment
was renamed tocomment
,keep.blank.line
->blank
,replace.assign
->arrow
,left.brace.newline
->brace.newline
, andreindent.spaces
->indent
; similarly, the corresponding global options were also renamed: now you should useoptions(formatR.comment)
instead ofoptions(keep.comment)
,keep.blank.line
->formatR.blank
, and so on; see?tidy_source
and https://yihui.org/formatR/ for details
-
the usage() function returns the source code of the usage of a function now; in previous versions, it only returns NULL
-
added a new argument 'tidy' in usage() to make it possible not to reformat the usage code
-
tidy_source() may not work for R 3.0.0 or 3.0.1 if the code only contains comments due to a bug in base R, which has been fixed; if you use R 3.0, please upgrade to at least R 3.0.2 (R 2.15.x is not affected)
-
the argument 'replace.assign' in tidy.source() will be treated as FALSE if there is no = in the code, because there is no need to replace = in such cases (this slighly improves the performance)
-
the PDF vignette was removed, and only the Markdown vignette was kept in this package, which uses the vignette engine knitr::docco_linear; see vignette('formatR', package = 'formatR')
-
tidy.source() uses utils::getParseData() to identify comments in R code under R 3.0.x, which is much more accurate than the regular expressions in previous versions; users are strongly recommended to try R 3.0.x (#25, #26)
-
changed the meaning of the argument 'width' in usage(); see documentation
-
tidy.source(text = character(0)) returns character(0) instead of ''
-
removed the (dark voodoo) functions parse.tidy() and deparse.tidy() as well as the operator "%InLiNe_IdEnTiFiEr%"; they were designed for the pgfSweave package, which has been archived on CRAN for a long time
-
the function unmask.source() is no longer exported
-
backslashes in whole lines of comments can be correctly retained now (e.g. #' \code{1+1}) (thanks, KAPLAN Bernard)
-
the font button in tidy.gui() works again (#23) (thanks, Dason Kurkiewicz)
-
the option left.brace.newline was buggy; it did not work for empty lines
- the option keep.space in tidy.source() was removed; the spaces before comments will not be faithfully kept
- the number of spaces for indentation can be specified in tidy.gui()
-
the replace.assign argument is much more reliable now; it is based on the codetools package (code analysis) instead of regular expressions (big thanks to Kohske Takahashi)
-
replace.assign also works when keep.comment=FALSE; in previous versions, replace.assign=TRUE only applies to keep.comment=TRUE
-
tidy.source() gained a new argument 'left.brace.newline'; when set to TRUE, the left curly brace { will be moved to a new line (#18) (thanks, Jared Lander)
-
the 'text.tidy' component in the results of tidy.source() is a character vector of code blocks instead of code lines now, e.g. in previous versions, the result may be c('if (TRUE) {', '1', '}') (vector of length 3), but now it becomes 'if (TRUE) {\n1\n}'; each element of 'text.tidy' contains a minimal complete code block
-
potential dependency on the parser package has been removed (replaced by the codetools package); this also makes it more robust to use Unicode characters in R code now, see issue #13 for example
-
roxygen comments (#') will not be reflowed; this gives us control over which comments to be reflowed (sometimes we do not want comments to be wrapped and we can write them in the special roxygen comments)
- the results of tidy.source() (a list) only contain text.tidy and text.mask now; begin.comment and end.comment were removed since they were not used anywhere
-
the dependency on the parser package was removed because it was orphaned on CRAN; this affects two features: replace = with <- (the 'replace.assign' option in tidy.source()) and the identification of inline comments; tidy.source() will still work in most cases, but please keep in mind that (1) 'replace.assign=TRUE' will not be entirely reliable without parser (so use with extreme caution if you do not have parser installed) (2) if you want to write # in a character string, you must use double quotes, e.g. "here is a #" will be fine whereas 'here is a #' is not; if you want to use quotes in comments, please always use single quotes, e.g. # 'single quotes' (inline comments that contain double quotes will be dropped); if the parser package is available in your system (e.g. you installed it from the archived source on CRAN), everything will be the same as before
-
the default value for 'envir' in tidy.eval() was changed from globalenv() to parent.frame()
- \t will no longer be replaced with \t when keep.space=TRUE because it is dangerous to do so; see #17 for an example
-
a new argument 'reindent.spaces' for tidy.source() to reindent the code with a specified number of spaces (e.g. 2)
-
comments will be reflowed as a whole block when possible (instead of being wrapped line by line); thanks, Paul Johnson
-
when a comment block is reflowed, the second and following lines will not be indented
-
the default value of the 'width.cutoff' argument in tidy.source() is getOption('width') now; in the past it was 75% of that width which was less intuitive
-
part of the documentation of tidy.source() has been moved to https://github.com/yihui/formatR/wiki/
-
internally the comments are preserved by putting them in an expression invisible("# comments"); in past versions comments were retained in assignments; this change should not affect end users
- fixed #16: \ in comments are preserved correctly now
-
slight tweaks to the vignette (stopped Sweave from adding \usepackage{Sweave} which introduces ae by default)
-
fixed the error message in tidy.source(), pointing users to the wiki page on GitHub (thanks, Gabor Grothendieck)
-
functions unmask.source(), parse.tidy(), deparse.tidy() and the operator %InLiNe_IdEnTiFiEr% were marked as `internal' in documentation
-
the vignette is processed by the knitr package
-
fixed a buglet in usage() so it can process functions with dots correctly
- the parser package is imported (in previous versions formatR depends on parser); thanks, Romain Francois
- the function formatR() was renamed to tidy.gui() which is a more meaningful name since it is used to create a GUI
-
usage() will tell if the function is S3
-
a wiki is set up as the manual for formatR: https://github.com/yihui/formatR/wiki
-
tidy.eval() can evaluate the code in a specified environment now; see the 'envir' argument
- keep.blank.line is TRUE by default now (was FALSE in previous versions), i.e. blank lines are preserved by default
-
a new function tidy.eval(): evaluate R code and insert the output masked in comments (following ##)
-
the empty lines before 'else' will be removed even if keep.blank.line = TRUE; it is ill-advised to use blank lines among incomplete code chunks, e.g. if (TRUE) {'this is a BAD style of R programming'}
-
tidy.source() reports the line number when errors occur, which can help users detect the problem in the R code more quickly (thanks, Hadley Wickham)
- 'else ...' will be moved back to the last line so that we will no longer see an 'else' statement in a new line
-
formatR now uses the parser package to parse inline comments, which can guarantee that these comments will be correctly parsed (no longer uses the 'unsafe' regular expressions to parse comments, so forget about the previous rules of writing comments -- just write comments with an arbitrary number of spaces before # as you wish)
-
the use of parser also enabled a new feature: '=' can be replaced with '<-' wherever appropriate (for example, '=' in function arguments will not be replaced; only thoese equal signs which are used to assigning purposes can be replaced)
-
long roxygen comments will not be wrapped (i.e. comments begin with #' or ##')
-
fixed a minor problem in the function usage() (out --> output)
-
comments after { will be moved to the next line (in previous versions, these comments will cause errors)
- the escape character '' in comments of complete lines will be successfully preserved, which is especially useful for tidy.source() to format the roxygen comments since we usually write comments like "##' @author Someone \email{}" but "\e" is not a legal character in R (this will lead to errors in earlier versions of this package)
- a new function usage() to print the formatted usage of a function
- tidy.source() can wrap long comments into shorter ones now (this only applies to the whole lines of comments; the inline comments will not be wrapped since it is tricky to do so)
- '\t' will be parsed to ' ' when 'keep.space' is TRUE in tidy.source() (this might be undesirable, though)
-
new functions parse.tidy() and deparse.tidy() for the package pgfSweave to help tidy the source code in Sweave
-
a new function tidy.dir() to format all the R scripts under a directory
-
added a package vignette
-
full support to multi-byte characters in the formatR() GUI
-
a new function unmask.source() to obtain the real source code from the masked source
-
a new operator '%InLiNe_IdEnTiFiEr%' designed mainly for pgfSweave (mask the inline comments)
-
the inline comments will also be preserved in most cases (in earlier versions, only single lines of comments are preserved)
-
tidy.source() gained a new argument 'text' to accept a character vector as the source code
-
multi-byte characters are partially supported in the formatR() GUI now (full support will come in 0.1-7)