Removing pagebreak/space between heading and first graph of RMarkdown 'asis' output.

Hi, does anyone know how to remove the pagebreak between heading and first graph of RMarkdown 'asis' output.

In an rmd file.

---
title: test
output:
  word_document
---

```{r, mtcars-plots, echo=FALSE, results='asis', fig.width=8, fig.height=9}
for (i in names(mtcars)) {
  cat('\n\n# Summary of the variable `', i, '`\n\n')
  x <- mtcars[, i]
  hist(x, xlab = i, main = '')
  plot(table(x), xlab = i, ylab = 'Frequency', lwd = 10)
}

For example, the output of the code above is:

How would you remove the white space?

Hello @williaml ,

I am able to reproduce the issue with the code you have shared.

One way to address the issue is by decreasing the height of the plots/figures using fig.height, consider the example here:

---
title: "test pagge breaks"
output: word_document
date: "2023-01-10"
---




```{r, mtcars-plots, echo=FALSE, results='asis', fig.width=8, fig.height=4}
for (i in names(mtcars)) {
  cat('\n\n# Summary of the variable `', i, '`\n\n')
  x <- mtcars[, i]
  hist(x, xlab = i, main = '')
  plot(table(x), xlab = i, ylab = 'Frequency', lwd = 10)
}

Alternatively, my guess is that creating a reference template of word docx and using it in YAML can help as well. you can read more about it here. This reference docx/template can have the margins and other styles of your prefrence.

Hope this helps,
Ayush

Thanks for the response @AyushBipinPatel In my real dataset, the graphs need to be that size. I'll look in to the YAML though, but I've also got a standard template to work with.

The answer that it just needs extra text lines. An extra cat line before and between the graphs solved it.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.