%-------------------------------------------------------------------------------
%	Packages and other Document configurations
%-------------------------------------------------------------------------------
\documentclass[a4paper,11pt]{article}
% Package declaration
%-------------------------------------------------------------------------------
% Specify input encoding
\usepackage[utf8]{inputenc}
% For A4 paper set all margins to 3cm
\usepackage[paper=a4paper,left=1.5cm,top=2cm,right=1.5cm,bottom=2cm]{geometry}%
% Set linespace, usage \doublespacing \singlespacing \onehalfspacing
\usepackage{setspace}%
% Set palatino font with small caps as default
\usepackage[sc]{mathpazo}%
% Rotation tools, including rotated full-page floats.
\usepackage{rotating}%
% Create subfigures
\usepackage{subfigure}%
% Extensive support for hypertext in LaTeX
\usepackage{hyperref}%
% For adding bookmarks to the document
\usepackage{bookmark}%
% For adding time to the document
\usepackage{datetime}
% For alignment of captions
\usepackage{caption}
% For multiple columns.
\usepackage{multicol}

% Start Article header
%-------------------------------------------------------------------------------
% Title
\title{Finlay-Wilkinson report for \Sexpr{escapeLatex(x$trait)}}%
% Authors
\author{\vspace{-5ex}}
%-------------------------------------------------------------------------------
% Dates
\date{\vspace{-5ex}}
%-------------------------------------------------------------------------------
% End article header

% For left aligning captions
\captionsetup{justification=raggedright,singlelinecheck=false}

% Start Document
%-------------------------------------------------------------------------------
\begin{document}

% Article title and date.
\maketitle
% Start single line spacing
\singlespacing

%-------------------------------------------------------------------------------
\section{General information}
%-------------------------------------------------------------------------------
<<label=general, echo=FALSE, results="asis">>=
bgItems <- c(format(attr(x, "timestamp"), "%y-%m-%d %H:%M:%S"),
             as.character(packageVersion("statgenGxE")))
names(bgItems) <- c("Analysis done on",
                    "statgenGxE version")
print(xtable::xtable(x = data.frame(bgItems), label = "general",
                     align = c("l", "l")),
      latex.environments = "flushleft", hline.after = NULL,
      include.rownames = TRUE, include.colnames = FALSE)
@

%-------------------------------------------------------------------------------
\section{Anova table}
%-------------------------------------------------------------------------------

<<label=anova, echo=FALSE, results="asis">>=
statgenGxE:::printAnova(x$anova)
@

\clearpage
%-------------------------------------------------------------------------------
\section{Diagnostic plots}
%-------------------------------------------------------------------------------
<<label=linePlot, echo=FALSE, out.width="90%", fig.align="left">>=
plot(x, plotType = "line")
@
\clearpage
<<label=scatterPlot, echo=FALSE, out.width="90%">>=
plot(x, plotType = "scatter")
@
\clearpage

%-------------------------------------------------------------------------------
\section{Finlay-Wilkinson model parameter estimates}
%-------------------------------------------------------------------------------

<<label=estimates, echo=FALSE, results="asis">>=
sortBy <- list(...)$sortBy
sortNames <- c("sensitivity", "genotypic mean", "mse")
sortCols <- c("sens", "genMean", "mse")
sortName <- sortNames[which(sortCols == sortBy)]
estimates <- x$estimates
estimates <- estimates[order(estimates[[sortBy]], decreasing = TRUE), ]
print(xtable::xtable(x = estimates[1:10, ],
                     caption = paste("Top 10 genotypes by", sortName),
                     label = "topEstimates",
                     align = c("l", "l", "r", "r", "r", "r", "r", "r"),
                     digits = c(0, 0, 2, 2, 2, 2, 2, 2)),
      caption.placement = "top", latex.environments = "flushleft",
      include.rownames = FALSE, include.colnames = TRUE)
print(xtable::xtable(x = estimates[(nrow(estimates) - 10):nrow(estimates), ],
                     caption = paste("Bottom 10 genotypes by", sortName),
                     label = "bottomEstimates",
                     align = c("l", "l", "r", "r", "r", "r", "r", "r"),
                     digits = c(0, 0, 2, 2, 2, 2, 2, 2)),
      caption.placement = "top", latex.environments = "flushleft",
      include.rownames = FALSE, include.colnames = TRUE)
@
%-------------------------------------------------------------------------------
% End Document
\end{document}
