\documentclass{article}\usepackage{graphicx, color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.2, 0.2, 0.2}
\newcommand{\hlnumber}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlfunctioncall}[1]{\textcolor[rgb]{0.501960784313725,0,0.329411764705882}{\textbf{#1}}}%
\newcommand{\hlstring}[1]{\textcolor[rgb]{0.6,0.6,1}{#1}}%
\newcommand{\hlkeyword}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlargument}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%
\newcommand{\hlcomment}[1]{\textcolor[rgb]{0.180392156862745,0.6,0.341176470588235}{#1}}%
\newcommand{\hlroxygencomment}[1]{\textcolor[rgb]{0.43921568627451,0.47843137254902,0.701960784313725}{#1}}%
\newcommand{\hlformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%
\newcommand{\hleqformalargs}[1]{\textcolor[rgb]{0.690196078431373,0.250980392156863,0.0196078431372549}{#1}}%
\newcommand{\hlassignement}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlpackage}[1]{\textcolor[rgb]{0.588235294117647,0.709803921568627,0.145098039215686}{#1}}%
\newcommand{\hlslot}[1]{\textit{#1}}%
\newcommand{\hlsymbol}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlprompt}[1]{\textcolor[rgb]{0.2,0.2,0.2}{#1}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\usepackage[hyphens]{url}
\usepackage{float}
\usepackage{fancyvrb}
\pagestyle{headings}
\title{Use of {\em knitr} to Generate Reproducible Reports}
\author{John Maindonald}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\maketitle




\section{Setup}

This document was created from the {\bf .Rnw} file {\bf knitDemo.Rnw}.
To create the LaTeX ({\bf .tex}) file, first ensure that {\em knitr}
and dependencies are installed.  Then place the file {\bf
  knitDemo.Rnw} in your working directory, and type from the R command
line:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlfunctioncall{library}(knitr)
\hlfunctioncall{knit}(\hlstring{'knitDemo.Rnw'})
\end{alltt}
\end{kframe}
\end{knitrout}


Options can be set near the start of the document that will apply to
the document as a whole. If there are input files, such setup code may
be required for each separate input file.  See Section \ref{sec:setup}
below for the setup code used here.

\begin{itemize}
\item[]
Paul Krugman on "The Excel Depression":  \quad
\url{http://www.nytimes.com/2013/04/19/opinion/krugman-the-excel-depression.html?_r=0}
\item[] Is The Reinhart-Rogoff Result Based on a Simple Spreadsheet Error? (Matthew Yglesias): \quad
\url{http://www.slate.com/blogs/moneybox/2013/04/16/reinhart_rogoff_coding_error_austerity_policies_founded_on_bad_coding.html}
\item[] The benefits of reproducible research: a public health example (Tim Churches):\quad
\url{https://github.com/timchurches/meta-analyses/blob/master/benefits-of-reproducible-research/benefits-of-reproducible-research.md}
\end{itemize}


\section{Printing and Executing Code}

Here are command line calculations:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
2*3*4*5          \hlcomment{# * denotes \hlstring{'multiply'}}
\end{alltt}
\begin{verbatim}
## [1] 120
\end{verbatim}
\begin{alltt}
\hlfunctioncall{sqrt}(10)         \hlcomment{# the square root of 10}
\end{alltt}
\begin{verbatim}
## [1] 3.162
\end{verbatim}
\begin{alltt}
pi               \hlcomment{# R knows about pi}
\end{alltt}
\begin{verbatim}
## [1] 3.142
\end{verbatim}
\begin{alltt}
2*pi*6378        \hlcomment{# Circumference of earth at \hlfunctioncall{equator} (km)}
\end{alltt}
\begin{verbatim}
## [1] 40074
\end{verbatim}
\begin{alltt}
\hlcomment{                 # (radius at equator is 6378 km)}
\end{alltt}
\end{kframe}
\end{knitrout}


\section{Plotting a Graph}

\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=0.47\textwidth]{fig/demo-graph1} 

}



\end{knitrout}

\caption{Plot of annual totals of carbon emissions (millions of tonnes),
against year\label{fig:carbon}}
\end{figure}
Code for Figure \ref{fig:carbon} is:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
Year <- \hlfunctioncall{c}(1800, 1850, 1900, 1950, 2000)
Carbon <- \hlfunctioncall{c}(8, 54, 534, 1630, 6611)
\hlcomment{## Now plot Carbon as a function of Year}
\hlfunctioncall{plot}(Carbon ~ Year, pch=16)
\end{alltt}
\end{kframe}
\end{knitrout}


\section{Graphs Shown Side by Side}
The dataset \texttt{mammals} {\em MASS} is an extreme example
of data that cries out for transformation.  Figure \ref{fig:Animal}A
shows the scatterplot for the raw data, while Figure \ref{fig:Animal}B
shows the scatterplot for the logged data.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlfunctioncall{library}(MASS)
\hlcomment{## Panel A}
\hlfunctioncall{plot}(brain ~ body, data=mammals, pty=\hlstring{"s"})
\hlfunctioncall{mtext}(side=3, line=0.75, \hlstring{"A: Unlogged Data"}, adj=0)
\hlcomment{## Panel B}
\hlfunctioncall{plot}(brain ~ body, data=mammals, log=\hlstring{"xy"}, pty=\hlstring{"s"})
\hlfunctioncall{mtext}(side=3, line=0.75, \hlstring{"B: Logaruthmic Scales"}, adj=0)
\end{alltt}
\end{kframe}
\end{knitrout}



\begin{figure}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=0.47\textwidth]{fig/demo-mammals-gph1} 
\includegraphics[width=0.47\textwidth]{fig/demo-mammals-gph2} 

}



\end{knitrout}


\caption{Brain weight (g) versus Body weight (kg), for 62 species of mammal.
  Panel A shows the unlogged data, while  Panel B uses log scales, for both
  axes. Notice that the scales are labeled in the original (unlogged)
  units.\label{fig:Animal}}
\end{figure}

\section{Code Chunks, with Markup}\label{sec:setup}

\input{demo-code}

\section{Links (\texttt{knitr}, more generally)}
\begin{itemize}
\item[] Elegant, flexible and fast dynamic report generation with R (Yihui Xie): \ \url{http://yihui.name/knitr/}
\item[] Slides obtained with knitr (Yihui Xie): \ 
\url{https://github.com/yihui/knitr-examples/blob/master/009-slides.md}
\item[] Getting started with R Markdown, knitr, and Rstudio 0.96 (Jeromy Anglim): \
\url{https://jeromyanglim.blogspot.com.au/2012/05/getting-started-with-r-markdown-knitr.html}
\item[] Ipython replication of Reinhart-Rogoff (Vincent Arel-Bundock): \
\url{http://nbviewer.ipython.org/5409848}
\end{itemize}

\end{document}
