Produce Word documents, web pages, and pdfs in R quickly and easily

When I started using R in about 2009 reproducible research was a complete eye-opener for me. Statistical analyses should be reproducible both by the author of them and by the scientific community and by embedding the analysis and outputting code within a literate programming framework such as Sweave this was possible. Nearly everything I produce now, whether for regular reports on patient experience for the NHS Trust for which I work or for scientific papers is reproducible. These tools have saved me (and therefore my employer) hundreds of hours and they are all totally free.

Having said all that, there has recently been a sea-change in the R world as far as reproducible research goes. Although I could produce beautiful pdfs in LaTeX I often needed to write Word documents to share with colleagues. I would work in LaTeX/ Sweave through all the early stages and then use LaTeX2RTF to produce an RTF which I would then save as a .doc. It was okay, a bit fiddly, and the reproduction was not always perfect, but even then these free tools were saving me hundreds of hours.

The advent of knitr, RMarkdown, and RStudio has made the whole process embarassingly easy. Load RStudio, select File… New… RMarkdown, produce your report based on the template and the help files which are embedded right in RStudio for you to use, click “Knit to HTML” and you’re done. Want to share your results? Click “Publish” and put them straight onto the internet, send a link to your collaborators.

But it gets better. I’ve been hearing about pandoc for some time but have always been worried that it would prove complicated and wouldn’t immediately repay the investment. How wrong I was.

Pandoc turns just about any document into just about any other type of document, I haven’t even scratched the surface, but I’ve been using it to produce nicely formatted word documents. Install pandoc (sudo apt-get install pandoc on Ubuntu, GIYF for other OS’s), write an RMarkdown document just as you did before, and run the following:


knit("myDoc.Rmd", "")

system("pandoc -o myDoc.doc")

Boom. Done. I get a weird thing where the figures are referenced to file locations on my computer, so when I email it to people the figures disappear. I’m sure there’s a clever way to deal with this, I just open the document and “Save As” a Word document elsewhere and the figures save in the file in the new location.

I cannot over-emphasise how quick and easy this is. I would advise anybody reading this who has even the slightest interest in any of this to jump immediately on the RMarkdown train. You can even publish straight to WordPress.

Setting up a new Linux install, quickstart guide

This is another post which is designed both to help me as an aide-memoire and also to help anyone else out there who wants to set up a new Linux install quickly and easily.

One of the many, many things I love about Linux is the way that you can just tear down your OS any time you like and start using a different one. Having said that, I must admit to having had some long weekends and evenings when my installation has gone kaput fiddling around trying to get everything working. I have now streamlined the process and can set up a new Ubuntu-based distribution in probably no more than about 45 minutes. My Linux career has only been from Ubuntu to the Ubuntu-based Mint so far, I have tried to use Fedora but sadly my ridiculously powerful NVidia graphics card left over from my (whisper it) Windoze days does not appear to play nicely at all with Fedora. Going to have a go with Fedora 20 some time, you never know your luck.

So with no further ado here is my speedy install process that I run every time I change distros (Mint 15 to 16, or just if I break one, which I do, from time to time, fiddling). I’m no Linux expert, so there may be one or two howlers in here, but it works for me, anyway, so use at your own risk.

This is all from a Linux Mint 16 install, and should work fine for Ubuntu proper, and may well work for other Ubuntu based distributions, I’m afraid I don’t know for sure.

Once the CD is in you will be offered the chance to boot to a Live-CD version.

If, like me, you have a ridiculously powerful NVidia graphics card you will want to press “e” to edit the commands before launch and change the bit that says “Quiet splash” so that it says “nomodeset”. This will launch the OS in a low graphics version.

Next select the icon to Install and reboot.

If you’ve got loads of different Linux and Windows OS’s on your machine as I have you may find that the GRUB loses track of things a bit, in which case just launch the OS that does work and run sudo update-grub, which should put things right again.

