Switch branches/tags
Nothing to show
Find file History
Latest commit a524233 Aug 19, 2018


Using R with Emacs and ESS - A Multi-functional Environment

This Org file explains the Emcas configuration to write code in the R langues in Emacs. For more information go to The Devil is in the Data.

This org file shows the elisp code for a minimal configuration to write R code in Emacs. You can write all relevant code to an init file (tangle) with the C-c C-v C-t keystrokes.

Installing packages

This first snippet adds the Melpa repository and installs the required packages, but it is not saved to the init file as we only need to execute this once. Press C-c C-c to evaluate this code block. ESS will ask confirmation to run the code.

; Configure repository
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
; list the packages
(setq package-list
    '(ess ess-smart-underscore org-ref auto-complete))

; activate all the packages

; fetch the list of packages available 
(unless package-archive-contents

; install the missing packages
(dolist (package package-list)
  (unless (package-installed-p package)
    (package-install package)))

Configure ESS

Emacs Speaks Statistics (ESS) supports editing of scripts and interaction with various statistical analysis programs such as R. You also need to install the R software. You can start an R terminal with M-x R and enter the preferred working directory.

In ESS, the underscore key is mapped to the <- assignment operator in R. If you need an underscore, you need to type it twice. This functionality can be annoying when you are an avid user of ggplot. The ess-smart-underscore package solves this issue by allowing a single underscore in certain circumstances.

(require 'ess-site)
(require 'ess-smart-underscore)

Org Babel

Org Babel is the enables literate programming, including the functionality in this file.

;; Active the R language in Babel
 '((R . t)))

Auto complete

;; Auto complete
(require 'auto-complete-config)

Org LaTeX and BibTeX

;;Org BibTeX references
(require 'org-ref)
(org-ref-define-citation-link "citeA" ?a)
(setq org-latex-pdf-process
      '("pdflatex -interaction nonstopmode -output-directory %o %f"
	"bibtex %b"
	"pdflatex -interaction nonstopmode -output-directory %o %f"

;; Org Mode Latex Templates
(require 'ox-latex)

;; American Psychological Association papers
(add-to-list 'org-latex-classes '("apa6"
    "\\documentclass[a4paper, jou]{apa6}
	       ("\\section{%s}" . "\\section*{%s}")
	       ("\\subsection{%s}" . "\\subsection*{%Rs}")