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 at gmail.com>
> Sent: Thursday, September 12, 2019 2:29 PM
> To: PIKAL Petr <petr.pikal at precheza.cz>; r-help mailing list
<r-help at 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/