knitr: include figures in report *and* output figures to separate files

柔情痞子 提交于 2019-11-27 02:20:07

问题


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?


回答1:


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.




回答2:


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")) 
```


来源:https://stackoverflow.com/questions/27992239/knitr-include-figures-in-report-and-output-figures-to-separate-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!