Tag Archive | ess

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!

Another Emacs trick – quick text swap

I’m currently attending the “R / Bioconductor for high-throughput sequence analysis” workshop at the Fred Hutchinson Research Centre in Seattle. I have always enjoy attending these workshops even during the “introduction to R” sections since I really enjoy seeing other people’s coding styles. There are so many nifty tricks with R, emacs…etc that I’ll never learn all the function out there. Furthermore, there are things that I go do in a round-about way but I’ve done it so many times, I just assume it’s logical. (Such as mapply for applying the apply functions to multiple objects!) I need to make sure I don’t become complacent with what I do know and to always keep an eye out for new things.

Saw the instructor using this trick and just couldn’t help myself and had to ask. So the quick text manipulation tip for emacs: To switch between two characters, C-shift-t. Whereas to switch between two words on the different sides of a comma, use C-M-t! This applies to not just your *R* buffers but in almost all buffers!

Lately I feel like I enjoy doing silly little R acrobatics more than any serious data analysis. Not sure if I should be happy that I’ve got another clue towards narrowing down that I enjoy doing or should I be depressed that I like those little things more than the more “interesting” work…….

Anyhow, I’m happy with enjoying the little things (thanks Zombieland). Any other favourite R or emacs or ess or programming tricks? Always happy to learn more! Happy coding people!

Reinstalling Emacs/ESS

I must say I’m not very computer savvy (compared to all those computer gurus out there). I don’t do windows update. I don’t update PERIOD………… unless the software automatically does it. Unfortunately, with use of R and Bioconductor, their packages tend to be frequently updated with new releases of the software. So when I install a new package, often I’ll get a warning saying the package was build for system XX.X – the newest release which I have NOT installed, pretty much saying that if I don’t update and my scripts does something wonky it’s not their fault.

For the sake of keeping up to date with the packages and know that I’m not running some out of date, potentially messed up scripts, I’m planning to update more frequently. I don’t update because I tend to forget how I set up the program the first time and have to learn how to fix little bugs ALL OVER AGAIN (stupid brain… shouldn’t you remember these things). So here’s another remind myself in the future type of post: HOW TO REINSTALL EMACS in Windows 7.

Sounds simple. Which it is. But it’s the little nitty details of getting it to work the way I want that is the issue. As you might be aware from my other post that I’m using Vincent Goulet’s emacs distribution. He updates this whenever new versions of ESS and Emacs are made available I believe.

Step 1) uninstall previous version of emacs and install the latest package from his website.

Next I want to associate all my .R files to GNU Emacs.

Step 2) Click START and type “set association” in the search box, then click “Change the file type associated with a file extension”. Scroll down to .R flies, select  and click [Change program…]. From here, go to the Emacs program folder, ie. C:\Program files (x86)\GNU Emacs xx.x\bin, with xx.x replaced by the Emacs version number, and select emacsclientw.exe.

When I tried to double-click any .R file, an error pops up. In order for this to work, the environment variable ALTERNATE_EDITOR needs to be set torunemacs.exe.

Step 3) Go to system PATH. Right click on [My Computer] and select [Properties] -> [Advance system settings] -> Under the |Advance| tab, select [Environment Variables…].

Step 3.1) Under “User Variables for XXX” (XXX represents your user name), look for a variable called “ALTERNATE_EDITOR”. If present, select it and click [edit], if not, click “New…”. Write “ALTERNATE_EDITOR” as Variable name; while typing in the location of the runemacs.exe file (ie. C:\Program Files (x86)\GNU Emacs 23.4\bin\runemacs.exe) for Variable value. Click OK.

Also, it would be ideal to add the emacs bin into PATH.

Step 3.2) Under “System variables”, select “Path” and click edit. This contains a list of locations for the system to look when searching for a program. The bin folder (as per above in step 2) contains all the emacs program executables (.exe files) so add this to the end of the variable value and separating it with a semicolon between the new folder and the previous will work. I would recommend, copy and paste the entire original variable value for path into notepad or something. Save it, then add “;C:\Program files (x86)\GNU Emacs xx.x\bin” to the end. Copy and paste this entire thing into the Variable value slot and giving it a try. If things start acting funny, at least you have the original path values saved and can revert it back. Click OK to save the changes.

So at this point, you should be able to click on a .R file and emacs should start. Yes you can choose emacs.exe as the default program, but unfortunately for me, a terminal also opens up when files are opened that way, taking up space in my taskbar which drives me nuts! (Never had this issue before but I don’t remember if I used to select emacs.exe or emacsclientw.exe as default program).

Also, if you want any new files to open in any existing Emacs sessions, add (server-start) to your .emacs file and that seemed to work for me.

Hopefully things will be easier next time. This is by no means an ultimate guide to reinstalling emacs. There are probably lots of redundancies and things that didn’t need to be done, but it worked for me this time and maybe it’ll help you. I gathered these information from various websites such as this and this. Thanks google and all the kind folks who leave info out there to help others. What will I do without internet and google………… lol. Happy coding everyone!

Go away auto-fill!

An annoying fill mode of emacs:
Any line that is too long in any text file, emacs will automatically insert a text break when you press space or enter. This is super annoying when I’m typing in scripts or command that’s suppose to go into shell. The reason being is that I’m using Vincent Goulet’s emacs distribution (which already comes with Auctex and ESS). Although his distribution makes life super easy for n00bs like me (Thx Vincent!!!), but he also included extras that I didn’t want.

So this auto-fill mode has been made to turn on by default whenever emacs is editing any text or related files. The lisp line is as such:

(add-hook ‘text-mode-hook ‘turn-on-auto-fill) ; wrap long lines in text mode

The hook, from my understanding, basically means that this change is only applied to text files.

This line is located in the “site-lisp/site-start.el” file located in your GNU emacs folder. Just add a semicolon in front of the line to comment it out and subsequently turn it off. YAY!~

It’s because of its presence, that why when I tried to modify my own .emacs file with the following:

(add-hook ‘text-mode-hook
‘(lambda () (auto-fill-mode 1)))

as demonstrated by emacs Init-example file, it still didn’t work. I’m guessing it gets overridden by the site-start.el.

I’ve also seen someone mention seeing some fill-mode changes in Rnw files depending on the location of the cursor. I’ll keep an eye out next time when I’m Sweaving and see if it bugs me or not.

Just FYI~ ^^ Happy coding everyone! (Or attempt at it in my case lol)

PS: If you’re just starting to use Emacs and ESS, GET VINCENT’S EMACS DISTRIBUTION here. Just scroll to the bottom to download. It is absolutely painless to install and makes the already difficult switch from WYSIWYG type of editor to emacs so much easier. I strongly encourage people to give emacs a try (or alternatively Vi, where there’s tons of users advocating it. Many claims it’s even better than emacs. I’ve never used it so I’m woefully unqualified to jump into that hot debate so I’m not even gonna try). Once you get the hang of it, you’ll wish every single program you use works that way!