Output printed when I run the chunk but not when I knit it

Hi,
The code below is a reproducible example (for me).

When I run the chunk in R (from RStudio) b1 is printed out as expected but when I knit it it says that b1 is NULL


title: "example"
author: "Alasdair Noble"
date: "21/05/2020"
output: word_document
editor_options:
chunk_output_type: console

library(lme4)
library(predictmeans)
mydat <- NULL
mydat$Grp <- c("A","A","A","B","B","B")
mydat$resp <- c(1,2,3,4,5,6)
mydat$exp <- as.factor(c(1,2,3,1,2,3))
mydat <- as.data.frame(mydat)



m1 <-  lmer(resp ~ exp + (1|Grp), data=mydat)  
sm1 <- summary(m1)
sm1
pm1 <- predictmeans(m1,"exp",pairwise=TRUE,adj="tukey", Df=3, plot=FALSE)
pm1
b <- lapply(pm1, attributes)
b1 <- b$"Pairwise p-value"$"Letter-based representation of pairwise comparisons at significant level ‘0.05’"
b1

For me this is working after enclosing your yaml section with --- and the R chunk with triple back-ticks.
The full file should look like

---
title: "example"
author: "Alasdair Noble"
date: "21/05/2020"
output: word_document
editor_options:
  chunk_output_type: console
--- 
  
```{r}
library(lme4)
library(predictmeans)
mydat <- NULL
mydat$Grp <- c("A","A","A","B","B","B")
mydat$resp <- c(1,2,3,4,5,6)
mydat$exp <- as.factor(c(1,2,3,1,2,3))
mydat <- as.data.frame(mydat)

m1 <-  lmer(resp ~ exp + (1|Grp), data=mydat)  
sm1 <- summary(m1)
sm1
pm1 <- predictmeans(m1,"exp",pairwise=TRUE,adj="tukey", Df=3, plot=FALSE)
pm1
b <- lapply(pm1, attributes)
b1 <- b$"Pairwise p-value"$"Letter-based representation of pairwise comparisons at significant level ‘0.05’"
b1
```

I was too quick it seems. I think I see the problem.

When I insert print statements at the end of your program

b <- lapply(pm1, attributes)
print(b)
b1 <- b$`Pairwise p-value`$`Letter-based representation of pairwise comparisons at significant level ‘0.05’`
print(b1)

then I see that the 'pairwise' component is not present in b when knitted.
(NB I replaced the " by a back-tick because I think " should not work but apparently it does in the non-knitr version ?? )

No idea where this different behaviour comes from.

Also in interactive work the first print(b1) shows no result.
Only when I do it a second time it will show the result.
Again no idea.

The output in the console and the docx also shows

unable to evaluate scaled gradient
Model failed to converge: degenerate  Hessian with 1 negative eigenvalues

but that is probably not related to the problem of not printing a result.

I think I made (only :wink:) two mistakes:

  • I said that the 'pairwise' component was not present. It is present .
  • I copied your statement with the definition of b1 from this page. I did not notice the 'strange' quotes around 0.05 !
    When I replace the 'strange' quotes by normal quotes, the output is as expected.

The complete input with the two inserted print statements is then:

---
title: "example"
author: "Alasdair Noble"
date: "21/05/2020"
output: word_document
editor_options:
chunk_output_type: console
--- 
  
```{r}
# editor_options:
#   chunk_output_type: console
library(lme4)
library(predictmeans)
mydat <- NULL
mydat$Grp <- c("A","A","A","B","B","B")
mydat$resp <- c(1,2,3,4,5,6)
mydat$exp <- as.factor(c(1,2,3,1,2,3))
mydat <- as.data.frame(mydat)

m1 <-  lmer(resp ~ exp + (1|Grp), data=mydat)  
sm1 <- summary(m1)
sm1
pm1 <- predictmeans(m1,"exp",pairwise=TRUE,adj="tukey", Df=3, plot=FALSE)
pm1
b <- lapply(pm1, attributes)
print(b)
b1 <- b$`Pairwise p-value`$`Letter-based representation of pairwise comparisons at significant level '0.05'`
print(b1)
```

I have resolved this by extracting the output using square brackets b[[5]][5] rather than using the name with strange inverted commas.
Thanks for the help

Alasdair

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.