Write an R Journal article with knitr

Articles submitted to the R Journal must be in a specific format. This post explains how to easily output articles written in markup.

R Journal

The R Journal publishes peer-reviewed short to medium length articles covering topics that might be of interest to users or developers of R. It is a welcome platform to spread word of new packages.

As most academic journals, it has strict guidelines regarding the format of submitted articles. Traditionally, these have been provided with a LaTex style file and an article template.

knitr templates

However, knitr has made R Markdown popular. So, wouldn’t it be nice to just write an article in RStudio in Markdown, and then use knitr to compile the resulting pdf file?

This is possible, thanks to the rticles package. It adds not only a knitr template for the R Journal, but also for the Journal of Statistical Software, for useR conference abstracts, and for many other popular publications. Yihui Xie, the big man behind knitr, writes about all this in the RStudio blog. There is also an RStudio document on this topic.

However, if you are a newbie, there are quite a few pre-requisites until you can get going. And unfortunately it’s not always easy to find this information on the internet. So here’s a quick step by step guide.

Create a new AWS instance

This guide is based on ubuntu. So if you are a Windows user like me, the easiest way to get started is to boot up a pre-built box on Amazon AWS. If you’ve never done this, refer to this page. In summary:

  • to get started quickly, use the pre-built RStudio AMI. An AMI is an image, with the OS and some software already pre-installed
  • a t2.micro instance is all you need to follow this. But for a few lumpy dollars per month you can upgrade to a t2.small. In any case, you can change the instance type later.

Once your box is running, you can log into rstudio by connecting via your browser to the IP of your instance. Use the user rstudio and the password rstudio. If you are using your instance for anything else than playing around, consider changing the pw. As a windows user, I’m using putty for this. See below for details.

Connect to your AWS instance by terminal

You’ll find plenty of information on the internet on how to connect to an AWS instance. But the most basic steps are, from Windows using putty:

  1. Open putty
  2. Enter your instance’s IP address:
  3. Tell putty where your private key is (the one chosen or created during AWS instance creation). You will find the setting under Connection > SSH > Auth
  4. Accept the fingerprint
  5. Log on using the user ubuntu

Install R packages

Inside RStudio, you want to make sure that the R Journal rmarkdown templates are downloaded. These are available in the rticles package, which is available directly from github. To install the package, you need devtools in turn, which you can get from CRAN. So, in short:

  1. install devtools from CRAN
  2. in the RStudio R console, type

Create Project

Go to File > New Project > New Directory > Empty Project

Now, provide a name. Please avoid spaces in the directory name, as RStudio hates these. You might get strange errors later if you omit this advice. For example, if you see

then the most likely cause is that you have spaces or dots or similar in your path.

Create Markdown file from template

If everything is right, you should now see the following:

 

Click OK and you’re good to go. An Rmd stub file is created, ready for your editing.

Install texinfo

Chances are that, when you click on the knitr button, you receive errors similar to this:

If this is the case, then the most likely cause is that your missing texinfo on your system. To install it, you need to connect to your AWS instance via SSH / putty (see above). Once that’s done, type the following into your command line:

Compile your rmd file

Once this is done, you can run the article stub by clicking on the knitr button. You should now see something like this:

Leave a Reply