Jan 062011

A new version of the formatR package is available on CRAN now (binary packages are still on the way). There are three major updates:

  1. the inline comments will also be preserved in most cases (in earlier versions, only single lines of comments are preserved)
  2. tidy.source() gained a new argument 'text' to accept a character vector as the source code
  3. multi-byte characters are supported in the formatR() GUI now (sorry, this is not completely true in 0.1-6; it has been fixed in 0.1-7)

The first feature is a request from Cameron, which is actually from another request  of another user. I also feel this is a necessary feature even from the first version of this package, but dealing with inline comments is not as easy as the single lines of comments, and it can be dangerous. Please read the help page of the function tidy.source() for all the dark and dirty tricks for preserving R comments when formatting the R code. Here is a quick example:

> library(formatR)
> src = c("# a single line of comments is preserved",
+     "1+1", "if(TRUE){",
+     paste("x=1  ", "# comments begin with at least 2 spaces!"),
+     "}else{", "x=2;print('Oh no... ask the right bracket to go away!')}",
+     "1*3 # this comment will be dropped!")
>
> ## source code
> cat(src, sep = "\n")
# a single line of comments is preserved
1+1
if(TRUE){
x=1   # comments begin with at least 2 spaces!
}else{
x=2;print('Oh no... ask the right bracket to go away!')}
1*3 # this comment will be dropped!

We can reformat the code as:

> ## the formatted version
> tidy.source(text = src)
# a single line of comments is preserved
1 + 1
if (TRUE) {
    x = 1   # comments begin with at least 2 spaces!
} else {
    x = 2
    print("Oh no... ask the right bracket to go away!")
}
1 * 3
Dec 032010

The formatR package has been silent for quite a few months now. Recently I’ve been moving my old packages from R-Forge to GitHub, and I finally killed several things on my TODO list. In the past, I made an awkward decision to let formatR depend on the animation package, which was ridiculous. Ronggui suggested me remove this dependency long time ago, and now it came true in the new version 0.1-5 (on CRAN now). So formatR is finally a standalone package, although you can choose to use the GUI version after installing the gWidgets package (see the formatR() function). To work with command lines, tidy.source() can be helpful. It takes a file, formats it into a tidy form, and writes the output into a specified file, or simply to the console.

When I first saw the Pretty-R tool provided by Revolution Analytics, I was not completely satisfied because it is just not “pretty” enough. If they have R and the formatR package installed on the server, perhaps they can provide another option to help the users format their source code in a more beautiful way, e.g. they could have turned

x = seq(0.1,10,0.1)

to

x = seq(0.1, 10, 0.1)

(You may ask: what the heck is the difference?! In this case, you don’t need the formatR package because you are not “picky” enough…)

At the same time, I also removed the function tidy.source() from the animation package. Since this was my first R package, I tended to put everything into a single package, including some functions which are apparently irrelevant. I put them there just for my own use. Now all of them are defunct; see ?'animation-defunct' in the next version of the animation package.

Apr 132010

It is not uncommon to see messy R code which is almost not human-readable like this:

 # rotation of the word "Animation"
# in a loop; change the angle and color
# step by step
for (i in 1:360) {
 # redraw the plot again and again
plot(1,ann=FALSE,type="n",axes=FALSE)
# rotate; use rainbow() colors
text(1,1,"Animation",srt=i,col=rainbow(360)[i],cex=7*i/360)
# pause for a while
Sys.sleep(0.01)}

Apparently it is pain reading unformatted R code, but on the other hand, it is natural for us to be lazy. I don’t care about adding spaces or indent to my raw R code — I’ll concentrate on programming first and format my code later. The R package ‘formatR‘ is intended to help us format our messy R code.

# formatR optionally depends on gWidgetsRGtk2
# please use the latest version of R (>=2.12.0)
install.packages('formatR')
library(formatR)
formatR()

## you will get an error if the package gWidgetsRGtk2 is not installed;
## then you need to install it
install.packages('gWidgetsRGtk2')
formatR('RGtk2')

Then you can either paste your code into the text box or click the “Open” button to open an existing R code file. Click the “Convert” button and you are done!

formatR: unformatted R code

formatR: unformatted R code

formatR: tidy R code

formatR: tidy R code

There are several options in the “Preferences” panel, e.g. you can specify whether to keep comments or blank lines, or specify the width of the formatted R code.

No matter how messy your code looks like, formatR can make it tidy and structured as long as there are no syntax errors in your R code. If you prefer the command line interface, you may want to take a look at the function tidy.source() in this package.

Note that multi-byte characters (say, Chinese) are also supported in the GUI.

Mar 232010

Here are some (trivial) R tips in the course Stat 511. I’ll update this post till the semester is over.

  1. Formatting R Code

  2. I’ve submitted an R package named formatR to CRAN yesterday. This package should be easier than the code below, because there is a GUI to tidy your R code. Install with install.packages('formatR').

    Reading code is pain, but the well-formatted code might alleviate the pain a little bit. The function tidy.source() in the animation package can help us format our R code automatically. By default it will read your code in the clipboard, parse it and return the well-formatted code. You have options to keep or remove the comments/blank lines and set the width of the code, etc. Spaces and indent will be added automatically. This can save us time typing spaces and paying attention to indent.

    ## install.packages('animation') if it is not installed yet
    library(animation)
    ## copy some R code somewhere and type:
    tidy.source()
    ## or specify the path of your code file
    tidy.source(file.path(system.file(package = "graphics"), "demo", "image.R"))
    ## can also use a URL
    tidy.source('http://www.public.iastate.edu/~dnett/S511/twofactor.R')
    ## remove blank lines
    tidy.source('http://www.public.iastate.edu/~dnett/S511/twofactor.R',
               keep.blank.line = FALSE)
    ## remove comments
    tidy.source('http://www.public.iastate.edu/~dnett/S511/twofactor.R',
               keep.comment = FALSE)
    
WWW.YIHUI.NAME XIE@YIHUI.NAME © 2007 - 2012 by Yihui Xie