Before you reboot into the new install (if you have a crazy graphics card) press “e” and once again change the bit that says “Quiet splash” so that it says “nomodeset”. Then (if you’re using NVidia) run:

sudo apt-get install nvidia-current

Reboot again without changing to nomodeset and you should find that your graphics works fine, if not I’m afraid it’s off to the forums with you.

Now to bring everything up to date:

sudo apt-get update
sudo apt-get upgrade

At this point we now have a fresh install, all up to date and playing nicely with our graphics card. The following list of software obviously reflects my own preferences, so make of it what you will.

Let’s install Google Chrome

This puts the key in:

wget -q -O - | sudo apt-key add - 

Repository setup:

sudo sh -c 'echo "deb stable main" >> /etc/apt/sources.list.d/google.list'

sudo apt-get update 
sudo apt-get install google-chrome-stable

Now let’s install The Mighty R

Run the following at the terminal:

gpg --keyserver --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -

Edit the sources list:

gksudo gedit /etc/apt/sources.list

And add the following (saucy is for Mint 16/ Ubuntu 13.10, you will want a different version if you are running an older distro (or a newer one, if you’re reading this in the future):

deb saucy/

Back to the terminal:

sudo apt-get update
sudo apt-get install r-base
sudo apt-get install r-base-dev

There are a few dependencies that I find are common in R packages (e.g. for RCurl, XML, and anything that uses Java), install them all like this:

sudo apt-get install openjdk-7-jdk libcurl4-gnutls-dev libxml2-dev

Install R Studio

Just directly install from here

Install Dropbox

Just directly install from here

I’ll be honest and say LaTeX/ Sweave is starting to take a back seat for me, reproducible programming wise, and being replaced with Markdown/ RMarkdown/ Knitr, but I have loads of old Sweave scripts and I haven’t abandoned it completely, so I still need a LaTeX install. One liner:

sudo apt-get install texlive-full

It’s a big install so go get yourself a cup of tea. The smaller one will not handle all the things that Sweave throws at it, in my experience, so you may as well go in large.

I’m an absolute sucker for programs that rotate your wallpaper (by which I mean, fetch you new ones, not turn it round, that would be annoying). Here’s a wonderful one:

Variety wallpaper changer

sudo add-apt-repository ppa:peterlevi/ppa
sudo apt-get update
sudo apt-get install variety

Once you’ve got it set up I recommend the following sources of wallpapers if you’re a massive geek like me: (games) (Marvel comics)

I edit my HTML/ Java/ other random things in the wonderful Geany IDE which supports compilation of code, preview of HTML and various other useful bits and pieces

sudo apt-get install geany

And, for those of us cursed with annoying Citrix email systems, Linux is all over that these days, it’s a bit fiddly but well worth it (in some ways the Linux client actually works better than it does on my Windoze 7 machine).

Not a lot of point my going through all the stuff that’s on this incredibly useful page.

UPDATE: I found recently that Firefox stopped playing nicely with Citrix, probably due to the big update Firefox has just had, but you can get it working on Chrome, too (which is my preferred browser anyway). Instructions are here.

The wonderful Conky which I confess I find rather difficult to use without the GUI manager that you can get for it.

sudo apt-get install conky
sudo apt-add-repository -y ppa:teejee2008/ppa
sudo apt-get update
sudo apt-get install conky-manager

And last but not least the mighty Git, which, wonderfully, once you’ve got it installed can then be used to fetch its own latest version. A bit of recursion there for all you recursion fans out there.

Another one liner:

sudo apt-get install git

Boom! You’ve got yourself a brand new Linux box, filled with top quality software, for free, in under 45 minutes.

My PhD thesis is online finally

To my great shame, I never published the findings from my PhD (Daily behavioural ratings: what can behavioural observation tell us about psychopathy, personality disorder and violence risk in secure psychiatric settings?) in any journals, by the time I’d finished I was so busy with my new job that I just never got round to it.

The least I can do is host the thesis on my server in case it proves of use to anyone, and I have added it to my website today, link to file here.