\subsubsection*{\em Section 1 -- Setup}

Setup code can conveniently be placed near the beginning of the main
file (after \texttt{\\begin\{document\}}.  It can also be placed at
the beginning of any {\bf .Rnw} files that are included .  The chunk
options can all be changed in the calls to individual code chunks.
Here, the setup code was:
\begin{SaveVerbatim}{sec1-1}
<<setup, echo=FALSE, results="hide", message=FALSE>>=
options(replace.assign=TRUE,width=90)
opts_chunk$set(fig.path='fig/demo-', cache.path='cache/manual-',
               fig.align='center', dev='pdf', tidy=FALSE,
               fig.width=2.75, fig.height=2.95, 
               out.width="0.47\\texwidth", fig.show='hold',
               cache=TRUE, par=TRUE)
knit_hooks$set(par=function(before, options, envir){
if (before && options$fig.show!='none') par(mar=c(4,4,1.6,.2),
              cex=1, cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),
              tcl=-.3)
}, crop=hook_pdfcrop)
@
\end{SaveVerbatim}

\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec1-1}
\vspace*{-8pt}

}}
\vspace*{8pt}

The chunk options apply to all later code chunks where there is
no explicit setting for that option.

Code with markup used to load {em knitr} and create the LaTeX file that
was then used to create this file was:
\begin{SaveVerbatim}{sec1-2}
<<knitr-run, eval=FALSE, echo=TRUE>>=
library(knitr)
knit('knitDemo.Rnw')
@
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec1-2}
\vspace*{-8pt}

}}
\vspace*{8pt}

\subsubsection*{\em Section 2 -- Printing and Executing Code}

The code that appears in the file, with markup added is:
\begin{SaveVerbatim}{sec2-1}
<<cmdline, echo=TRUE, eval=TRUE, tidy=FALSE>>=
2*3*4*5          # * denotes 'multiply'
sqrt(10)         # the square root of 10
pi               # R knows about pi
2*pi*6378        # Circumference of earth at equator (km)
                 # (radius at equator is 6378 km)
@
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec2-1}
\vspace*{-8pt}

}}
\vspace*{8pt}

The markup lines that precede and end the code have the form:
\begin{SaveVerbatim}{sec2-2}
<<cmdline, ...>>=
@
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
{\large
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec2-2}
\vspace*{-8pt}

}}
\vspace*{8pt}
}

Here \texttt{cmdline} is the name for this code chunk, amd
\texttt{...} is replaced by chunk options.
The code chunk ends with an \texttt{@} symbol.

\subsubsection*{\em Section 3 -- Plotting a Graph}

Code, with enclosing markup, is:
\begin{SaveVerbatim}{sec3-1}
\begin{figure}
<<graph01, eval=TRUE, echo=FALSE, out.width="0.47\\textwidth">>=
Year <- c(1800, 1850, 1900, 1950, 2000)
Carbon <- c(8, 54, 534, 1630, 6611)
## Now plot Carbon as a function of Year
plot(Carbon ~ Year, pch=16)
@
\caption{Annual totals of carbon emissions (millions of tonnes),
against year}
\end{figure}
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec3-1}
\vspace*{-8pt}

}}
\vspace*{8pt}

The option \texttt{out.width} specifies the width that the graph
should occupy in the final document.  Notice that it is specified
as a text string. This is done so that it can be inserted as text
into the LaTeX {\bf .tex} document that is generated.

The markup code that caused the printing of the code
for the graph was:
\begin{SaveVerbatim}{sec3-2}
<<graph1-code, ref.label="graph1", eval=FALSE, echo=TRUE>>=
@
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec3-2}
\vspace*{-8pt}

}}
\vspace*{8pt}

Notice the use of \texttt{ref.label="graph1"} to identify the
code chunk that is printed.

\subsubsection*{\em Section 4 -- Graphs Shown Side by Side}

Here, the code chunk used is printed and stored under the name
\texttt{mammals}, but not evaluated at this point.

\begin{SaveVerbatim}{sec4-1}
<<mammals, eval=TRUE, echo=TRUE>>=
## Panel A
plot(brain ~ body, data=mammals, pty="s")
mtext(side=3, line=0.75, "A: Unlogged Data", adj=0)
## Panel B
plot(brain ~ body, data=mammals, log="xy", pty="s")
mtext(side=3, line=0.75, "B: Logaruthmic Scales", adj=0)
@ %
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec4-1}
\vspace*{-8pt}

}}
\vspace*{8pt}

The code used to create the graph is then:
\begin{SaveVerbatim}{sec4-2}
\begin{figure}
<<mammals-gph, ref.label="mammals", echo=FALSE>>=
@ %

\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}
\end{SaveVerbatim}

\vspace*{8pt}
\noindent
\fbox{
\parbox{\textwidth}{
\vspace*{-8pt}

\UseVerbatim{sec4-2}
\vspace*{-8pt}

}}
\vspace*{8pt}

Use of \texttt{ref.label="mammals"}, in the code chunk
\texttt{mammals-gph}, has the effect of inserting the code at
that point.  As the default is \texttt{eval=TRUE}, it is evaluated
and the graphs are printed.

The setup code \verb!out.width="0.47\\texwidth"! in Section 1
allowed room for two plots side by side.  As the code chunk that
created Figure \ref{fig:Animal} made no change from this initial
setup, the LaTeX file that is created accordingly places the plots
side by side.  Specify, e.g., \verb!out.width="0.32\\texwidth"!, to
allow three graphs side by side across the page.

