Animated Statistics Using R (to be moved to AniWiki!)
Highlight is a software tool for formatting source code in many languages, including R.
About Hightlight

Highlight is a freeware for converting source code to formatted text with syntax highlighting by André Simon; it is released under the terms of the GNU GPL license. It supports more than 100 programming languages, including R. You may download it freely from this page.

I'm using Highlight because it will be far more convenient to read those boring (!!!) program codes. The colors and font styles will be of great help to us.

Hightliehg for R Language

By default the definition file for R in Hightlight is far from complete, but it's very easy to complement the definition, as we only need to define the keywords of R. I've written the detailed method in one of my blog pages here: http://www.yihui.name/en/read.php/6.htm

Actually the extraction of R keywords (mainly funtions) can be easily realized in R itself, and the method I mentioned in the above blog page is just like:

  1. lst = search()
  2. lst = lst[grep("package:", lst)]
  3. ########################################################
  4. # "package:stats" "package:graphics" "package:grDevices"
  5. # "package:utils" "package:datasets" "package:methods"
  6. # "package:base"
  7. ########################################################
  8. x = NULL
  9. for (i in 1:length(lst)) x = c(x, ls(lst[i]))
  10. y = x[-grep("[]\\\ \\|\\(\\)\\[\\{\\^\\$\\*\\+\\?~#%&=:!/@<>-]",
  11.    x)]
  12. # remove "functions" with special symbols using regexp
  13. kw = c("if", "else", "repeat", "while", "function",
  14.    "for", "in", "next", "break", "ifelse", "switch", "NULL",
  15.    "NA", "Inf", "NaN", "TRUE", "T", "FALSE", "F")
  16. # remove some constants, etc
  17. for (i in 1:length(kw)) y = y[y != kw[i]]
  18. sink("c:/func.txt")   # save the result
  19. cat(y, sep = " ") # format the result as highlight needs
  20. sink()

I have modified the above code and put it in the package 'animation' now: the function highlight.def() can dynamically generate such a definition file.

The R definition file for Highlight can also be downloaded there.