
<div style="display:block;clear:both;" class="rosetta-data-reduction-start"></div>
<div class="rosetta-data-reduction-container">

```{r, eval=TRUE, echo=FALSE}

oldKableViewOption <- getOption("kableExtra_view_html", NULL);
options(kableExtra_view_html = FALSE);

if (!exists('headingLevel') || !is.numeric(headingLevel) || (length(headingLevel) != 1)) {
  headingLevel <- 0;
}

```

```{r, results='asis', eval=TRUE}
ufs::heading(x$output$analysisName,
             " {#rosetta",
             x$input$FactorName,
             "}",
             headingLevel=headingLevel);
```

```{r, echo=echoPartial, results='asis', eval=TRUE}

knitTable(
  data.frame(c("Extraction method:", "Rotation:", "Sample size:"),
             c(x$output$extractionMethod,
               x$output$rotation,
               x$output$n),
             stringsAsFactors=FALSE),
  caption = "Input parameters",
  col.names = NULL,
  align = c("r", "l"),
  row.names = FALSE
);

```


```{r, echo=echoPartial, results='asis', eval=(x$input$loadings) }

# ufs::heading(x$input$FactorName, " loadings",
#              headingLevel = headingLevel+1);

if (x$input$colorLoadings) {
  
  if (x$input$colorLoadings) {

    x$output$loadings <-
      dataReduction_loadingColoring(
        x$output$loadings,
        digits = x$input$digits
      );

  }
  
  # x$output$loadings[, 1:(ncol(x$output$loadings) - 1)] <-
  #   data.frame(
  #     lapply(
  #     x$output$loadings[, 1:(ncol(x$output$loadings) - 1),
  #                       drop=FALSE],
  #     function(column) {
  #       kableExtra::cell_spec(
  #         round(column, x$input$digits),
  #         bold = T, 
  #         color = kableExtra::spec_color(
  #           abs(column),
  #           end = 0.9,
  #           direction = -1,
  #           scale_from = c(0, 1)
  #         ),
  #         font_size = kableExtra::spec_font_size(
  #           abs(column),
  #           scale_from = c(-1, 1)
  #         )
  #       )
  #     }
  #   )
  # );

}

ufs::kblXtra(
  x$output$loadings,
  caption = paste0(x$input$FactorName, " loadings"),
  digits = digits,
  viewer = FALSE
);

# kableExtra::kbl(
#   x$output$loadings,
#   caption = paste0(x$input$FactorName, " loadings"),
#   digits = digits
# );

```


```{r, echo=echoPartial, results='asis', eval=(x$input$summary) }

# ufs::heading(x$input$FactorName, " summary",
#              headingLevel = headingLevel+1);

roundedSummary <-
  round(x$output$summary, digits=x$input$digits);
if (ncol(roundedSummary) > 2) {
  roundedSummary[, 2:3] <- 100*roundedSummary[, 2:3];
} else {
  roundedSummary[, 2] <- 100*roundedSummary[, 2];
}

knitTable(
  roundedSummary,
  caption = paste0(x$input$FactorName, " summary"),
  digits = digits
);

```


```{r, echo=echoPartial, results='asis', eval=(x$input$correlations) }

# ufs::heading(x$input$FactorName, " correlations",
#              headingLevel = headingLevel+1);

knitTable(
  x$output$correlations,
  caption = paste0(x$input$FactorName, " correlations"),
  digits = digits
);

```

```{r, echo=echoPartial, results='asis', eval=(x$input$modelFit) }

# ufs::heading("Model fit",
#              headingLevel = headingLevel+1);

modelFit <-
  x$output$modelFit;
modelFit[, 1:4] <-
  round(modelFit[, 1:4], digits+2);
modelFit[, 5:6] <-
  round(modelFit[, 5:6], max(digits-2, 0));
modelFit[, 7] <-
  round(modelFit[, 7]);
modelFit[, 8] <-
  ufs::formatPvalue(modelFit[, 8], includeP = FALSE, spaces=FALSE);

knitTable(
  modelFit,
  caption = "Model fit",
  escape=TRUE
);

```

```{r, echo=echoPartial, results='asis', eval=(x$input$modelFit) }

# ufs::heading("Initial eigen values",
#              headingLevel = headingLevel+1);

knitTable(
  x$output$eigenValues,
  caption="Initial eigen values",
  digits = digits + 2
);

```

```{r, echo=echoPartial, results='asis', eval=(x$input$screePlot), fig.width=5, fig.height=5, fig.cap="Scree plot of the eigen values" }

# ufs::heading("Scree plot",
#              headingLevel = headingLevel+1);

x$output$screePlot;

```


```{r, echo=echoPartial, results='asis', eval=(x$input$residuals) }

# ufs::heading("Residuals",
#              headingLevel = headingLevel+1);

knitTable(
  x$output$residuals,
  caption = "Residuals",
  digits = digits,
);

```

```{r echo=FALSE}
if (!is.null(oldKableViewOption)) {
  options(kableExtra_view_html = oldKableViewOption);
}
```

</div>
<div style="display:block;clear:both;" class="rosetta-data-reduction-end"></div>
