KnitR and spin in R

In my undergrad years I always wondered about unique style of presentation slides in some of my classes. I knew they are certainly not made of powerpoint or keynote. I also wondered about whether you can edit pdf files directly because I always used to type it in a word document and convert it into a pdf document. During one of my summer research experience at Ohio State University, I learned about LaTex and R (as a matter of fact that summer experience changed the course of my academic career). After I started graduate school, I have extensively used R and LaTex in addition to other softwares. A week or two after the beginning of graduate school, I heard about this R package called knitr from one of my friends. Knitr reduced my workload by half, because I was able to conduct statistical analysis with R and then use knitr to create a great document with the analysis instead of copying and pasting everything into latex. Very recently I got to know about spin, this function in knitr makes your life even easier. In this post I will show how you can use knitr and spin, and some useful resources and reference to knitr and spin. After opening RStudio go to the file tab, then new file and choose R Markdown… as shown in the picture below. RMarkdownOpen So instead of regular R script this document will be saved as .Rmd. After choosing the R Markdown option you can create any of the following file type(HTML / PDF / Word Doc) and any mode such as document / presentation/ Shiny or Unique template option. Header of the file looks like the picture below. It also gives some guidance about how to begin if you are a newbie to knitr.


You can display R code in the file by this syntax

Your R code here

You can load R packages, create plots and do all sorts of things that you can do in R studio console. Sometimes when you load an R package there will be warning messages and other info that might take up a lot of space in your document or if you don’t want to see the r code or if you want to set the figure width and height of your plot you can do it with specific handles. In case you don’t want to see the R code or any warning message in your final documentation you can do something like this

```{r, echo = FALSE, warning = FALSE}
Your R code here

echo is the command to printout the R code so we suppress that and warning by this command. If you are doing memory intense calculations, then whenever you try to knit the file it will do the computation all over again so it might take a lot of time. In order to avoid that you can use the handle cache = TRUE. This will enable R to do the calculation once and store it in the local memory instead of retrieving everything whenever you knit a file. There are a few more tips and tricks to control the output in Yihui’s page. Although most of the syntax in knitr is very similar to LaTex, there are some major differences, for bullet points and numbering you don’t have to use itemize or enumerate you can just type numbers and stars as if it is a word document. You can add mathematical symbols and Greek letters in the same way as in a LaTex document. If you need to add some specific package you have to include that in your header. For example, if I want to include pictures in my file I have to load the graphicx package, the picture below shows how you can include the package in the header. header_includes Then inside the script you can add the includegraphics function to include the picture. You can find more information in the knitr website. Although Rmarkdown along with knitr was a great way to present your results, it has a few drawbacks. One is that I have to create an R file, write all the code, make sure it works and only after that I can transfer the code to .Rmd file. Essentially I have to create two files and you cannot run the R code in the code chunk of an .Rmd file independently, you have to compile the whole file. I was wondering about these drawbacks for a while and then one day my summer project mentor mentioned about spin which seemed to solve both of the previous problems. spin is not a separate package it is a function in the knitr package. In order to use spin you create a new R script file that has an extension .R. You type R code as if it were a regular R file. In order to add text you add #’ in the beginning of the line. By adding extra signs you can increase the size of the text, If you add ###’ Header, it will look like a title. You can also control output in spin, just use #+ then use the handle echo / warning or whatever output you want to control. I am still trying to figure out how to include pictures etc. But so far spin has been very helpful because I can run the R code independently, i.e. I don’t have to compile the file in order to debug or see the output of the file. When you want to compile the file you have to press the page with a binder button. Rightmost icon in the picture below.


Below is an example code I wanted to spin.

###' write normal text like this and chunk options like below
#+ test, echo=TRUE
#' My equation $epsilon = mu hat{psi^2}$

y<-c(23,45,576,456,54) # This is it

The output file looks like this.


I hope you will find this interesting and useful too. If you know any more cool tips and tricks about knitr or spin let me know.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a website or blog at

Up ↑

%d bloggers like this: