How to use the R parser to format R code

Add the following to your .emacs file (from ESS mailing list):

(defun ess-indent-region-as-R-function (beg end)
  (let ((string (replace-regexp-in-string
		 "\"" "\\\\\\&"
		 (replace-regexp-in-string ;; how to avoid this double matching?
		  "\\\\\"" "\\\\\\&" (buffer-substring-no-properties beg end))))
	(buf (get-buffer-create "*ess-command-output*")))
    (ess-force-buffer-current "Process to load into:")
    (ess-command (format "local({oo<-options(keep.source=FALSE);
options(oo)})\n"  string) buf)
    (with-current-buffer buf
      (goto-char (point-max))
      ;; (skip-chars-backward "\n")
      (let ((end (point)))
	(goto-char (point-min))
	(goto-char (1+ (point-at-eol)))
	(setq string (buffer-substring-no-properties (point) end))
  (delete-region beg end)
  (insert string)

(add-hook 'ess-mode-hook '(lambda () (set (make-local-variable 'indent-region-function)

It is then possible to format R code with the R parser: select a region, and press ‘C-M-\’ (’M-x indent-region’). If there is no R process associated to the buffer, a new one is launched. Note that the code in the region should be syntactically correct.