
------

<div style="display:block;clear:both;" class="conceptual-independence-matrix-start"></div>

```{r cim-heading, eval=TRUE, echo=FALSE}

digits <- x$input$digits;

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

mainHeadingLevel <- headingLevel;
varsHeadingLevel <- headingLevel + 1;
warnHeadingLevel <- headingLevel + 2;
eachWarnHeadingLevel <- headingLevel + 3;

### Headings in HTML go to max level 6
if (eachWarnHeadingLevel < 7) {
  warnTabSet <- "{.tabset}"
} else {
  warnTabSet <- ""
}

if (varsHeadingLevel < 7) {
  mainTabSet <- "{.tabset}"
} else {
  mainTabSet <- ""
}

```

`r ufs::repStr("#", mainHeadingLevel);` Conceptual Independence Matrix (CIM) `r mainTabSet`

This table shows the scales (which should correspond to constructs) and items within each scale (which should correspond to aspects of that construct) between which this CIM shows the conceptual independence, as well as the abbreviations that are used:

```{r cim-abbreviationLegend, tab.cap="Scales, items, and abbreviations.", echo=echoPartial, results='asis'}
knitr::kable(x$intermediate$abbreviationLegend);
```


```{r cim-graphs, echo=echoPartial, results='asis', fig.width=14, fig.height=8}

for (rowVar in names(x$input$scales)) {
  rowIndex <- which(names(x$input$scales) == rowVar);

  for (colVar in names(x$input$scales)) {
    colIndex <- which(names(x$input$scales) == colVar);
    ### Only for one half; not for the diagonal itself and
    ### the other half, because we combine those two.

    if (colIndex > rowIndex) {
      ufs::cat0("\n\n",
                ufs::repStr("#", varsHeadingLevel),
                " Conceptual (in)dependence of ",
                rowVar, " and ",
                colVar, "\n\n");
      ufs::knitFig(
        gridExtra::arrangeGrob(
          grobs=list(x$intermediate$grobsList[[rowIndex]][[colIndex]],
                     x$intermediate$grobsList[[colIndex]][[rowIndex]]),
          ncol=2),
        figWidth = 14,
        figHeight = 8,
        figCaption=paste0("Factor loadings for ", rowVar, " and ", colVar),
        catPlot=TRUE);
      cat("\n\n");
      
      ###----------------------------------------------------------------------
      ### EFA error, warning, message printing
      ###----------------------------------------------------------------------
    
      allErrors_efa <- 
        c(x$intermediate$errorsList[[rowIndex]][[colIndex]]$efa, 
          x$intermediate$errorsList[[colIndex]][[rowIndex]]$efa);
      allWarnings_efa <- 
        c(x$intermediate$warningsList[[rowIndex]][[colIndex]]$efa, 
          x$intermediate$warningsList[[colIndex]][[rowIndex]]$efa);
      allMessages_efa <- 
        c(x$intermediate$messagesList[[rowIndex]][[colIndex]]$efa, 
          x$intermediate$messagesList[[colIndex]][[rowIndex]]$efa);
            
      ###----------------------------------------------------------------------
      ### CFA for one factor error, warning, message printing
      ###----------------------------------------------------------------------

      allErrors_cfa1 <- 
        c(x$intermediate$errorsList[[rowIndex]][[colIndex]]$cfa1, 
          x$intermediate$errorsList[[colIndex]][[rowIndex]]$cfa1);
      allWarnings_cfa1 <- 
        c(x$intermediate$warningsList[[rowIndex]][[colIndex]]$cfa1, 
          x$intermediate$warningsList[[colIndex]][[rowIndex]]$cfa1);
      allMessages_cfa1 <- 
        c(x$intermediate$messagesList[[rowIndex]][[colIndex]]$cfa1, 
          x$intermediate$messagesList[[colIndex]][[rowIndex]]$cfa1);
      
      ###----------------------------------------------------------------------
      ### CFA for two factors error, warning, message printing
      ###----------------------------------------------------------------------

      allErrors_cfa2 <- 
        c(x$intermediate$errorsList[[rowIndex]][[colIndex]]$cfa2, 
          x$intermediate$errorsList[[colIndex]][[rowIndex]]$cfa2);
      allWarnings_cfa2 <- 
        c(x$intermediate$warningsList[[rowIndex]][[colIndex]]$cfa2, 
          x$intermediate$warningsList[[colIndex]][[rowIndex]]$cfa2);
      allMessages_cfa2 <- 
        c(x$intermediate$messagesList[[rowIndex]][[colIndex]]$cfa2, 
          x$intermediate$messagesList[[colIndex]][[rowIndex]]$cfa2);

      ###----------------------------------------------------------------------
      ### Processing errors, warnings, messages: removing 'warning' messages
      ###----------------------------------------------------------------------
      
      allWarnings_efa <-
        allWarnings_efa[(seq_along(allWarnings_efa) %% 2) == 0];
      
      allWarnings_cfa1 <-
        allWarnings_cfa1[(seq_along(allWarnings_cfa1) %% 2) == 0];
      
      allWarnings_cfa2 <-
        allWarnings_cfa2[(seq_along(allWarnings_cfa2) %% 2) == 0];
      
      ### Reapply logic to messages and warnings, if those also 
      ### consistently include an 'introduction' line.

      ###----------------------------------------------------------------------
      ### Show errors, warnings, messages
      ###----------------------------------------------------------------------

      if (length(c(allErrors_efa, allWarnings_efa, allMessages_efa,
                   allErrors_cfa1, allWarnings_cfa1, allMessages_cfa1,
                   allErrors_cfa2, allWarnings_cfa2, allMessages_cfa2)) == 0) {
        ufs::cat0("\n\n",
                  ufs::repStr("#", warnHeadingLevel),
                  " No messages, warnings, or errors encountered at all\n\n");
      } else {
        ufs::cat0("\n\n",
                  ufs::repStr("#", warnHeadingLevel),
                  " Messages, warnings, and errors ",
                  warnTabSet,
                  "\n\n");
        ufs::cat0("Messages, warnings, and/or errors were encountered. ");
        if (nchar(warnTabSet) > 0) {
          ufs::cat0("To see the specific messages, warnings, and errors, ",
                    "click the tabs.");
        }
        ufs::cat0("\n\n",
                  "The following numbers of messages, warnings, and ",
                  "errors were encountered:\n\n");
        
        ufs::cat0("- EFA:\n\n");
        ufs::cat0("  - Errors: ", length(allErrors_efa), "\n\n");
        ufs::cat0("  - Warnings: ", length(allWarnings_efa), "\n\n");
        ufs::cat0("  - Messages: ", length(allMessages_efa), "\n\n");
        ufs::cat0("- CFA1:\n\n");
        ufs::cat0("  - Errors: ", length(allErrors_cfa1), "\n\n");
        ufs::cat0("  - Warnings: ", length(allWarnings_cfa1), "\n\n");
        ufs::cat0("  - Messages: ", length(allMessages_cfa1), "\n\n");
        ufs::cat0("- CFA2:\n\n");
        ufs::cat0("  - Errors: ", length(allErrors_cfa2), "\n\n");
        ufs::cat0("  - Warnings: ", length(allWarnings_cfa2), "\n\n");
        ufs::cat0("  - Messages: ", length(allMessages_cfa2), "\n\n");

        if (nchar(warnTabSet) > 0) {
          ufs::cat0("\n\n",
                    "The specific errors, warnings, and message that ",
                    "were encountered are listed in the other tabs.");
        } else {
          ufs::cat0("\n\n",
                    "The specific errors, warnings, and message that ",
                    "were encountered are listed below.");
        }

        ### EFA

        if (length(allErrors_efa) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " EFA errors\n\n");
          cat("\n\nEncountered the following errors during EFA:\n\n");
          print(knitr::kable(table(allErrors_efa)));
        }

        if (length(allWarnings_efa) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " EFA warnings\n\n");
          cat("\n\nEncountered the following warnings during EFA:\n\n");
          print(knitr::kable(table(allWarnings_efa)));
        }
    
        if (length(allMessages_efa) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " EFA messages\n\n");
          cat("\n\nEncountered the following messages during EFA:\n\n");
          print(knitr::kable(table(allMessages_efa)));
        }
  
        ### CFA 1
  
        if (length(allErrors_cfa1) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " One-factor CFA errors\n\n");
          cat("\n\nEncountered the following errors during one-factor CFA:\n\n");
          print(knitr::kable(table(allErrors_cfa1)));
        }
    
        if (length(allWarnings_cfa1) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " One-factor CFA warnings\n\n");
          cat("\n\nEncountered the following warnings during one-factor CFA:\n\n");
          print(knitr::kable(table(allWarnings_cfa1)));
        }
    
        if (length(allMessages_cfa1) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " One-factor CFA messages\n\n");
          cat("\n\nEncountered the following messages during one-factor CFA:\n\n");
          print(knitr::kable(table(allMessages_cfa1)));
        }
  
        ### CFA 2
          
        if (length(allErrors_cfa2) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " Two-factor CFA errors\n\n");
          cat("\n\nEncountered the following errors during two-factor CFA:\n\n");
          print(knitr::kable(table(allErrors_cfa2)));
        }

        if (length(allWarnings_cfa2) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " Two-factor CFA warnings\n\n");
          cat("\n\nEncountered the following warnings during two-factor CFA:\n\n");
          print(knitr::kable(table(allWarnings_cfa2)));
        }
  
        if (length(allMessages_cfa2) > 0) {
          ufs::cat0("\n\n", ufs::repStr("#", eachWarnHeadingLevel),
                    " Two-factor CFA messages\n\n");
          cat("\n\nEncountered the following messages during two-factor CFA:\n\n");
          print(knitr::kable(table(allMessages_cfa2)));
        }
        
      }
      
      cat("\n\n");
            
    }
  }
}

```

<div style="display:block;clear:both;" class="conceptual-independence-matrix-end"></div>

------
