**Pairach Piboonrungroj » R**, and kindly contributed to R-bloggers)

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.

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

**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} \hypersetup{ colorlinks, linkcolor=blue, 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 %%%%%%%%%%%%%%%%%% %Add Breaking Line \begin{center} \linethickness{1mm} \line(1,0){480} \end{center} %%%%%%%%%%%%%%%%%% 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 summary(lm1) @ 8. Now we can create a post-hoc plots to check assumptions of regression < >= # Creating post-hoc plot for lm1 par(mfrow=c(2,2)) plot(lm1) @ \end{document}

## And this is the Output

Filed under: R

**leave a comment**for the author, please follow the link and comment on their blog:

**Pairach Piboonrungroj » R**.

R-bloggers.com 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...