How to append a plot to an existing pdf file

前端 未结 4 1092
刺人心
刺人心 2020-12-13 09:46

I want to append a plot to an existing pdf long after dev.off() has been called*. After reading the pdf() help file and after reading the Q &

4条回答
  •  Happy的楠姐
    2020-12-13 09:50

    This is horribly hacky and probably belies my limited UNIX shell fu, but it works for me on a Fedora 17 box with the pdfjam package installed (not an R package, but from the 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()
    

    The output in R is:

    > 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
    

    Basically, pdfjoin will take input from stdin if it is the only input file so I pipe the output from pdf() to the pdfjoin program and specify the output file using the --outfile argument. Then using && is join the original pdf1.pdf with the pdf2.pdf just created, specifying that the output PDF is pdf1.pdf, the name of the original PDF.

提交回复
热议问题