Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new line within an RMarkdown chunk

Tags:

r

When working in R Markdown, I typically add text outside of a chunk (e.g., figure/tables headings) but in this case I need to loop through 50+ variables to create a couple plots specific to each variable. Hence, I'm adding a few lines of text within the code chunk so that it is included with each set of plots. Ideally, I'd also like to add a page break before moving on to the next variable as well. I'm knitting to Word (this is the format that has been requested of me).

I've tried adding spaces after each line, "/newline", "/n", without success (I've tried with one backslash and two, as I wasn't clear which to use for a Word doc). I could just edit the document manually but that kind of defeats the purpose! Any tips would be appreciated.

```{r create plots, echo=FALSE, fig.height=8, fig.width=8, results='asis'}

for (i in seq(1, nrow(variables))) {
  x <- variables[i,]
  x <- drop.levels(x)
  var <- x$variables

# Add some text
cat("Variable: **", paste(var), "** (N = ", number obs, ")", sep="")  
cat("\n")  
cat("More Text")    
cat("\n")  
cat("Even more text")  

# Create plots.....

# insert page break
cat("\pagebreak")
}

```

like image 924
Jean S. Avatar asked Mar 29 '18 16:03

Jean S.


1 Answers

After following the directions at How to add a page break in word document generated by RStudio & markdown to create the reference_docx, this seems to do what you want

---
output: 
  word_document:
     reference_docx: headingfive.docx
---
```{r, results="asis"}
for (var in LETTERS[1:6]){
  # Add some text
  cat("Variable: **", paste(var), "**",  sep="")  
  cat("  \n")  
  cat("More Text")    
  cat("  \n")  
  cat("Even more text")  
  cat("  \n")

  # Create plots.....

  # insert page break
  cat("\n")
  cat("#####\n")
  cat("\n")


}
```

The extra "\n" at the end of the level 5 heading, and before/after, are important.

like image 182
atiretoo Avatar answered Nov 06 '22 21:11

atiretoo