0

I'm having difficulty using psych::fa2latex in the context of an RMarkdown document to produce a factor analysis result. If you just run the first bit of code in an RMarkdown document in RStudio (1.0.153), you get an error message : " LaTeX Error: \caption outside float” and the document does not compile. Thanks for any help.

##Place In RMarkdown Document, save and compile. 
---
title: "Test"
author: "Simon Kiss"
date: '2018-01-09'
output: pdf_document
---

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

```{r code, results='asis'}
data("Thurstone")
mod<-fa(Thurstone, nfactors=3)
fa2latex(mod)
```

However, if you take the raw latex output that is produced by fa2latex() and put it straight into a Sweave document, it compiles just fine.

#Place in Sweave document, save and compile. 
\documentclass{article}

\begin{document}

\begin{table}[htpb]\caption{fa2latex}
\begin{center}
\begin{scriptsize} 
\begin{tabular} {l r r r r r r }
 \multicolumn{ 6 }{l}{ A factor analysis table from the psych package in R } \cr 
 \hline Variable  &   MR1  &  MR2  &  MR3  &  h2  &  u2  &  com \cr 
  \hline 
Sentences   &  \bf{ 0.90}  &  -0.03  &   0.04  &  0.82  &  0.18  &  1.01 \cr 
 Vocabulary   &  \bf{ 0.89}  &   0.06  &  -0.03  &  0.84  &  0.16  &  1.01 \cr 
 Sent.Completion   &  \bf{ 0.84}  &   0.03  &   0.00  &  0.74  &  0.26  &  1.00 \cr 
 First.Letters   &   0.00  &  \bf{ 0.85}  &   0.00  &  0.73  &  0.27  &  1.00 \cr 
 Four.Letter.Words   &  -0.02  &  \bf{ 0.75}  &   0.10  &  0.63  &  0.37  &  1.04 \cr 
 Suffixes   &   0.18  &  \bf{ 0.63}  &  -0.08  &  0.50  &  0.50  &  1.20 \cr 
 Letter.Series   &   0.03  &  -0.01  &  \bf{ 0.84}  &  0.73  &  0.27  &  1.00 \cr 
 Pedigrees   &  \bf{ 0.38}  &  -0.05  &  \bf{ 0.46}  &  0.51  &  0.49  &  1.96 \cr 
 Letter.Group   &  -0.06  &   0.21  &  \bf{ 0.63}  &  0.52  &  0.48  &  1.25 \cr 
\hline \cr SS loadings & 2.65 &  1.87 &  1.49 &  \cr  
\cr 
            \hline \cr 
MR1   & 1.00 & 0.59 & 0.53 \cr 
 MR2   & 0.59 & 1.00 & 0.52 \cr 
 MR3   & 0.53 & 0.52 & 1.00 \cr 
 \hline 
\end{tabular}
\end{scriptsize}
\end{center}
\label{default}
\end{table} 
spindoctor
  • 1,719
  • 1
  • 18
  • 42
  • Your piece of `Rmarkdown` file is compiling just fine here. What are you using to compile it? – Willian Vieira Jan 09 '18 at 20:21
  • RStudio 1.0.153, pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) and Latexmk – spindoctor Jan 09 '18 at 20:50
  • I should be clear, I'm trying to compile to a pdf, not an html document? – spindoctor Jan 09 '18 at 20:55
  • Yes, it also works here as pdf. What version of the `psych` package are you using (`packageVersion("psych")`)? Seems there was a [bug](https://stackoverflow.com/questions/31024409/output-of-fa2latex-command-from-psych-causing-latex-error-in-pandoc) in the package. – Willian Vieira Jan 09 '18 at 21:12
  • Hi, Version 1.7.8 – spindoctor Jan 09 '18 at 21:15
  • What are you using to compile this? – spindoctor Jan 09 '18 at 21:42
  • I'm compiling straight on terminal by `Rscript -e "rmarkdown::render('FILE.Rmd')"`. Should be the same Rstudio does. – Willian Vieira Jan 10 '18 at 00:49
  • So I tried that, and I got precisely the same error! – spindoctor Jan 10 '18 at 01:28
  • Hi: I think I know what might be happening. When I run fa2latex(mod) the first line of output is actually `% Called in the psych package fa2latex % Called in the psych package mod `. When I preview the code as a notebook, it does sort of compile but the output looks like this: `% Called in the psych package fa2latex % Called in the psych package mod \begin{table}[htpb]` THEN, it proceeds to print the actual Latex code. But somehow, the `\caption{}` command is stripped out. So,I wonder if there is a way to suppress the printing of that comment? – spindoctor Jan 10 '18 at 15:04
  • I notice that there is a way to suppress similar output from the xtable package, see here [link](https://stackoverflow.com/questions/24400308/how-to-remove-the-lines-in-xtable-table-output-by-knitr), but how do I get that to work to suppress stuff from psych(). I already tried `options(psych.comment=F)` and it didn't work. – spindoctor Jan 10 '18 at 15:05
  • Also weird: I tried a brand new Sweave document in RStudio with only the following code. `\documentclass{article} \begin{document} <<>>= library(psych) @ <<>>= data(Thurstone) summary(Thurstone) @ <<>>= mod<-fa(Thurstone, nfactors=2) @ <<>>= print(mod) @ <>= fa2latex(mod) @ \end{document} ` It compiled beautifully. So what is happening with knitting a markdown document to pdf? – spindoctor Jan 10 '18 at 15:10
  • 1
    This actually provided me the cleanest solution. [link](https://stackoverflow.com/questions/42027792/input-tex-in-rmarkdown). Write the results from fa2latex to a file, with `results='hide'`, then read it right back in. – spindoctor Jan 16 '18 at 19:16

0 Answers0