我想dev.off()在调用*之后很长时间将一个图添加到现有的pdf中.在阅读了pdf()帮助文件后,在这里和这里阅读了问答后,我很确定它不能在R中完成.但是,也许你们中的一些聪明人有一个我无法找到的解决方案.
pdf("Append to me.%03d.pdf",onefile=T)
plot(1:10,10:1) #First plot (page 1)
dev.off()
pdf("Append to me.%03d.pdf",onefile=T)
plot(1:10,rep(5,10)) #Want this one on page 2
dev.off()
Run Code Online (Sandbox Code Playgroud)
*这不是上面链接的问题的重复,因为我想在pdf设备关闭后附加到pdf文件.
Mat*_*rde 21
您可以使用recordPlot将每个绘图存储在a中list,然后将它们全部写入pdf文件中replayPlot.这是一个例子:
num.plots <- 5
my.plots <- vector(num.plots, mode='list')
for (i in 1:num.plots) {
plot(i)
my.plots[[i]] <- recordPlot()
}
graphics.off()
pdf('myplots.pdf', onefile=TRUE)
for (my.plot in my.plots) {
replayPlot(my.plot)
}
graphics.off()
Run Code Online (Sandbox Code Playgroud)
Jos*_*ien 18
如果您愿意安装小型,免费,平台无关的pdftk utililty,您可以使用R中的系统调用将所有数字拼接在一起:
## A couple of example pdf docs
pdf("Append to me.1.pdf")
plot(1:10,10:1)
dev.off()
pdf("Append to me.2.pdf")
plot(1:10,rep(5,10))
dev.off()
## Collect the names of the figures to be glued together
ff <- dir(pattern="Append to me")
## The name of the pdf doc that will contain all the figures
outFileName <- "AllFigs.pdf"
## Make a system call to pdftk
system2(command = "pdftk",
args = c(shQuote(ff), "cat output", shQuote(outFileName)))
## The command above is equiv. to typing the following at the system command line
## pdftk "Append to me.1.pdf" "Append to me.2.pdf" cat output "AllFigs.pdf"
Run Code Online (Sandbox Code Playgroud)
这是可怕的hacky并且可能掩盖了我有限的UNIX shell fu,但它适用于安装了pdfjam软件包的Fedora 17盒子(不是R软件包,而是来自YUM repos)
pdf("pdf1.pdf")
plot(1:10)
dev.off()
pdf("| pdfjoin --outfile \"pdf2.pdf\" && pdfjoin pdf1.pdf pdf2.pdf --outfile pdf1.pdf && rm pdf2.pdf")
plot(10:1)
dev.off()
Run Code Online (Sandbox Code Playgroud)
R中的输出是:
> pdf("| pdfjoin --outfile \"pdf2.pdf\" && pdfjoin pdf1.pdf pdf2.pdf --outfile pdf1.pdf && rm pdf2.pdf")## && pdfunite joined.pdf tmp.pdf joined.pdf && rm tmp.pdf")
> plot(10:1)
> dev.off()
----
pdfjam: This is pdfjam version 2.08.
pdfjam: Reading any site-wide or user-specific defaults...
(none found)
pdfjam: No PDF/JPG/PNG source specified: input is from stdin.
pdfjam: Effective call for this run of pdfjam:
/usr/bin/pdfjam --fitpaper 'true' --rotateoversize 'true' --suffix joined --outfile pdf2.pdf -- /dev/stdin -
pdfjam: Calling pdflatex...
pdfjam: Finished. Output was to 'pdf2.pdf'.
----
pdfjam: This is pdfjam version 2.08.
pdfjam: Reading any site-wide or user-specific defaults...
(none found)
pdfjam: Effective call for this run of pdfjam:
/usr/bin/pdfjam --fitpaper 'true' --rotateoversize 'true' --suffix joined --outfile pdf1.pdf -- pdf1.pdf - pdf2.pdf -
pdfjam: Calling pdflatex...
pdfjam: Finished. Output was to 'pdf1.pdf'.
null device
1
Run Code Online (Sandbox Code Playgroud)
基本上,如果它是唯一的输入文件,pdfjoin将接受输入,stdin因此我将输出pdf()传递给pdfjoin程序并使用--outfile参数指定输出文件.然后使用&&is将原始文件pdf1.pdf与pdf2.pdf刚刚创建的文档连接起来,指定输出PDF是pdf1.pdf原始PDF的名称.