From maechler at stat.math.ethz.ch  Tue Apr  3 16:07:01 2018
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Tue, 3 Apr 2018 16:07:01 +0200
Subject: [Rd] [FORGED] recordPlot/replayPlot not working with
 saveRDS/readRDS
In-Reply-To: <92ecddb0-a17f-080c-ecaa-5702342f9590@stat.auckland.ac.nz>
References: <CAFOpNVE7oyo1P_JUeQbXcO_YbabXRcMoogeV=3ULesoX5BdfdQ@mail.gmail.com>
 <92ecddb0-a17f-080c-ecaa-5702342f9590@stat.auckland.ac.nz>
Message-ID: <23235.35461.48296.589585@stat.math.ethz.ch>

>>>>> Paul Murrell <paul at stat.auckland.ac.nz>
>>>>>     on Tue, 3 Apr 2018 09:41:56 +1200 writes:

    > Hi What you are doing "wrong" is loading a recordedplot
    > into the same session that it was created in.  The
    > saveRDS()/readRDS() works if you save in one R session and
    > then read in a different R session.  The assumption is
    > that if you are still in the same session that created the
    > recordedplot you still have the recordedplot (e.g., you
    > can just do replayPlot(r) instead of replayPlot(r2).  Is
    > that a bad assumption ?

Not "bad", really,  but somewhat *un*intuitive I believe to most
users, including me.
I would have done the same as Winston to check if saving and
loading of a recorded plot work fine.

Also, the error message we get is not very helpful:

    > saveRDS(r, 'recordedplot.rds')
    > r <- readRDS('recordedplot.rds')
    > png('test2.png')
    > replayPlot(r)
    Error: NULL value passed as symbol address

{coming from deep down: checkValidSymbolId() in src/main/dotcode.c }

I've pondered a bit and in the end found it easy enough to
tryCatch() the error, and "do the right thing" instead.
As that should typically only happen in exactly such a use case,
the change should be "uniformly >=" the previous state.

Ideally, maybe we (R core) would provide an R level API to a
"TRUE/FALSE" valued version of checkValidSymbolId() or its
caller, so that *from R code* one could check if the
recordedplot 'x' passed to replayPlot() with its external
pointers needs to be fixed up by
     x <- restoreRecordedPlot(x)
or not.

But that would be another topic I'd not get envolved deeply just now.

Martin

> Paul
> 
> On 03/04/18 05:23, Winston Chang wrote:
> > The documentation for recordPlot says the following:
> > 
> >> As of R 3.3.0, it is possible (again) to replay a plot from another R session using, for example, saveRDS and readRDS.
> > 
> > However, I haven't been able to save and restore a plot displaylist
> > and have it work within the same R session, using R 3.4.3 or 3.3.3.
> > Here's an example:
> > 
> > # Save displaylist for a simple plot
> > png('test.png')
> > dev.control(displaylist ="enable")
> > plot(1:5, 1:5)
> > r <- recordPlot()
> > dev.off()
> > 
> > # Replay plot. This works.
> > png('test1.png')
> > replayPlot(r)
> > dev.off()
> > 
> > #  Save the plot and load it, then try to replay it. This does not work.
> > saveRDS(r, 'recordedplot.rds')
> > r2 <- readRDS('recordedplot.rds')
> > png('test2.png')
> > replayPlot(r2)  ## Error: NULL value passed as symbol address
> > dev.off()
> > 
> > 
> > Is there something that I'm doing wrong here?
> > 
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> -- 
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland



[Rd] [FORGED] recordPlot/replayPlot not working with saveRDS/readRDS
Martin Maechler