[ESS] Editing Rpad and other html/R files

Tom Short tshort at eprisolutions.com
Thu Dec 21 19:15:39 CET 2006


Rpad files are html files with R interspersed for making webapps (www.rpad.org).
It is possible to edit Rpad files with ESS's R-mode by adding the following to
your .emacs file:

(add-to-list 'auto-mode-alist '("\\.[Rr]pad\\'" . R-mode))

That works fine, but it's nice to be able to have emacs adjust it's syntax
highlighting depending on if you are editing the R part or the html part. That
is possible with two-mode-mode, available at (despite its name, two-mode-mode
can support multiple modes):

http://www.hst.aau.dk/net/Report_writing/Emacs_and_editors/Config_Emacs/two-mode-mode.el

Once that is installed, you can add the following to your .emacs file:

(require 'two-mode-mode)
(require 'css-mode)
(defun Rpad-mode ()
  "Treat the current buffer as an Rpad file (HTML with embedded R)."
  (interactive)
  (setq default-mode (list "HTML" 'html-mode))
  (setq second-modes (list
                      (list "R" "<pre dojoType=\"Rpad\"" "</pre>" 'R-mode)
                      (list "Rtextarea" "<textarea dojoType=\"Rpad\""
"</textarea>" 'R-mode)
                      ))
  (two-mode-mode))
(add-to-list 'auto-mode-alist '("\\.[Rr]pad$" . Rpad-mode))

Here's a more advanced version that adds javascript, css, and php into the mix: 

(require 'two-mode-mode)
(require 'css-mode)
(defun Rpad-mode ()
  "Treat the current buffer as an Rpad file (HTML with embedded R)."
  (interactive)
  (setq default-mode (list "HTML" 'html-mode))
  (setq second-modes (list
                      (list "R" "<pre dojoType=\"Rpad\"" "</pre>" 'R-mode)
                      (list "Rtextarea" "<textarea dojoType=\"Rpad\""
"</textarea>" 'R-mode)
                      (list "Javascript" "<script" "</script>"
'javascript-generic-mode)
                      (list "css" "<style>" "</style>" 'css-mode)
                      (list "php" "<?php" "?>" 'c++-mode)
                      ))
  (two-mode-mode))
(add-to-list 'auto-mode-alist '("\\.[Rr]pad$" . Rpad-mode))

Overall, the experience works relatively well. Occasionally, emacs gets mixed up
by the code in another section (especially by apostrophes). 

It is possible to adapt this approach to other mixed R/html filetypes, including
Sweave/html (in R2HTML) and R.rsp files. The key is adapting the regex strings
for finding the start and end of the code block.
http://www.emacswiki.org/cgi-bin/wiki/MultipleModes also has info on alternatives.

- Tom

Tom Short
EPRI Solutions, Inc.




More information about the ESS-help mailing list