Not only would I like my figures to appear in my knitr-generated report, but I would also like to output them to separate files, too. To do this, I have included code like the following:
```{r}
#Plot figure in report
plot(x,y)
#Plot figure in file
pdf(file="MyFig.pdf")
plot(x,y)
dev.off()
```
This works fine, but I expect there's a more elegant solution for this already built into knitr. Is there a chunk option or something similar that achieves the same results?
knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.
When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.
knitr is an R package that integrates computing and reporting. By incorporating code into text documents, the analysis, results and discussion are all in one place.
You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.
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.
Keyword dev='pdf'
as explained by Yihui here http://yihui.name/knitr/options/
Together with other options I have found useful:
```{r 'setup', echo = FALSE, cache = FALSE}
opts_chunk$set(dev = c('pdf', 'png'),
fig.align = 'center', fig.height = 5, fig.width = 8.5,
pdf.options(encoding = "ISOLatin9.enc"))
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With