### Response Variable: `r selectedResponse` 

```{r setup, include = FALSE, hide = TRUE, eval = TRUE}
explorDir <- paste0(outDir,"/","Integration/MixOmics/", selectedResponse, "/")
dir.create(path = explorDir)

knitr::opts_chunk$set(fig.path = explorDir)
```

```{r}
Data_res <- set_res <- NULL
Data_res <- getMixOmics(rflomics.MAE, response = selectedResponse)
set_res  <- getMixOmicsSettings(rflomics.MAE)
ncompDat <- min(Data_res$ncomp, 5)
fig_height <- NULL
fig_height <- ceiling(length(set_res$selectData)/2)*5
fig_width <- 15
nrow_plot <- ceiling(length(set_res$selectData)/2)
```

#### Overview

This overview present the number of observations and the number of features per dataset.
`r ifelse(set_res$sparsity, "As a sparse analysis was performed, you will find for each component and each table the best number of features associated.",  "")`

```{r mixOmics_overview}
if (is.list(Data_res$X)) {
    # block.plsda or block.splsda
    df <- t(vapply(Data_res$X, dim, c(1, 1)))
    colnames(df) <- c("Ind", "Features")
    
    if (set_res$sparsity) {
        df <- cbind(df, do.call("rbind", Data_res$keepX))
        colnames(df)[!colnames(df) %in% c("Ind", "Features")] <-
            paste("Comp", seq_len(length(Data_res$keepX[[1]])))
    }
    
} else {
    # plsda or splsda
    df <-  data.frame("Ind" = nrow(Data_res$X),
                      "Features" = ncol(Data_res$X))
    if (set_res$sparsity) {
        df <- cbind(df, do.call("cbind", as.list(Data_res$keepX)))
        colnames(df)[!colnames(df) %in% c("Ind", "Features")] <-
            paste("Comp", seq_len(length(Data_res$keepX)))
    }
}

datatable(t(df), options = list(dom = 't'))
```

#### Explained variance

These graphs represent the explained variance for each datatable. 
The graph on the left is the total of all component explained variance,
the graph on the right is the decomposition and is read as: component x explains y% of data D and z% of dataset M.
Keep in mind the purpose of (s)plsda is not the same as pca, it does not aim at capturing the most variance.

```{r mixOmics_varExp, fig.width = 12, fig.height = 5}
RFLOMICS:::plotMOVarExp(rflomics.MAE, selectedResponse = selectedResponse)
```

#### Idividuals plots {.tabset}

These graphs represent the samples coordinates projected onto the first 
<span style=color:blue>`r ncompDat`</span> components found by mixOmics.
Samples are colored according to 
<span style=color:blue>`r selectedResponse`</span>. 
It is best when the samples are grouping into response modalities.
If it is not the case, the analysis was maybe not successful.

```{r, results="asis", fig.height = fig_height, fig.width=fig_width, fig.align = "center"}
childTest <- NULL
outMixOmics <- NULL

for (i in seq_len(ncompDat - 1)) {
    outMixOmics <- c(outMixOmics,
                 paste0("\n##### Comp ", i, " - ", i + 1, "\n"))
    
    childTest <-  c(
        "```{r, fig.width = fig_width, fig.height = fig_height}\n",
        "mixOmics::plotIndiv(Data_res, comp = c(i, i + 1), 
                             ellipse = FALSE, legend = TRUE,
                             cex = 5,
                             size.xlabel = ggplot2::rel(2),
                             size.ylabel = ggplot2::rel(2),
                             size.axis = ggplot2::rel(1.5))",
        "```\n")
    outMixOmics <- c(outMixOmics, knitr::knit_child(text = childTest,
                                            envir = environment(),
                                            quiet = TRUE))
}
knitr::asis_output(outMixOmics)

```

#### Loadings {.tabset}

For each block (datatable), these graphs represent the 15 first features in 
terms of loadings score (coefficients) for the first 
<span style=color:blue>`r ncompDat`</span> components, by ascending order. 
The higher (absolute) the score, the more important the variable is in the 
component computation. 
This means the highest-scores features also help the most to discriminate 
between categories of the response variable.

```{r, results="asis", fig.height = fig_height, fig.width=fig_width, fig.align = "center"}
childTest <- NULL
outMixOmics <- NULL

for (i in seq_len(ncompDat)) {
    outMixOmics <- c(outMixOmics,
                 paste0("\n##### Comp ", i, "\n"))
    
    childTest <-  c(
        "```{r, fig.width = fig_width, fig.height = fig_height}\n",
        "mixOmics::plotLoadings(Data_res, 
                       comp = i,
                       ndisplay = 15, layout = c(nrow_plot, 2),
                       size.name = 1.2, size.legend = 1.2) ",
        "```\n")
    outMixOmics <- c(outMixOmics, knitr::knit_child(text = childTest,
                                            envir = environment(),
                                            quiet = TRUE))
}
knitr::asis_output(outMixOmics)

```

#### {.unlisted .unnumbered}

---

<!-- Exports -->

```{r mixOmics_export}
loads <- do.call("rbind", lapply(names(Data_res$loadings), FUN = function(nam){
    df <- data.frame(Data_res$loadings[[nam]])
    df$Table <- nam
    relocate(df, Table)
}
))

write.table(loads, 
            file = paste0(explorDir, selectedResponse, "_Loading_values.txt"),
            sep = "\t", quote = FALSE, col.names = TRUE, row.names = TRUE)

inds <- do.call("rbind", lapply(names(Data_res$variates), FUN = function(nam){
    df <- data.frame(Data_res$variates[[nam]])
    df$Table <- nam
    relocate(df, Table)
}
))

write.table(inds, file = paste0(explorDir, selectedResponse, "_Ind_coord.txt"),
            sep = "\t", quote = FALSE, col.names = TRUE, row.names = TRUE)
```


