I am trying to find a way to integrate statistical output from the apa R package in Sweave.
I need to use the ez package for my statistical analysis (as I need type 3 sum of squares). This works perfectly with Rmarkdown, but I am having problems with the format of the output in Sweave.
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
\section*{Results}
<<echo=FALSE, results=hide>>=
library(knitr)
library(ez)
library(apa)
subject<- c(1:40)
reward<- rep(c("p", "s", "p", "s", "s", "p", "s", "p", "s", "p"), 4)
class<- rep(c("ST", "GT", "ST", "GT", "GT", "ST", "GT", "ST", "GT", "ST"), 4)
value<- runif(40, min=0, max=5)
df<- data.frame(subject, reward, class, value)
df$subject<- as.factor(df$subject)
analysis<- ezANOVA(data= df, dv=.(value), wid= .(subject),between = .(class), detailed= TRUE, type= 3)
apa.format<- apa(analysis, format= "text")
@
The results, \Sexpr{(apa.format[2,2])}, indicate...
\end{document}
The result it should produce is:
F(1, 38) = 0.93, p = .341, ηp² = .02
I have tried to change the format from the apa
function, and every output produces a different issue:
* latex: produces textit (and it does not make the 'F' italic or the 'p')
* markdown: makes the the '*' instead of producing italic.
* text: does not produce the italic or the partial eta squared symbol.
This can be achieved easily in Rmarkdown, however I had many other issues.
Suggestions? This may include: different package for APA reporting in Sweave, a different way to integrate Latex and Rcode (and use packages) or a fix to the previous output.
Thanks!
The \Sexpr
macro appears to "eat" the backslashes that apa()
puts into the output, so you need to double them. So choose "latex"
format, and then double the backslashes:
apa.format <- apa(analysis, format= "latex")
apa.format <- gsub("\\", "\\\\", as.matrix(apa.format), fixed = TRUE)
This fixes all entries of apa.format
so they are compatible with \Sexpr
. The as.matrix
is necessary, because apa.format
is a tibble, and it wouldn't work nicely with gsub()
. (I imagine you could do the same using mutate
or something like that, if you know what you're doing with tibbles. I don't.)
Output is