Tag Archive | knitr

LateX SWeave/Knitr naming file

OMGGGGGGGGGGGGGGGGGGGGGGGG this is just one of those days….. when you keep hitting the same problem over and over again. But miraculously under certain conditions, the problem is not there! Indicting that it’s not your computer set up that’s the issue. The problem lies with you but for the love of god you cannot figure out why!

I was modifying my Sweave files for knitr and trying to compile. I could run the files through knitr with no problem by M-n r after installing knitr (as per my previous post). But after when I’m running latex (C-c in emacs in the .tex file), I kept running into the problem of ERROR: LaTeX Error: Missing \begin{document}. I kept thinking it was my knitr conversion process that screwed up my preamble. After about an hour of struggle and double, triple checking a working file that I downloaded from the knitr webpage, and finding that although two files have the exact same content, one would work while another doesn’t. Turns out it’s because of the stupid file name!!!!! The saddest thing is that I knew about it before when I was working in Sweave…… \textbf{File names cannot have spaces!} I just forgot… damn brain!

Well I hope if you ever come across this problem, it won’t take you THAT long to figure it out. This is a clear indication that my brain is going to mush compared to before. I used to think that I would naturally remember these things but clearly not………………………………

At least now I know and hopefully I won’t forget next time. And I can start knitting(knitr-ing) :P~

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!