Emacs: The all-in-one R, Sweave, LaTeX, and BibTeX editor

April 6, 2009

Emacs is a cross-platform, highly extensible, open source text editor developed by this guy. It is the way to go if you use R and LaTeX. If you want a vanilla version of the package go to the above link, but if you want Emacs already preloaded with a bunch of goodies try the following for Mac OS X or this one. For Windows, this one has a bunch of nice features that enables it to better integrate with Windows but it doesn’t come with ESS so you might prefer this one which comes with ESS. For Linux, it’s usually a simple apt-get install emacs-gtk, uprmi emacs, zypper in emacs, or yum install emacs (you get the picture) with Linux you’ll also need to install ESS. Also, to run LaTeX make sure you have a LaTeX bundle installed.

Here are some useful Emacs tips for using R (you could also use this cheat sheet:

First thing you should do is create a file in Emacs and save it with the extension .Rnw (if you want to do Sweave) or .R (if you just want it to be a R script). For example, test.Rnw (though you could save it as test.R).
Then you can add R code. To run the code you can either invoke R in Emacs as described below, click the run code icon, or just have Emacs invoke R when you run your first line of code by typing C-c C-n. That’s it.

M-x R
(Starts R in Emacs)

M-n s

(Runs Sweave, but Emacs needs to know where Sweave is … so if you’re running Linux everything (I should be careful here, I guess I mean Debian or Ubuntu) is set up fine but in Windows or Mac OS X you have to tell Emacs where Sweave.sty is located. This is included with R. Locate the file and create a softlink in Mac OS X to the directory where your other *.sty are or you can copy the Sweave.sty file to a directory on your computer such as “/Users/me/Sweave/Sweave.sty” and specify the full path to the Sweave file in your preamble.

(Go to previous line)

If you use scholar.google.com, BibTeX is the way to go.  Set up the scholar preferences to “show links to import citations into BibTeX”. Then copy and paste the reference into a reference file with the file extension *.bib. Then all you need to do is call the reference from Emacs when you cite it in LaTeX.

For Sweave put your R code in the following

x <- c(1,2,3,4,5)

If you want to include comments

# Assign 1 through 5 to X
x <- c(1,2,3,4,5)

For figures use,


Also you must first “Sweave” a document, then run “Latex”, then run “Bibtex”, then run “Latex” again. Small price to pay for a well integrated way of creating documents.



  1. Hello,
    I just posted an article about how to include Sweave functionality in WordPress blog posts, in addition to org-mode integration.

    If you use Sweave, you might find it useful:


  2. Hello Friend,

    I really need to learn how to use R under Emacs. Could you please let me know how to do it? Is there any manual that explain it in plain way that I can use it. Thanks a lot

    • Google ESS or Emacs Speaks Statistics. Specifically google for the ESS cheat sheet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: