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!

QuickCam Fusion on Windows 7

Using the logitech website’s driver does not work with Windows 7 on a 64bit machine. In order to get it working:
1) Download software from microsoft:
2) Open folder and unzip the file within
3) Plug in camera
4) Go to device manager and select the “other device” with the question mark
5) Select install driver by browing your computer and go to the logitech folder
6) Install and it’ll work!
Thanks to qcamfusi0nuser on the logitech forum with the answer to this problem!
Happy skyping!

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!

Snowboarding season 2011/12 here I come!!!

Yes, I’m weird that way. Or you can say I’m obsessed. (I blame it on @burtonsnowboard twitter feed ^^.) Whenever school is back in session, my internal snowboarder clock starts the count down, signifying the end of summer and the start of winter (I just noticed that autumn is non existent in my calender…..) Just talking about it has me aching to glide down that mountain of white fluffy stuff!! The crisp winter air whipping across my face; that super early morning sky; the adrenaline rush…. I CANNOT WAIT! So, in the mean time while I suffer, here’s an overview and some tips on the three local ski hills.

Here in rainy Vancouver, we’re lucky to have access to three fabulous ski hills (all within 30 mins from my place). Cypress has the largest range of runs and is definitely my personal favorite! Something about having thoroughly bruised your knees and ankles on the same mountain…. correction, TWO mountains (cypress has two hills: Black Mountain and Mt. Strachan) for two years straight makes that place very special. If you will go there at least twice a year, and if the first time you go is during any peak season (Christmas and New Years period or weekends), make sure you go grab a gold metal card! Buy it from any Bell store before you go, this will be the best discount/deal that you can get for Cypress! The card includes the lift ticket for free, plus 20% discount on all following ticket purchase. Each person needs their own card but still worth it for that huge range of terrain.

Grouse is excellent for the family and people visiting Vancouver. The gondola ride up gives you a great view of Vancouver. They have an outdoor ice rink just by the main building, plus an awesome sleigh ride around  Christmas time. The Observatory is a great place to end any evening with a scenic fine dining experience. Don’t forget the annual 24hr ski: take advantage of the gorgeous view from the cut for a sun rise ski!

If you are a girl, you’re in luck! Seymour has an annual fundraiser between January and March: ladies night free ski every Monday for the breast cancer society. I’ve heard some hardcore snowboarders claim that Seymour has the “sickest” snow among the three local mountains! Also it doesn’t hurt that it’s the cheapest of the three~

Hope this tip/ info will be useful to someone out there. I’ll do another info blog of other ski hills around the area (Whistler for sure!) later. Until then, I’ll keep a lookout for snow-capped mountains!

Methylumi data import

I cannot believe how long this took to figure out. Guess this happens when you haven’t used it for ages, you are too lazy to re-read the manual and you give up everytime you hit the same problem. (In my defense… I gave up because I had a perfectly working alternative, albeit longer, script that did the exact same thing.) So why should I spend time fixing it?….. Because I won’t need to make little changes to the script everytime my data is slightly different which equals = SAVE TIME IN THE LONG RUN!

This is just a small tip on what is needed in order to input files using the methylumi package from Bioconductor to input Illumina Infinium HumanMethylation450 data into R. This also works for HumanMethylation27 data as well. I kept hitting these two errors when using the methylumiR function so I’m writing down what happened and how to solve this as a reminder for next time. The methylumiR function allows you to create a methyLumiSet object which contains slots for the following:

  • phenoData – variables that describe the samples
  • featureData – description of each probe
  • exprs – average beta values which corresponds to the percentage of DNA methylation at each CpG site
  • pvals – detection P values
  • unmethylated – signalA intensities which corresponds to the unmethylated intensity
  • methylated – signalB intensities which corresponds to the methylated intensity

1) In genomeStudio, export the entire “Sample Methylation Profile” data set. Include all columns (including all the probe annotation columns); For subcolumns, only “AVG_Beta”, “Signal_A”,”Signal_B” and “Detection Pval” are necessary. Let’s call this the “all.txt”.

2) Export the “Samples Table”. This isn’t actually necessary, but I like to use this since it has the “Sentrix Barcode” and “Sample Section” information. This also makes sure my sampleID is in the same order as my all.txt file. Let’s call this one “sample.txt”.

This is usually where I stop and I had the following error:
> samp <- read.table(“sample.txt”,sep=”\t”,na.strings=c(“”,” “,”NA”),,header=T,row.names=1)
> DATA <- methylumiR(“all(test=1000).txt”,sampleDescriptions=samp)
Error in if (!sampleIDcol) { : argument is of length zero

So the problem is that the “sample.txt” file needs to have a column with the name of “SampleID”.

After fixing this, the second most common problem I had is the following:
Error in if (labelCol) { : argument is of length zero

This problem is because there needs to be a “SampleLabel” column. By adding the two now I’m good. So here’s step 3 and 4 of using methylumiR.

3) Change the “Sample ID” column name into “SampleID”. Also duplicate the column and name it as “SampleLabel”. Otherwise, add a “SampleLabel” column and indicate the name you want to use as sample name in the methyLumiSet object.

3b) Include extra columns in the “sample.txt” file. Any extra columns will be deposited into the phenoData and can be accessed using pData(DATA).

4) Use the following script:
> samp <- read.table(“sample.txt”,sep=”\t”,na.strings=c(“”,” “,”NA”),,header=T,row.names=1)
> DATA <- methylumiR(“all(test=1000).txt”,sampleDescriptions=samp)

This should generate a methyLumiSet object where the average beta values can be accessed by using betas(DATA).

No more extreme long scripts that is replaceable by this awesome function!
If you have any tips to share or questions, please feel free to leave a comment!

Pillow’s happy hour

Note to self: never forget it is your responsibility to put that happy face on the little guy and all those around you. It doesn’t take much, but it counts a lot.