6

Using spineplot in R, I am trying to create a series of plots that compare some potential covariates against age categories so we can do some preliminary data exploration. I have this code written up in R Markdown and I am using RStudio to knit the file into an HTML output. However, I am having difficulty getting each plot produced by my loop to have an appropriate RMarkdown header. I have tried putting cat("## my header") in the loop that produces each plot, but for some reason it only shows up for the first plot, and not the rest. It prints out for the rest of them, but does not get interpreted as Markdown.

Here's an example of my code:

---
title: "Minimal Working Example"
author: "TARehman"
date: "Wednesday, August 27, 2014"
output:
  html_document:
    toc: yes
---

# Spineplots
```{r mwe, echo=FALSE, results='asis', fig.height=5, fig.width=8, dpi=300}
tempdata <- data.frame(age=c("0-1","0-1","1-3","1-3","3-7","10-15","3-7","7-10"),
                       covA=c("Class 0","Class 1","Class 3","Class 2","Class 4","Class 3","Class 1","Class 2"),
                       covB=c("Class 1","Class 3","Class 2","Class 4","Class 4","Class 1","Class 1","Class 0"),
                       covC=c("Class 3","Class 3","Class 2","Class 3","Class 1","Class 4","Class 4","Class 4"))

temp_covars <- c("covA","covB","covC")

temp_locvec <- c(0.1,0.3,0.5,0.7,0.9)
temp_labvec <- c("0-1","1-3","3-7","7-10","10-15")
temp_colvec <- rainbow(n = 5,start = 7/12,end = 9/12,alpha = 0.8)

for(x in temp_covars) {

    cat(paste("## Spineplot of",x,"vs. age groups\n",sep=" "))

    spineplot(x = tempdata[[x]],
              y = tempdata$age,
              ylab = "Age Group",
              xlab = "Class",
              col = temp_colvec,
              off = 0,
              yaxlabels = c(NA,NA,NA,NA,NA),
              main = paste("Classes of",x,"versus age groups",sep=" "))

    for(j in 1:5) {
        axis(side = 2,
             at = temp_locvec[j],
             labels = temp_labvec[j],
             col.axis = temp_colvec[j],
             font.axis = 2)
        }
    rm(j)
}
rm(list=c("x","temp_locvec","temp_labvec","temp_colvec"))
```

The output looks like this.

Minimal Working Example Output

TARehman
  • 6,659
  • 3
  • 33
  • 60
  • if you set the output to md_document, it shows the problem: `##` becomes `\#\#` – baptiste Aug 27 '14 at 19:35
  • Why only on subsequent loops? And is there a way to prevent such a thing? – TARehman Aug 27 '14 at 19:41
  • 3
    this looks like a bug. It works if you add empty lines after each plot. – baptiste Aug 27 '14 at 19:44
  • Wow. It does. I had been doing that, but I'd only added one newline at the end. It worked when I added two newlines. – TARehman Aug 27 '14 at 19:46
  • Can you just answer your own question below? – Yihui Xie Aug 28 '14 at 02:20
  • could someone report this bug? I'm not sure if it's rmarkdown or knitr, and don't have time to investigate – baptiste Aug 28 '14 at 10:51
  • I'll answer my own question sometime today. Yihui is maintainer of knitr, so I think it's reported? – TARehman Aug 28 '14 at 16:36
  • not sure if @Yihui was pinged, typically github is the place to report issues – baptiste Aug 28 '14 at 23:17
  • I'd rather not think this is a bug. It is a markdown issue: a header has to have a blank line before it by default (see http://johnmacfarlane.net/pandoc/README.html#headers). That is why I asked you to answer this question with the solution above. – Yihui Xie Aug 29 '14 at 03:47
  • Sorry for the delay. I answered the question. @Yihui, if you think I could improve it, feel free to suggest an edit. – TARehman Sep 03 '14 at 16:15

2 Answers2

6

It turns out that you need an extra carriage return. As Yihui pointed out, a header needs a blank line before it to be interpreted correctly by Markdown. Adding a simple cat("\n\n") to the end of my loop fixes things.

I had tried only adding one line break, but missed that two were needed to get the header to be interpreted correctly.

---
title: "Working Solution"
author: "TARehman"
date: "September 3, 2014"
output:
  html_document:
    toc: yes
---

# Spineplots
```{r mwe, echo=FALSE, results='asis', fig.height=5, fig.width=8, dpi=300}
tempdata <- data.frame(age=c("0-1","0-1","1-3","1-3","3-7","10-15","3-7","7-10"),
                       covA=c("Class 0","Class 1","Class 3","Class 2","Class 4","Class 3","Class 1","Class 2"),
                       covB=c("Class 1","Class 3","Class 2","Class 4","Class 4","Class 1","Class 1","Class 0"),
                       covC=c("Class 3","Class 3","Class 2","Class 3","Class 1","Class 4","Class 4","Class 4"))

temp_covars <- c("covA","covB","covC")

temp_locvec <- c(0.1,0.3,0.5,0.7,0.9)
temp_labvec <- c("0-1","1-3","3-7","7-10","10-15")
temp_colvec <- rainbow(n = 5,start = 7/12,end = 9/12,alpha = 0.8)

for(x in temp_covars) {

    cat(paste("## Spineplot of",x,"vs. age groups\n",sep=" "))

    spineplot(x = tempdata[[x]],
              y = tempdata$age,
              ylab = "Age Group",
              xlab = "Class",
              col = temp_colvec,
              off = 0,
              yaxlabels = c(NA,NA,NA,NA,NA),
              main = paste("Classes of",x,"versus age groups",sep=" "))

    for(j in 1:5) {
        axis(side = 2,
             at = temp_locvec[j],
             labels = temp_labvec[j],
             col.axis = temp_colvec[j],
             font.axis = 2)
        }
    rm(j)
    cat("\n\n")
}
rm(list=c("x","temp_locvec","temp_labvec","temp_colvec"))
```
TARehman
  • 6,659
  • 3
  • 33
  • 60
  • I've been trying example code from several folks and I can't fix a weird problem: The first and second title show up in the html file, then the first plot, then the third title, then the second plot, then "NA", then third plot. Any way to fix this? – Nova Jun 16 '17 at 17:46
  • @Nova I'd recommend asking a new question, and then creating your example in there. You'll get better responses by doing that. – TARehman Jun 16 '17 at 17:47
  • No love on this question yet, and I'm still struggling with it! https://stackoverflow.com/questions/44595377/r-markdown-bug-when-displaying-plots-in-a-loop-with-headers – Nova Jul 26 '17 at 19:06
1

I paste a short example I had prepared for another question:

```{r, results='asis'}
cat("\n\n## Title")
for (s in unique(cars$speed)){
    cat("\n\n### speed",s)
}
```

I deleted that example in the other question as it was on markdown syntax and didn't fit with Latex/ Rnw topic.

Community
  • 1
  • 1
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110