I am trying to break to a new page from a R markdown document that is calling a function to produce a set of graphics for each set of data (400+ pages in some cases). I am using LaTeX.
Any ideas? Here is a simple example that demonstrates some my challenge though it doesn't use a function to produce the output.
---
title: "Test Page Break"
output: pdf_document
---
```{r}
summary(cars)
cat("\\pagebreak")
print(cars)
```
Update #1:
My example didn't demostrate the use of a function within the code chunk. Here is a better example.
---
title: "Test Page Break"
output: pdf_document
---
```{r}
pr_w_pagebreak <- function() {
print(summary(cars))
cat("\\pagebreak")
print(cars)
}
pr_w_pagebreak()
```
The trouble you'll have with your chunks is that you want some results to appear as 'markup' and some to appear 'asis'. If you want to get the page break with the single block as you have it, you will need to do:
```{r, echo=-2}
summary(cars)
knitr::asis_output("\\pagebreak")
print(cars)
```
Otherwise, you'll have to do
```{r}
summary(cars)
```
\pagebreak
```{r}
print(cars)
```
And as Floo0 points out, you can get LaTeX code from the R chunks if you use results = 'asis', but you have to be careful because the following will not give you what you want.
```{r, results='asis'}
summary(cars)
cat("\\pagebreak")
print(cars)
````
Related
How can I add an empty code chunk in R markdown? I have found several options to manipulate the html to give more white space. But I would like to present some empty lines in the well known gray code box in order to indicate space for assigments.
---
title: "Untitled"
author: "Author"
output: html_document
---
## R Markdown
```{r cars}
summary(cars)
```
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
```
A hacky way... almost there:
```{r, code="'\n\n\n\n'", results=F}
```
A possible solution using results = 'asis' and relying on chunck HTML class:
```{r, results='asis', echo=F}
cat(
'<pre class="r">
<code class = "hlsj"> <span class="hljs-string"> <br> <br> </span> </code>
</pre>
')
```
Just add <br> to increase the number of lines.
There does not seem to be a way to get knitr to recognise a completely empty chunk as a chunk. It will always omit it, regardless of the chunk options.
You have to insert something to get it to render, for example a comment. So you can put the empty lines between two comments:
---
output: html_document
---
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
# Insert code here
# End
```
Or with the strip.white=FALSE chunk option we can use a single comment line, but strangely this only works for leading, not trailing, whitespace:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(strip.white = FALSE)
```
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
# Insert code above
```
Consider the following R Markdown document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text.
I want all the body text to be double-spaced,
but I want all echoed code from code chunks to be single spaced.
In other words, not this:
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
Is there a canned or relatively painless way to have all normal text double-spaced, but have all code echoed from code chunks single spaced? I tried consulting the guide to chunk options here, but couldn't quite find what I was looking for.
If you are outputting to pdf the most painless way might be adding some LaTeX commands to your Rmd document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text. I want all the body text to be double-spaced, but I
want all echoed code from code chunks to be single spaced. In other words, not
this:
\singlespacing
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
\doublespacing
Some additional body text. Nor hence hoped her after other known defer his.
For county now sister engage had season better had waited. Occasional mrs
interested far expression acceptance. Day either mrs talent pulled men
rather regret admire but. Life ye sake it shed. Five lady he cold in meet up.
Alternatively, you could define a new chunk option using knitr chunk hooks. For instance, you could include in the setup chunk:
```{r setup, include=FALSE}
hook_chunk = knitr::knit_hooks$get('chunk')
knitr::knit_hooks$set(chunk = function(x, options) {
regular_output = hook_chunk(x, options)
# add latex commands if chunk option singlespacing is TRUE
if (isTRUE(options$singlespacing))
sprintf("\\singlespacing\n %s \n\\doublespacing", regular_output)
else
regular_output
})
knitr::opts_chunk$set(echo = TRUE, singlespacing = TRUE)
```
Some useful references:
Hooks - Customizable functions to run before/after a code chunk, tweak the output, and manipulate chunk options
How to Create New Chunk Options in R Markdown
In a .Rmd document, I am generating multiple related figures (from a list) in a knitr chunk.
When knitting to html, those figures are wrapped properly and all visible.
When knitting to pdf, the figures are all one after the other and only the first two are visible (and half of the third). Here is some code that reproduces the issue:
---
title: "Example figure wrapping problem"
output:
pdf_document:
keep_tex: true
classoption:
landscape
---
# SK-N-SH plex panel {.tabset .tabset-fade}
```{r, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=9, fig.show="hold", hightligh=TRUE, warnings=TRUE, error=FALSE, cache=FALSE, echo=FALSE, dpi=100)
```
```{r}
for (ii in 1:6) {
plot(1:3, 1:3, main=ii)
}
```
I figured that the problem comes from the generation of the .tex file, which contains this line:
\includegraphics{figure/unnamed-chunk-2-1.png}\includegraphics{figure/unnamed-chunk-2-2.png}\includegraphics{figure/unnamed-chunk-2-3.png}\includegraphics{figure/unnamed-chunk-2-4.png}\includegraphics{figure/unnamed-chunk-2-5.png}\includegraphics{figure/unnamed-chunk-2-6.png}
Adding line breaks every two includegraphics solves the problems:
\includegraphics{figure/unnamed-chunk-2-1.png}\includegraphics{figure/unnamed-chunk-2-2.png}
\includegraphics{figure/unnamed-chunk-2-3.png}\includegraphics{figure/unnamed-chunk-2-4.png}
\includegraphics{figure/unnamed-chunk-2-5.png}\includegraphics{figure/unnamed-chunk-2-6.png}
However it is obviously not practical as there are many more figures. I could also run sed 's/}\(\\includegraphics\)/}\r\1/g' on the file but it feels like uselessly complicating the compiling process.
Is there a native knitr or rmarkdown way to solve my problem.
If you specify fig.align="center" and fig.show="asis" in the code chunk it seems to work. For example
---
title: "Example figure wrapping problem"
output:
pdf_document:
keep_tex: true
classoption:
landscape
---
# SK-N-SH plex panel {.tabset .tabset-fade}
```{r, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=9, fig.show="hold", hightligh=TRUE, warnings=TRUE, error=FALSE, cache=FALSE, echo=FALSE, dpi=100)
```
```{r fig.align="center",fig.show="asis"}
for (ii in 1:6) {
plot(1:3, 1:3, main=ii)
}
```
looks fine. It generates LaTeX code
\begin{center}\includegraphics{Untitled_files/figure-latex/unnamed-chunk-2-1} \end{center}
\begin{center}\includegraphics{Untitled_files/figure-latex/unnamed-chunk-2-2} \end{center}
etc.
which breaks up the figures. You can use fig.align="right" or fig.align="left" instead, but it's essential that you don't use fig.show="hold", or all the figures are wrapped together, and you return to the original problem.
Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this
I want to have my graph output display on the next slide while the code chunk stays on the first. I am using the default ioslides in Rstudio. I would think that it would be some attribute of the code chunk but I can't figure out what it is.
---
output: ioslides_presentation
---
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
Any one have any idea on how to do this in Rstudio?
I want to use this for educational purposes. First showing the code and than revealing the graph. Now I am stuck with doing double code chunks with echo=FALSE and TRUE and eval=FALSE and TRUE.
It's easier to run the code twice, once not evaluating the code, the second time not showing the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot 1
```{r, eval = FALSE}
plot(1:10)
```
## Plot 2
```{r, echo = FALSE}
plot(1:10)
```
It also avoids the error when smaller is true.
To move a plot to the next slide, you need to add a horizontal rule ---- before it (see documentation). You can modify the default plot hook to do it:
```{r setup, include=FALSE}
library(knitr)
local({
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
paste0('\n\n----\n\n', hook_plot(x, options))
})
})
```
If you want all your plots to appear on the next slide Yihui's answer is the way to go. But if you want some plots to appear on the same slide you may be better off doing it manually, similar to what you are already doing (and what Dario suggested). Except that I would strongly recommend the use of chunk references. That way you avoid the need to duplicate the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot code
```{r cars_plot, echo = TRUE, eval = FALSE}
plot(cars)
```
## Plot display
```{r cars_plot, echo = FALSE, eval = TRUE}
```
```
I agree this would be desirable, but I'm finding ioslides to be buggy. Ioslides project started eith google, but was let go a while ago.
You may have better luck with beamer/home slides. But does require a LaTeX distribution to be installed.