[R] test if something was plotted on pdf device

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Fri Sep 13 12:13:08 CEST 2019


Dear Duncan

Thank you for the code, I will test it or at least check what it does. I finally found probably easier solution.

I stay with my original code

if (dev.cur()==1) plot(ecdf(velik[,"ecd"]), main = ufil[j], col=i) else
plot(ecdf(velik[,"ecd"]), add=T, col=i)

After plot is finished and cycle ends, I copy result to pdf device

dev.copy(pdf,paste(gsub(".xls", "", ufil)[j], ".pdf", sep=""))
dev.off()

Using this approach I could stay with my original code (almost), check if plot was initialised by dev.cur() and save it after it is finished to pdf.

The only obstacle is that my code flashes during plotting to basic device, however I can live with it.

Thank you again and best regards

Petr

> -----Original Message-----
> From: Duncan Murdoch <murdoch.duncan using gmail.com>
> Sent: Thursday, September 12, 2019 2:29 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>; r-help mailing list <r-help using r-
> project.org>
> Subject: Re: [R] test if something was plotted on pdf device
>
> On 12/09/2019 7:10 a.m., PIKAL Petr wrote:
> > Dear all
> >
> > Is there any simple way checking whether after calling pdf device
> something was plotted into it?
> >
> > In interactive session I used
> >
> > if (dev.cur()==1) plot(ecdf(rnorm(100))) else plot(ecdf(rnorm(100)),
> > add=T, col=i) which enabled me to test if plot is open
> >
> > But when I want to call eg. pdf("test.pdf") before cycle
> > dev.cur()==1 is FALSE even when no plot is drawn and plot.new error
> comes.
> >
> >> pdf("test.pdf")
> >
> > if (dev.cur()==1) plot(ecdf(rnorm(100))) else plot(ecdf(rnorm(100)),
> > add=T, col=i)
> >
> > Error in segments(ti.l, y, ti.r, y, col = col.hor, lty = lty, lwd = lwd,  :
> >    plot.new has not been called yet
> >
>
> I don't know if this is reliable or not, but you could use code like this:
>
>    f <- tempfile()
>    pdf(f)
>    blankPlot <- recordPlot()
>    dev.off()
>    unlink(f)
>
>    pdf("test.pdf")
>
>    ...  unknown operations ...
>
>    if (dev.cur() == 1 || identical(recordPlot(), blankPlot))
>      plot(ecdf(rnorm(100)))
>    else
>      plot(ecdf(rnorm(100)), add=TRUE, col=i)
>
>
>
> Duncan Murdoch
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/



More information about the R-help mailing list