July 12, 2014

Although my job title is ‘Lecturer in Theoretical Phonology’ (no, really, I’ve been getting into more empirical work. I went on a data collection trip to South Wales in February to work with Welsh, and only early this week we had two great days collecting Gaelic data in North Argyll – not an area readily associated with Gaelic these days, but in fact we found quite a few very good speakers.

Anyway, this post isn’t about that. Having collected the data and processed some of it, I have also been getting to grips with actually doing statistical analysis (as opposed to working through textbooks), which these days inevitably means R. I’m giving a couple of talks in Wales next week (here and here) with some of the results, so the time had come for me to think about getting my data into my presentations (and eventually papers). Being a LaTeX user, after some poking about I settled on knitr as the way to get stuff out of R and into LaTeX.

Most of the R wizards I know use RStudio, which is a very good IDE for R, but doesn’t come close to the power of Emacs with AUCTeX for LaTeX writing. So far I have found ESS to be perfectly adequare for my R needs. ESS also supports knitting .Rnw files out of the box via M-n r. Its default weaving engine, however, is Sweave. After some poking about, here’s how to get ESS and AUCTeX to play nicely with knitr.

There are two components. One is to make knitr the default engine when tangling outside AUCTeX. This is easily achieved via (setq ess-swv-processor 'knitr) in your init.el, but this only allows you to use knitr as the default when you tangle via M-n T. What this doesn’t automate is running LaTeX on the product of the knitting: you have to switch to the .tex file manually and run stuff from there, but this runs the risk of making edits to the .tex file instead of the .Rnw. ESS also has a latex tangling option (M-n l), but this just runs latex from the shell, without any of the AUCTeX customizations.

ESS also has the option to plug into AUCTeX, making relevant commands available to the user via C-c C-c. This is much handier, but unfortunately the functions which add these commands are hardcoded to use Sweave from the shell rather than ess-swv-run-in-R, which is sensitive to the value of ess-swv-processor. So here’s what you need in order to get knitr to run into AUCTeX.

;; somewhere after (require 'ess-site)
(setq ess-swv-plug-into-AUCTeX-p t)

(defun ess-swv-add-TeX-commands ()
  "Add commands to AUCTeX's \\[TeX-command-list]."
  (unless (and (featurep 'tex-site) (featurep 'tex))
    (error "AUCTeX does not seem to be loaded"))
  (add-to-list 'TeX-command-list
               '("Knit" "Rscript -e \"library(knitr); knit('%t')\""
                 TeX-run-command nil (latex-mode) :help
                 "Run Knitr") t)
  (add-to-list 'TeX-command-list
               '("LaTeXKnit" "%l %(mode) %s"
                 TeX-run-TeX nil (latex-mode) :help
                 "Run LaTeX after Knit") t)
  (setq TeX-command-default "Knit")
  (mapc (lambda (suffix)
          (add-to-list 'TeX-file-extensions suffix))
        '("nw" "Snw" "Rnw")))

(defun ess-swv-remove-TeX-commands (x)
  "Helper function: check if car of X is one of the Knitr strings"
  (let ((swv-cmds '("Knit" "LaTeXKnit")))
    (unless (member (car x) swv-cmds) x)))

These are just little changes to the original functions defined in ess-swv.el. Now you can, via C-c C-c, access Knit (which just produces the .tex file) and LaTeXKnit, which runs the LaTeX process as customized by AUCTeX (e.g. to use XeTeX or whatever other options you normally use) on the output, without you having to go the .tex file. Which is nice.


Blog archive  |
 
comments powered by Disqus
 

About me

I’m Pavel Iosad, and I'm a Lecturer in Theoretical Phonology in the department of Linguistics and English Language at the University of Edinburgh. ¶ You can always go to the start page to learn more.

elsewhere

Updates

Subscribe to the  RSS feed, or follow me on Twitter at  @anghyflawn.

Search