Search code examples
rr-markdownpsych

Printing readable factor analysis output in Rmarkdown


I have to print readable factor analysis output in an Rmarkdown document (can ultimately be pdf, word or html), and I am having real trouble doing this. I had this problem here. I am running psych version 1.8.12.

Reproducible code is below, but I have also put up a github respository here:

Note, when I run results='markup' the pdf compiles, but the table is not readable: it prints the latex code. When I run results='asis', an error is returned ! LaTeX Error: \caption outside float.

I am much less familiar with the packages like kabel and texreg. Would those be an option? `


title: "Factor Analysis Test"
author: "Simon Kiss"
date: '2019-06-07'
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(psych)
```

```{r}
data('Thurstone')
```
```{r}
mod<-fa(Thurstone, 3)
```
```{r results='markup'} 
fa2latex(mod)
````

Solution

  • if you want to output a table make sure to convert it to a dataframe first.

    title: "Factor Analysis Test"
    author: "Simon Kiss"
    date: '2019-06-07'
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r}
    library(psych)
    ```
    
    ```{r}
    data('Thurstone')
    ```
    
    ```{r}
    mod<-fa(Thurstone, 3)
    ```
    ```{r results='markup'} 
    knitr::kable(data.frame(psych::fa.sort(mod$loadings)[1:8,]))