Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

<<chunk1, echo=TRUE, results='hide'>>=
@


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}
\hypersetup{
urlcolor=blue
}
\setlength{\topmargin}{-15mm}
\setlength{\oddsidemargin}{-2mm}
\setlength{\textwidth}{165mm}
\setlength{\textheight}{250mm}

\let\section=\subsubsection
\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
\let\proglang=\textit
\let\code=\texttt
\renewcommand{\title}[1]{\begin{center}{\bf \LARGE #1}\end{center}}
\newcommand{\affiliations}{\footnotesize}
\newcommand{\keywords}{\paragraph{Keywords:}}

\begin{document}
\pagestyle{empty}

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

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

\begin{affiliations}
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]}
\end{affiliations}
\vskip -0.5cm
%%%%%%%%%%%%%%%%%%
\begin{center}
\linethickness{1mm}
\line(1,0){480}
\end{center}
%%%%%%%%%%%%%%%%%%
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()
<<custom-dev2>>=
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
summary(lm1)
@

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
par(mfrow=c(2,2))
plot(lm1)
@
\end{document}


## And this is the Output



View this document on Scribd

Filed under: R