Create multiple charts with unique RMarkdown headers

Using spineplot in R, I am trying to create a series of graphs that compare some potential covariates with age categories, so we can conduct a preliminary exploration of the data. I have this code written in R Markdown, and I use RStudio to insert the file into the HTML output. However, itโ€™s hard for me to get every plot created by my loop to have the corresponding RMarkdown title. I tried putting cat("## my header") in a loop that produces each plot, but for some reason it appears only for the first plot, and not for the rest. It prints for the rest of them, but is not interpreted as Markdown.

Here is 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 result is as follows.

Minimal Working Example Output

+6
source share
2 answers

Turns out you need an extra carriage return. As Yahui pointed out, the header needs an empty string before it will correctly interpret Markdown. Adding a simple cat ("\ n \ n") to the end of my loop fixes things.

I only tried adding one line break, but missed that two were needed to correctly interpret the header.

 --- 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")) ``` 
+5
source

Paste the short example that I prepared for another question :

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

I deleted this example in another question , as it was in the markdown syntax, and did not fit the Latex / Rnw theme.

+1
source

Source: https://habr.com/ru/post/974434/


All Articles