knitr and Emacs

Just started to look into knitr. It still amazes me how fast new systems/programs come up to replace the old. RStudio is something I would have loved before I became attached to Emacs. But now that I can’t live without M-% replace, I can’t go back. Although if you are just starting to get into R, I would HIGHLY recommend it. I’ve been asked why I’m sticking with Emacs. I must say it’s the ability to adjust the layout however I like, the ease of navigating through the entire file without need of a mouse and the possibility of programming repeated commands (like macro in Excel) that’s made it impossible to move from Emacs. (Disclaimer: I haven’t used RStudio enough to say that it can’t do it, rather just been too lazy to learn another thing lol.)

ANYHOW…. there I go getting distracted from my initial point: knitr. From all the user comments, it seems like knitr is going to solve a lot of problems that I’ve struggled with using Sweave! Thank you Yihui Xie!

So first thing, before I can start learning how to use knitr (aside from reading the manual), I need to install it. Yihui uses LyX but I’d still like to stick with Emacs. After some google searches I came upon Simon Potter’s fix. I tried Simon Potter’s simple direction but given that I’m using Vincent Goulet’s ready-to-install Emacs/ESS package, it didn’t work. Here’s what I did in my initial attemp, I just downloaded Simon Potter’s ess-knitr.el file into a new folder “ess-knitr” in .emacs.d (a hidden folder in C:\Users\MyUsername\”) and added the follow lines to my .emacs file.

(add-to-list ‘load-path “~/.emacs.d/ess-knitr/”) ; change the last variable to specify ess-knitr.el file location
(require ‘ess-knitr)

This resulted in an error: “Symbol’s value as variable is void: noweb-minor-mode-map”. The reason behind the error (as hinted by Simon Potter) is that the lisp line needs to be read after ESS is initiated. I’m guessing that with Vincent Goulet’s Emacs, the initiation of ESS doesn’t occur in my default .emacs file. (ie:
my default .emacs file is loaded before the ESS initiation lisp code that Vincent included in his Emacs package.)

Here’s what I did to fix the issue:
First identify the code which initiates ess. It can be found in the “default.el” file under the “site-lisp” folder of GNU Emacs in program files (x86). ie “C:\Program Files (x86)\GNU Emacs XX.X\site-lisp\default.el”.
Identify the following code chunk in the default.el file.
(require ‘ess-site)
(require ‘ess-eldoc)
Below this code chunk, add the two lines from Simon Potter’s fix and restart Emacs. You should be good to go!

I have a big analysis job that needs to be done which would be an excellent opportunity to write a Rnw file that would streamline all the normalization steps with some quality control analysis in a reproducible report. But given the deadline, maybe I hope I’ll get it done in time …… That’s how I learned Sweave (under a tight deadline ><), so perhaps that's what I need to do with knitr.

Happy coding!

Update 2012-07-18: When copying the dark blue font above into the ‘default.el’ file, please beware of the single quotes and double quotes. When I tried to copy and paste into my ‘default.el’ file, I had trouble saving due to the fonts of the quotes (I modified my ‘default.el’ file in emacs.). Just simply retype the single or double quotes and it should work. Please don’t hesitate to drop me a line if you run into any trouble or see any errors!

Advertisements

Tags: , ,

About lucialam

attempts at organizing my thoughts and mind

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: