knitr: include figures in report *and* output figures to separate files
Use the option self_contained: no
if you are using html_document
, or keep_tex: yes
if you use pdf_document
, so that rmarkdown will not remove the figure files after rendering the output document.
Setting knitr to output separate images
self_contained
is an option for html_document
, not a top-level YAML setting. The document below works using just that. PNG is the default figure type, so you don't need to specify that.
---
title: "Untitled"
output:
html_document:
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
In RMarkdown, is there a global option that can be set for all figures and tables to be placed in their own page?
I am doing this by using \pagebreak before and after the coding chunk.
It is possible to program what you do using a knitr
hook.
For instance, the following knitr
hook defines a new pagebreak
chunk option:
knitr::knit_hooks$set(chunk = function(x, options) {
if(identical(options$pagebreak, TRUE)) {
paste("\\pagebreak", x, "\\pagebreak", sep = "\n")
} else {
x
}
})
When this new pagebreak
chunk option is TRUE
, it does exactly what you describe: \pagebreak
is inserted before and after the chunk output.
Therefore, you can use this new chunk option in a code chunk like that:
```{r pressure, pagebreak=TRUE}
plot(pressure)
```
or set this option for all the chunks in your document with:
knitr::opts_chunk$set(pagebreak = TRUE)
Here is a minimal Rmd
file which uses this hook:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::knit_hooks$set(chunk = function(x, options) {
if(identical(options$pagebreak, TRUE)) {
paste("\\pagebreak", x, "\\pagebreak", sep = "\n")
} else {
x
}
})
knitr::opts_chunk$set(pagebreak=TRUE)
```
Some text.
```{r pressure}
plot(pressure)
```
More text.
How to add multiple figures across multiple pages in a chunk using knitr and RMarkdown?
I have solved my problem.
In the generated tex file, there aren't new lines after each figure. This tex code us generated using rmd file above:
\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-2.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-3.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}
The solution is to add a new line after each cycle to print a figure.
cat('\r\n\r\n')
Not sure why I need two "\r\n" here. The generated tex file looks like:
\includegraphics{test_files/figure-latex/plot_phenotype-1.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-2.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-3.pdf}
\includegraphics{test_files/figure-latex/plot_phenotype-4.pdf}
This is the full example of my Rmd file
---
title: "Knitr test"
output:
pdf_document:
keep_tex: yes
date: "6 April 2015"
---
## Section A
Row B
```{r plot_phenotype, echo = FALSE, fig.height=8, fig.width=6.5}
library(ggplot2)
library(grid)
for (i in seq(1, 4))
{
grid.newpage()
p <- ggplot(cars, aes(speed, dist)) + geom_point()
print(p)
cat('\r\n\r\n')
}
```
How to place table of contents on separate page using Knitr for PDF without using separate files?
You could add \\clearpage
at the end of your abstract. Therefore, it would become:
abstract: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\\clearpage"
You need to add one aditional slash (\
) to escape the LaTeX command \clearpage
.
-output
Related Topics
Devtools::Install_Github() - Ignore Ssl Cert Verification Failure
Get the Size of the Window in Shiny
Numbers as Column Names of Data Frames
Unknown Timezone Name in R Strptime/As.Posixct
Using Dplyr Within a Function, Non-Standard Evaluation
Problems Formatting Date into Format "%Y-%M"
Display a Summary Line Per Facet Rather Than Overall
R Subset with Condition Using %In% or ==. Which One Should Be Used
How to Produce a Heatmap with Ggplot2
Convert Hours:Minutes:Seconds to Minutes
Represent Numeric Value with Typical Dollar Amount Format
How to Jitter Both Geom_Line and Geom_Point by the Same Magnitude
How to Select Non-Numeric Columns Using Dplyr::Select_If