File: formatR.Rmd

package info (click to toggle)
r-cran-formatr 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 232 kB
  • sloc: sh: 10; makefile: 4
file content (250 lines) | stat: -rw-r--r-- 10,034 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!--
%\VignetteEngine{knitr::docco_linear}
%\VignetteIndexEntry{An Introduction to formatR}
-->

<script type="text/javascript">
if (location.protocol === 'https:' && location.href.match('yihui.name') === null)
  location.href = 'http://yihui.name/formatR';
</script>

```{r setup, include=FALSE}
knitr::opts_chunk$set(tidy = TRUE)
```

# An Introduction to formatR

## 1. Installation

You can install **formatR** from [CRAN](https://cran.r-project.org/package=formatR), or [XRAN](http://yihui.name/xran) if you want to test the latest development version:

```{r eval=FALSE}
install.packages('formatR', repos = 'http://cran.rstudio.com')
#' to install the development version, run
#' install.packages('formatR', repos = 'http://yihui.name/xran')
```

Or check out the [Github repository](https://github.com/yihui/formatR) and install from source if you know what this means. This page is always based on the development version.

```{r}
library(formatR)
sessionInfo()
```

## 2. Reformat R code

The [**formatR**](https://cran.r-project.org/package=formatR) package was designed to reformat [R](https://www.r-project.org) code to improve readability; the main workhorse is the function `tidy_source()`. Features include:

- long lines of code and comments are reorganized into appropriately shorter ones
- spaces and indent are added where necessary
- comments are preserved in most cases
- the number of spaces to indent the code (i.e. tab width) can be specified (default is 4)
- an `else` statement in a separate line without the leading `}` will be moved one line back
- `=` as an assignment operator can be replaced with `<-`
- the left brace `{` can be moved to a new line

Below is an example of what `tidy_source()` can do. The source code is:

```{r example, eval=FALSE, tidy=FALSE}
## comments are retained;
# a comment block will be reflowed if it contains long comments;
#' roxygen comments will not be wrapped in any case
1+1

if(TRUE){
x=1  # inline comments
}else{
x=2;print('Oh no... ask the right bracket to go away!')}
1*3 # one space before this comment will become two!
2+2+2    # only 'single quotes' are allowed in comments

lm(y~x1+x2, data=data.frame(y=rnorm(100),x1=rnorm(100),x2=rnorm(100)))  ### a linear model
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1  ## comments after a long line
## here is a long long long long long long long long long long long long long comment which will be wrapped
```

We can copy the above code to clipboard, and type `tidy_source(width.cutoff = 70)` to get:

```{r example, eval=FALSE, tidy.opts=list(width.cutoff=70)}
```

Two applications of `tidy_source()`:

- `tidy_dir()` can reformat all R scripts under a directory
- `usage()` can reformat the usage of a function, e.g. compare `usage()` with the default output of `args()`:
    ```{r collapse=TRUE}
    library(formatR)
    usage(glm, width=70)  # can set arbitrary width here
    args(glm)
    ```

## 3. The Graphical User Interface

If the **shiny** packages has been installed, the function `tidy_app()` can launch a Shiny app to reformat R code like this ([live demo](https://yihui.shinyapps.io/formatR/)):

```r
formatR::tidy_app()
```

[![R source code before tidying](http://i.imgur.com/lUgtEAb.png)](https://yihui.shinyapps.io/formatR/)

After hitting the `Tidy` button:

[![R source code after tidying](http://i.imgur.com/TBZm0B8.png)](https://yihui.shinyapps.io/formatR/)

## 4. Evaluate the code and mask output in comments

It is often a pain when trying to copy R code from other people's code which has been run in R and the prompt characters (usually `> `) are attached in the beginning of code, because we have to remove all the prompts `> ` and `+ ` manually before we are able to run the code. However, it will be convenient for the reader to understand the code if the output of the code can be attached. This motivates the function `tidy.eval()`, which uses `tidy_source()` to reformat the source code, evaluates the code in chunks, and attaches the output of each chunk as comments which will not actually break the original source code. Here is an example:

```{r comment=NA}
set.seed(123)
tidy_eval(text = c("a<-1+1;a  # print the value", "matrix(rnorm(10),5)"))
```

The default source of the code is from clipboard like `tidy_source()`, so we can copy our code to clipboard, and simply run this in R:

```{r eval=FALSE}
library(formatR)
tidy_eval()  # without specifying any arguments, it reads code from clipboard
```

## 5. Showcase

We continue the example code in Section 2, using different arguments in `tidy_source()` such as `arrow`, `blank`, `indent`, `brace.newline` and `comment`, etc.

### Replace `=` with `<-`

```{r example, eval=FALSE, echo=6, tidy.opts=list(arrow=TRUE)}
```

### Discard blank lines

Note the 5th line (an empty line) was discarded:

```{r example, eval=FALSE, echo=1:6, tidy.opts=list(blank = FALSE)}
```

### Reindent code (2 spaces instead of 4)

```{r example, eval=FALSE, echo=6, tidy.opts=list(indent = 2)}
```

### Move left braces `{` to new lines

```{r example, eval=FALSE, echo=6, tidy.opts=list(brace.newline = TRUE)}
```

### Discard comments

```{r example, eval=FALSE, tidy.opts=list(comment = FALSE)}
```

## 6. Further notes

The tricks used in this packages are very dirty. There might be dangers in using the functions in **formatR**. Please read the next section carefully to know exactly how comments are preserved. The best strategy to avoid failure is to put comments in complete lines or after _complete_ R expressions. Below are some known issues that `tidy_source()` may fail.

### In-line comments after an incomplete expression or ;

```r
1 + 2 + ## comments after an incomplete line
    3 + 4
x <- ## this is not a complete expression
     5
x <- 1; # you should not use ; here!
```

It is not a good idea to interrupt R code with comments and sometimes it can be confusing -- comments should come after a complete R expression naturally; by the way, `tidy_source()` will move the comments after `{` to the next line, e.g.

```{r comment-brace, tidy=FALSE, eval=FALSE}
if (TRUE) {## comments
}
```

will become

```{r comment-brace, eval=FALSE}
```

### Inappropriate blank lines

Blank lines are often used to separate complete chunks of R code, and arbitrary blank lines may cause failures in `tidy_source()` as well when the argument `blank = TRUE`, e.g.

```r
if (TRUE)

{'this is a BAD style of R programming!'} else 'failure!'
```

There should not be a blank line after the `if` statement. Of course `blank = FALSE` will not fail in this case.

### `?` with comments

We can use the question mark (`?`) to view the help page, but **formatR** package is unable to correctly format the code using `?` with comments, e.g.

```r
?sd  # help on sd()
```

In this case, it is recommended to use the function `help()` instead of the short-hand version `?`.

### `->` with comments

We can also use the right arrow `->` for assignment, e.g. `1:10 -> x`. I believe this flexibility is worthless, and it is amazing that a language has three assignment operators: `<-`, `=` and `->` (whereas almost all other languages uses `=` for assignment). Bad news for **formatR** is that it is unable to format code using both `->` and comments in a line, e.g.

```r
1:10 -> x  # assignment with right arrow
```

I recommend you to use `<-` or `=` consistently. What is more important is consistency. I always use `=` because it causes me no confusion (I do not believe it is ever possible for people to interpret `fun(a = 1)` as assigning `1` to a variable `a` instead of passing an argument value) and `<-` is more dangerous because it works everywhere (you might have unconsciously created a new variable `a` in `fun(a <- 1)`; see [an example here](https://stat.ethz.ch/pipermail/r-devel/2011-December/062786.html)). The only disadvantage is that most R people use `<-` so it may be difficult to collaborate with other people.

## 7. How does `tidy_source()` actually work?

The method to preserve comments is to protect them as strings in R expressions. For example, there is a single line of comments in the source code:

```r
  # asdf
```

It will be first masked as

```r
invisible(".IDENTIFIER1  # asdf.IDENTIFIER2")
```

which is a legal R expression, so `base::parse()` can deal with it and will no longer remove the disguised comments.  In the end the identifiers will be removed to restore the original comments, i.e. the strings `invisible(".IDENTIFIER1` and `.IDENTIFIER2")` are replaced with empty strings.

Inline comments are handled differently: two spaces will be added before the hash symbol `#`, e.g.

```r
1+1#  comments
```

will become

```r
1+1  #  comments
```

Inline comments are first disguised as a weird operation with its preceding R code, which is essentially meaningless but syntactically correct!  For example,

```r
1+1 %InLiNe_IdEnTiFiEr% "#  comments"
```

then `base::parse()` will deal with this expression; again, the disguised comments will not be removed. In the end, inline comments will be freed as well (remove the operator `%InLiNe_IdEnTiFiEr%` and surrounding double quotes).

All these special treatments to comments are due to the fact that `base::parse()` and `base::deparse()` can tidy the R code at the price of dropping all the comments.

## 8. Global options

There are global options which can override some arguments in `tidy_source()`:

| argument        | global option                      | default |
|-----------------|------------------------------------|---------|
| `comment`       | `options('formatR.comment')`       | `TRUE`  |
| `blank`         | `options('formatR.blank')`         | `TRUE`  |
| `arrow`         | `options('formatR.arrow')`         | `FALSE` |
| `indent`        | `options('formatR.indent')`        | `4`     |
| `brace.newline` | `options('formatR.brace.newline')` | `FALSE` |

Also note that single lines of long comments will be wrapped into shorter ones automatically, but roxygen comments will not be wrapped (i.e., comments that begin with `#'`).