Search code examples
rpapaja

r object not printed in the text


Some R objects that are printed in Rmarkdown without a problem but is not printed using papaja template. It does not generate any any error message. For example, let's say I wrote a markdown file as below:

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


```{r}
library("pacman")
p_load(plyr, dplyr, ggplot2, lmSupport, lme4, psycho, psych, 
GPArotation, tidyverse, tinytex, afex, foreign,purrr, lavaan, citr, 
papaja)
options(scipen = 0, digits = 3) 
DF <- data.frame(id = paste0("ID.", 1:200), 
    x = sample(c("control", "treat"), 200, replace = TRUE),
    y = rnorm(200))
m <- lm(y ~x, data= DF)
summary(m)
s1<-apa_print.lm(m)
s1$statistic[2]
```

# Result

I fitted a  linear regression model in which condition (control vs. 
treat) predicts scores. Treat group showed significantly higher scores 
compared to control group, `r s1$estimate[2]`, `r s1$statistic[2]`. 

Solution

  • Good question. This is not intended behavior in papaja and will be fixed shortly in the development version. The issue is that the inline hook doesn't properly handle lists. If you select the list element such that the output object is a vector it should also work in the current version.

    You could either use, s1$estimate[2][1] as you have found, or s1$estimate[[2]], but personally, I'd prefer indexing by name via s1$estimate[["xtreat"]] or s1$estimate$xtreat.

    As an aside, if you want to report estimates and test statistics, you can use the full_result-element.

    So for your example, I'd suggest:

    ```{r setup, include = FALSE}
    library("papaja")
    ```
    
    ```{r}
    DF <- data.frame(id = paste0("ID.", 1:200), 
        x = sample(c("control", "treat"), 200, replace = TRUE),
        y = rnorm(200))
    m <- lm(y ~ x, data = DF)
    s1 <- apa_print.lm(m)
    ```
    
    # Result
    
    I fitted a  linear regression model in which condition (control vs. 
    treat) predicts scores. Treat group showed significantly higher scores 
    compared to control group, `r s1$full_result$xtreat`.