Reproducible reports & research with knitr in R Studio

June 18, 2012

(This article was first published on Pairach Piboonrungroj » R, and kindly contributed to R-bloggers)

Homepage of knitr package

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

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).

Screenshot of R Studio (Windows PC)

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:

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{mailto:[email protected][email protected]}
\vskip -0.5cm
%Add Breaking Line
1. Show only R source code
1 + 1

2. Show only output

3. Show both source code and output
1 + 1

4. Show source code in grey shade but the output
1 + 1

5. Now, testing a linear model
# 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
plot(x, y)

7. Let's build a linear model by regressing y on x
# 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
# Creating post-hoc plot for lm1

And this is the Output


Filed under: R

To leave a comment for the author, please follow the link and comment on their blog: Pairach Piboonrungroj » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.