Structuring a Statistical Analysis with R Using Emacs/ESS Structuring a Statistical Analysis with R Using Emacs/ESS r r

Structuring a Statistical Analysis with R Using Emacs/ESS


In general I use org-mode and org-babel, but when I've got to share scripts with others, I've got the following in my .emacs :

(defgroup ess-jb-faces nil  "Faces used by cutomized ess-mode"  :group 'faces)(defface ess-jb-comment-face  '((t (:background "cornsilk"     :foreground "DimGrey"    :inherit font-lock-comment-face)))  "Face used to highlight comments."  :group 'ess-jb-faces)(defface ess-jb-comment-bold-face  '((t (:weight bold    :inherit ess-jb-comment-face)))  "Face used to highlight bold in comments."  :group 'ess-jb-faces)(defface ess-jb-h1-face  '((t (:height 1.6     :weight bold     :foreground "MediumBlue"    :inherit ess-jb-comment-face)))  "Face used to highlight h1 headers."  :group 'ess-jb-faces)(defface ess-jb-h2-face  '((t (:height 1.2     :weight bold     :foreground "DarkViolet"    :inherit ess-jb-comment-face)))  "Face used to highlight h2 headers."  :group 'ess-jb-faces)(defface ess-jb-h3-face  '((t (:height 1.0     :weight bold     :foreground "DarkViolet"    :inherit ess-jb-comment-face)))  "Face used to highlight h3 headers."  :group 'ess-jb-faces)(defface ess-jb-hide-face  '((t (:foreground "white"    :background "white"    :inherit ess-jb-comment-face)))  "Face used to hide characters."  :group 'ess-jb-faces)(font-lock-add-keywords 'ess-mode   '(("^###\\(#\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h1-face t))     ("^###\\(##\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h2-face t))     ("^###\\(###\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h3-face t))     ("^###\\( .*\\|$\\)" 1 'ess-jb-comment-face t)     ("^###" "\\*.*?\\*" nil nil (0 'ess-jb-comment-bold-face append))     ))

With this, any comment with #### at the beginning of the line is formatted as an "header 1". Any comment with ##### is formatted as an "header 2", etc. And lines which begin with ### is seen as a comment with a special font-lock (used for long comments).

This can give something like this :

enter image description here

This is quite hacky, but the advantage is that it only uses standard R comments and as such can be shared without problem with others. In general I use the following for "header 1" : others see it as below, while I enjoy my defined font-lock :

################################################ HEADER 1############################################

With this syntax, you can also use the following to activate outline-minor-mode on the sections defined previously and be able to fold/unfold them :

(add-hook 'ess-mode-hook      '(lambda ()         (auto-complete-mode nil)         (outline-minor-mode 1)         (setq outline-regexp "\\(^#\\{4,6\\}\\)\\|\\(^[a-zA-Z0-9_\.]+ ?<- ?function(.*{\\)")         (setq outline-heading-alist           '(("####" . 1) ("#####" . 2) ("######" . 3)             ("^[a-zA-Z0-9_\.]+ ?<- ?function(.*{" .4)))))

All this code has not been very well tested, and I'm far from an expert in emacs lisp, so there should be better ways to do it, and don't be surprised in case of bugs !


Recent (Feb 2013) additions to Orgmode mean that you should now be able to embed org headings in your source code, and then navigate through them using orgstruct-mode. So, upgrade your org mode via git, and then try opening the following example R file. When you are on a comment line that has an org heading embedded, just hit TAB, or shift-TAB, and you should get org-mode headings.

### * Create datadata = list( s1=list(x=1:3, y=3:1),      s2=list(x=1:5, y=1:5), s3=list(x=1:4, y=rep(3,4)))### * Base graphics versionpar(mfrow=c(2,2))lapply(data, plot)### * Lattice versionnplots <- length(data)pts.per.plot <- sapply(data, function(l) length(l$x))df <- data.frame(which=rep(1:nplots, times=pts.per.plot),                 x=unlist(sapply(data, function(l) l$x)),                 y=unlist(sapply(data, function(l) l$y)))xyplot(y~x|which, data=df, layout=c(2,2))### ** Make the pdfpdf(file='o.pdf')xyplot(y~x|which, data=df, layout=c(2,1))                 dev.off()### * End of file### Local Variables:### eval: (orgstruct-mode 1)### orgstruct-heading-prefix-regexp: "### "### End:


Sounds like you have one big analysis script. That's not very optimal...

Think about redoing it with RMarkdown or Sweave, and using knitr to run it. Then you can use emacs' tools for navigating markdown or TeX and you can use the caching system in knitr to save having to do analyses right from the start when you've changed something near the end.

You also get a formatted report out of the analysis, if you want.

Also, breaking stuff out into separate files for sourcing is a bit sub-optimal - far far better to write functions and put them in a package - and to use the devtools package to make working with it really simple. Just edit your .R files and load_all updates it (none of the complex package build stuff of times past).

Big winnage.