Aug 122007

This is my function for tidying up R code:

Downdload the file here

Actually it’s quite easy, though I didn’t know it before. When R was upgraded from 2.4.1 to 2.5.0, the function source() was also modified. In the past I used to make use of source(my_source_file, echo = TRUE, prompt = "") to “tidy up” my code because it’s not convenient for me to type every space between operators, what’s more, I have no fixed rules to break a line or make a proper indent. Thus I need a function to automatically “tidy up” my code.

After I’ve read the source code of the function source(), I quickly found that the most critical function is parse(), which can turn your code file into neat expressions, and the rest work is just to extract substrings.

tidy.source = function(file = choose.files()) {
   exprs = parse(file)
   for (i in 1:length(exprs)) {
       dep = paste(deparse(exprs[i]), collapse = "\n")
       dep = substring(dep, 12, nchar(dep) - 1)
       cat(dep, "\n")
   }
}

Related Posts

5 Responses to “Tidy up your R code”

Comments (5)
  1. Camellia says:

    Hello

  2. leffgh says:

    i want to learn from you in a more direct and comvenient way .
    is that possible

  3. leffgh says:

    sometimes it keeps me waiting for too long , and render the answer almost useless,for i have figoured out by myself.

Leave a Reply

(required)

(required)

WWW.YIHUI.NAME XIE@YIHUI.NAME © 2007 - 2012 by Yihui Xie