简体   繁体   English

如何使用spin()编织器生成报告并以pdf格式保存一些图

[英]How to produce a report with spin() knitr AND save some plot in a pdf

I'm a little lost with the knitr package. 我对针织包感到迷茫。 I love the spin() function to quickly give me a global view of my analyses, but I need to save some very important plots in separate pdf (by example the 8th plot of my file should be save as "Figure1.pdf" AND displayed in the report). 我喜欢spin()函数可以快速为我提供我的分析的全局视图,但是我需要将一些非常重要的图保存在单独的pdf中(例如,文件的第8个图应另存为“ Figure1.pdf”并显示在报告中)。

When I write : 当我写:

Some random code and plots...

pdf('Figure1.pdf')
plot(important plot)
dev.off()

Some random code and plots

The important plot is not displayed in the report after a spin(), and I would like to be in the report AND the pdf. spin()之后,重要图未显示在报告中,我想在报告和pdf中显示。

Is there any way ? 有什么办法吗?

Thanks 谢谢

spin() do save all plots, although it saves plots in the PNG format by default, because of the argument spin(..., format = 'Rmd') (R Markdown uses the PNG device). 尽管由于参数spin(..., format = 'Rmd') (R Markdown使用PNG设备), spin()确实会保存所有图,尽管默认情况下会以PNG格式保存图。

You can specify chunk options in the R script as well: just use #+ or #- , eg 您也可以在R脚本中指定块选项:只需使用#+#- ,例如

#' Some random code and plots...

#+ Figure1, fig.path='', dev=c('png', 'pdf')
plot(important plot)

#' Some random code and plots

Note the dev option takes a vector of devices, as @baptiste mentioned. 请注意, dev选项需要一个设备向量,如@baptiste所述。

Or you use the Rnw format, eg (you do not need to specify dev ) 或者您使用Rnw格式,例如(无需指定dev

spin(..., format = 'Rnw')

The output will be LaTeX/PDF instead of HTML in this case. 在这种情况下,输出将是LaTeX / PDF而不是HTML。

If I understand the question, repeating the plot statement after closing the device has worked without producing a duplicate in the report: 如果我理解这个问题,请在关闭设备后重复执行plot语句,但不会在报告中产生重复:

Some random code and plots...

pdf('Figure1.pdf')
plot(important plot)
dev.off()
plot(important plot)

Some random code and plots

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM