%-------------------------------------------------------------------------------
%	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{\Sexpr{if (x$GGE) "GGE" else "AMMI"} 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=setup, echo=FALSE>>=
hook_plot <- knitr::knit_hooks$get("plot")
knitr::knit_hooks$set(plot = function(x, options) {
  paste0("\\begin{kframe}\\end{kframe}", hook_plot(x, options))})
@
<<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 tables}
%-------------------------------------------------------------------------------

<<label=anova, echo=FALSE, results="asis">>=
if (x$byYear) {
  years <- names(x$anova)
  for (year in years) {
    statgenGxE:::printAnova(aovTab = x$anova[[year]], title = year)
  }
} else {
  statgenGxE:::printAnova(aovTab = x$anova)
}
@
\clearpage
%-------------------------------------------------------------------------------
\section{PCA Summaries}
%-------------------------------------------------------------------------------

<<label=importance, echo=FALSE, results="asis">>=
if (x$byYear) {
  for (year in years) {
    nPC <- ncol(x$envScores[[year]])
    importance <- x$importance[[year]][, 1:nPC]
    print(xtable::xtable(x = importance, caption = year,
                         label = paste0("importance", year),
                         align = c("l", rep("c", ncol(importance)))),
          caption.placement = "top",
          latex.environments = "flushleft",
          include.rownames = TRUE, include.colnames = TRUE,
          scalebox = ifelse(nPC > 10, 0.8 - 0.05 * (nPC - 12), 1))
  }
  cat("\\clearpage")
} else {
  nPC <- ncol(x$envScores)
  importance <- x$importance[, 1:nPC]
  print(xtable::xtable(x = importance, label = "importance",
                       align = c("l", rep("c", ncol(importance)))),
        latex.environments = "flushleft",
        include.rownames = TRUE, include.colnames = TRUE,
        scalebox = ifelse(nPC > 10, 0.8 - 0.05 * (nPC - 12), 1))
}
@

<<label=impPlot, echo=FALSE, fig.height=4, fig.width=5, fig.align="left", background="white">>=
if (x$byYear) {
  for (year in years) {
    plot(x = 1:ncol(x$importance[[year]]), y = x$importance[[year]][2, ],
         type = "b", xaxt = "n", xlab = "", ylab = "", main = year)
    axis(side = 1, at = 1:ncol(x$importance[[year]]),
         labels = colnames(x$importance)[[year]])
  }
} else {
  plot(x = 1:ncol(x$importance), y = x$importance[2, ], type = "b",
       xaxt = "n", xlab = "", ylab = "")
  axis(side = 1, at = 1:ncol(x$importance), labels = colnames(x$importance))
}
@
\newpage

%-------------------------------------------------------------------------------
\section{Biplots}
%-------------------------------------------------------------------------------
<<label=biplot, echo=FALSE, fig.width=9, fig.height=6.3, out.width="0.9\\maxwidth", background="white">>=
if (x$byYear) {
  nPCs <- max(sapply(X = x$envScores, FUN = ncol))
  PCs <- paste0("PC", 2:nPCs)
} else {
  PCs <- paste0("PC", 2:ncol(x$envScores))
}
for (PC in PCs) {
  plot(x, plotType = "AMMI2", secAxis = PC, scale = 0.5)
}
@

%-------------------------------------------------------------------------------
% End Document
\end{document}
