```
>=
figSweave <- function(){
par(bty = "n", mar = c(5, 4, 4, 1) + 0.1)
## WHATEVER OTHER R COMMANDS
}
options(SweaveHooks = list(fig = figSweave))
@
%%% ++++++++++++++++++++++++++++++++++++++++++++++++++
\textbullet\ Figure which is drawn, saved as \texttt{PDF} and automatically placed in a~text (see Figure~\ref{fig01}). Note that \textsf{pdf}{\LaTeX} must then be used to process the {\TeX} file.
\begin{figure}[h!]\centering
<
```>=
COL <- rainbow_hcl(2, start = 90)
plot(f2.sco.math ~ fam.comp, data = nelsNE2, col = COL,
xlab = "Family composition", ylab = "Score in mathematics")
@
\caption{Score in mathematics by family composition.}
\label{fig01}
\end{figure}
%%% ++++++++++++++++++++++++++++++++++++++++++++++++++
\textbullet\ Figure which was drawn, saved as \texttt{PDF} but it is nowhere placed automatically. Placing the figure into the document (see Figure~\ref{fig02}) is the author's responsibility. %'
%%% Figure which is drawn, saved automatically as PDF ('fig = TRUE'), the filename will be fig-boxplot2.pdf
%%% with specified height and width (in inches)
%%% but it is nowhere placed automatically ('include = FALSE').
%%% Placing the figure into the document (see below) is the author's responsibility.
<>=
COL2 <- terrain_hcl(2)
plot(f2.sco.math ~ fam.comp, data = nelsNE2, col = COL2,
xlab = "Family composition", ylab = "Score in mathematics")
@
\begin{figure}[h!]\centering
\includegraphics[width = 0.85\textwidth]{./Figures/fig-boxplot2}
\caption{Score in mathematics by family composition (again).}
\label{fig02}
\end{figure}
%%% ++++++++++++++++++++++++++++++++++++++++++++++++++
\clearpage
\textbullet\ It is also possible to use standard functions \texttt{pdf()}, \texttt{postscript()},
\texttt{png()} etc. to save a~plot in an arbitrary format on an arbitrary place with an arbitrary filename:
%%% Of course, we may also use a "standard" chunk (default 'fig = FALSE') and within the chunk
%%% create any graphical file using commands like pdf(), postscript(), png(), ...
<>=
postscript("./Figures/fig-boxplot1.eps", width = 6, height = 6,
horizontal = FALSE, paper = "special")
plot(f2.sco.math ~ fam.comp, data = nelsNE2, col = COL,
xlab = "Family composition", ylab = "Score in mathematics")
dev.off()
#
RES <- 500
png("./Figures/fig-boxplot1.png", width = 6*RES, height = 6*RES, res = RES)
plot(f2.sco.math ~ fam.comp, data = nelsNE2, col = COL,
xlab = "Family composition", ylab = "Score in mathematics")
dev.off()
@
%%% ------------------------------------------
\clearpage
\section{Results of a~more extensive analysis}
Results of analysis of dependence of score in mathematics on family composition is shown in Table~\ref{tab01again} and on Figure~\ref{fig01again}. All results are then in Tables~\ref{tab-sco_math-fam_comp}\,--\,\ref{tab-sco_read-f2_arrest} and on Figures~\ref{fig-sco_math-fam_comp}\,--\,\ref{fig-sco_read-f2_arrest}.
%%% In principle, the whole analyzis (its R code) can be included in the Sweave document.
%%% Nevertheless, if some parts of the analyzis are computationally time consuming
%%% (this is for sure the case of any computer simulation) we would run this time consuming
%%% code each time when the PDF is compiled from the Rnw file. It is then better to perform
%%% intensive calculations separately and store results in the .RData file. This file
%%% can now be loaded and the only thing done here, in the Sweave file, is formatting
%%% the output etc.
%%% ++++++++++++++++++++++++++++++++++++++++++++
%%% Load previously calculated results
<>=
(load(paste(ROOT, "/RResult/nmst440-tabsfigs.RData", sep = "")))
@
%%% ++++++++++++++++++++++++++++++++++++++++++++
%%% Manually include one table and one figure
\begin{table}[h!]\centering
\renewcommand{\arraystretch}{1.2}
\caption{Analysis of \textbf{score in mathematics} by \textbf{family composition}.}
\label{tab01again}
\vspace{1ex}
<>=
cat(tabs[["sco_math-fam_comp"]][["tex"]])
@
\end{table}
\begin{figure}[h!]\centering
\includegraphics[width = 0.8\textwidth]{./Figures/fig-sco_math-fam_comp}
\caption{Score in mathematics by family composition (once again).}
\label{fig01again}
\end{figure}
%%% ++++++++++++++++++++++++++++++++++++++++++++
%%%
%%% Main advantage of using Sweave to prepare report is
%%% that all R programming capabilities (e.g., the 'for' loop)
%%% can be used to place 10^whatever plots or figures in one document
%%% within just few minutes needed to write the R code.
%%% The R code below basically writes a sequence of LaTeX commands
%%% finally resulting in 16 pages in the report. On each page,
%%% one table and one plot are included.
%%% Include everything by an R script
<>=
for (yv in yVars){
for (xv in xVars){
tname <- paste(yv, xv, sep = "-")
tname <- gsub("\\.", "_", tname)
cat("\\clearpage\n",
"\\subsection{", yLabs[yv], " by ", xLabs[xv], "}\n", sep = "")
cat("\\begin{table}[h!]\\centering\n",
"\\renewcommand{\\arraystretch}{1.2}\n",
"\\caption{Analysis of \\textbf{", yLabs[yv], "} by \\textbf{", xLabs[xv], "}.}\n",
"\\label{tab-", tname, "}\n",
"\\vspace{1ex}\n", sep = "")
cat(tabs[[tname]][["tex"]])
cat("\\end{table}\n\n")
cat("\\begin{figure}[b!]\\centering\n",
"\\includegraphics[width = 0.8\\textwidth]{./Figures/fig-", tname, "}\n",
"\\caption{Boxplots of \\textbf{", yLabs[yv], "} by \\textbf{", xLabs[xv], "}.}\n",
"\\label{fig-", tname, "}\n",
"\\end{figure}\n", sep = "")
}
}
@
%%% ------------------------------------------
\clearpage
\bibliographystyle{akplainnat}
\bibliography{sampleReport}
\end{document}