Hi IU would like to modify the dev.off function, so that it automatically compresses the created pdf. I am thinking of doing the following in the modified dev.off: 1) Get the filename of the pdf device 2) close the device by calling the original dev.off() 3) compress the pdf file with a system call of pdftk. My question: how can I get the filename of the pdf from the device before it is closed? Thanks Rainer -- Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch University, South Africa
Rainer M Krug-6 wrote:> > > My question: how can I get the filename of the pdf from the device > before it is closed? >Setting a hook might work (not tried, though) http://finzi.psych.upenn.edu/R/Rhelp08/2009-February/188168.html Dieter -- View this message in context: http://www.nabble.com/Getting-file-name-from-pdf-device--tp24755915p24756308.html Sent from the R help mailing list archive at Nabble.com.
Dear Rainer, You could have a look at ggsave() from the ggplot2 package. I guess that such an approach to save a plot is easier to combine with a tool like pdftk. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Rainer M Krug Verzonden: vrijdag 31 juli 2009 14:50 Aan: r-help at stat.math.ethz.ch Onderwerp: [R] Getting file name from pdf device? Hi IU would like to modify the dev.off function, so that it automatically compresses the created pdf. I am thinking of doing the following in the modified dev.off: 1) Get the filename of the pdf device 2) close the device by calling the original dev.off() 3) compress the pdf file with a system call of pdftk. My question: how can I get the filename of the pdf from the device before it is closed? Thanks Rainer -- Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch University, South Africa ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
Rainer M Krug-6 wrote:> > Hi > > IU would like to modify the dev.off function, so that it automatically > compresses the created pdf. > I am thinking of doing the following in the modified dev.off: > > 1) Get the filename of the pdf device > 2) close the device by calling the original dev.off() > 3) compress the pdf file with a system call of pdftk. > >If you are the one who initiated the pdf device shouldn't you know the name of the file? in that case just do something like: my.dev.off <- function (file, which = dev.cur()) { if (which == 1) stop("cannot shut down device 1 (the null device)") .Internal(dev.off(as.integer(which))) #system call (or something similar) system(paste('pdftk',file)) dev.cur() } fn <- 'myplot.pdf' pdf(fn) #plot stuff my.dev.off(fn) -Cameron -- View this message in context: http://www.nabble.com/Getting-file-name-from-pdf-device--tp24755915p24761759.html Sent from the R help mailing list archive at Nabble.com.
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 = "")) { dev2bitmap(file) 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: plot(sin) show() # <---- plot inserted into transcript here plot(cos) show("cos.png") # this time, a named local file instead of a temp file - Tom