[R] Getting file name from pdf device?

Tom Short tshort.rlists at gmail.com
Sat Aug 1 23:21:17 CEST 2009

On Fri, Jul 31, 2009 at 8:49 AM, Rainer M Krug<r.m.krug at gmail.com> wrote:
> My question: how can I get the filename of the pdf from the device
> before it is closed?

I've also looked for this and couldn't find a way. I had a similar
use, where I wanted to get an R transcript with embedded plots in
emacs (see prettyR for another transcript-with-plots option). What I
did was use dev2bitmap to write out a PNG file. You could do something
similar with dev.copy2pdf to create the pdf after you do the plotting.
You could also use dev2bitmap in this manner to drive ghostscript to
create pdf's for you (I don't know if it'll compress like you want).
Here's what I did:

show <- function(file = paste(tempfile(), ".png", sep = "")) {
    cat("[[", file, "]]\n", sep = "") # I do some post-processing in
emacs to see the embedded graphic

My use case was that plots would be inserted where I used "show" as follows:

show()    # <---- plot inserted into transcript here
show("cos.png") # this time, a named local file instead of a temp file

- Tom

More information about the R-help mailing list