Skip to content

Reproducible reports & research with knitr in R Studio

Screen Shot 2012-06-18 at 16.20.17

Arguably, knitr (CRAN link) is the most outstanding R package of this year and its creator, Yihui Xie is the star of the useR! conference 2012. This is because the ease of use comparing to Sweave for making reproducible report. Integration of knitR and R Studio has made reproducible research much more convenience, intuitive and easier to use.

R Studio: A user friendly and cross platform IDE for R

Screenshot of R Studio (Windows PC)

This post is an example, based on the demo by Yihui Xie himself, I will show how to create a reproducible report consisting the R code in a LaTeX style in the Cardiff R User Group session at the Cardiff Business School (CARBS) Research Fair tomorrow (19 June 2012).

knitr option for sweave document in R Studio

In the code

  • Lines 001-043 are just normal preamble syntax of the LaTeX code I took from the Template of useR! conference abstract.
  • Lines 044-96 are the R codes and descriptions.

A chuck of R code is wrapped in the following code:

<<chunk1, echo=TRUE, results='hide'>>=
Put your R code here

chunk1 is the name of the chuck

echo = TRUE to show your R code in the chuck, = FALSE if you do not want to show the code.

result = ‘markup’ to show the result unless = ‘hide’

The code of the whole document is as follow:

\documentclass[11pt, a4paper]{article}
\usepackage{amsfonts, amsmath, hanging, hyperref, natbib, parskip, times}

\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
\renewcommand{\title}[1]{\begin{center}{\bf \LARGE #1}\end{center}}


\title{Using knitR (R + \LaTeX) in R Studio: A Demo}

 {\bf Pairach Piboonrungroj$^{1,2,^\star}$}

1. Logistics Systems Dynamics Group, Cardiff Business School, Cardiff University, United Kingdom \\[-2pt]
2. Chiang Mai School of Economics, Chiang Mai University, Thailand \\[-2pt]
%3. Second affiliation of author B \\[-2pt]
$^\star$Email: \href{}{}
\vskip -0.5cm
%Add Breaking Line
1. Show only R source code
<<chunk1, echo=TRUE, results='hide'>>=
1 + 1

2. Show only output
<<chunk2, ref.label='chunk1', echo=FALSE, results='markup'>>=

3. Show both source code and output
<<chunk3, echo=TRUE, results='markup'>>=
1 + 1

4. Show source code in grey shade but the output
<<chunk4, echo=TRUE, results='asis'>>=
1 + 1

5. Now, testing a linear model
<<chunk5, echo=TRUE, results='markup'>>=
# generating value for x variable from 1 to 100
x <- c(1:100)
# creat error term
e <- rnorm(100, mean = 5, sd = 10000)
# computing y equal to 3 plus five times x plus random number
y = 10 + 100*x + e

Set the format of all object called pdf()
my_pdf = function(file, width, height) {pdf(file, width = 5, height = 5, pointsize = 10)}

6. See the scatter plot
<<chunk6, echo=TRUE, results='markup', dev='my_pdf', fig.ext='pdf'>>=
plot(x, y)

7. Let's build a linear model by regressing y on x
<<chunk7, echo=TRUE, results='markup'>>=
# creating a linear model by regressing y on x as 'lm1' object
lm1 <- lm(y ~ x)
# calling a summary of linear model result

8. Now we can create a post-hoc plots to check assumptions of regression
<<chunk8, echo=TRUE, results='markup', dev='my_pdf', fig.ext='pdf'>>=
# Creating post-hoc plot for lm1

And this is the Output


Related posts

5 Comments Post a comment
  1. Since v0.6, you can actually use dev=’pdf’, dev.args=list(pointsize=10) instead of defining a new device. BTW, thanks for the kind words :)

    June 18, 2012
    • Hi Yihui,

      Thanks a lot for such a prompt comment and of course making this package.

      I made this document before the v.06 but just post it today, that’s why.
      It’s great to be able to specify separately in the chuck!

      Great works!

      June 18, 2012
  2. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old
    daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed.

    There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is entirely off
    topic but I had to tell someone!

    July 6, 2013
  3. Attention on their staff and ambientBy Transfer company pay their special attention on training of their drivers, safety, reliability orlando taxi service and cost-friendly services.

    This can actually lead to a reduction in the amount you had
    to pay for the car in the first place. Oh, well,
    I can’t give up the kids, right, chairman of the city’s administrative law division, in an apparent critique of my
    choice of music. While you are in search of something extravagant, you can pay big bills like rent, utilities, etc.

    May 28, 2014

Trackbacks & Pingbacks

  1. Highlights of the useR! 2012 – Review of the reviews | Pairach Piboonrungroj

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


Get every new post delivered to your Inbox.

Join 2,778 other followers

%d bloggers like this: