From ripley at stats.ox.ac.uk  Tue Jan 10 15:26:17 2012
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 10 Jan 2012 14:26:17 +0000
Subject: [Rd] serializing recordedplot object
In-Reply-To: <7CE3B991-C200-45D5-836F-96E6DE807BCF@r-project.org>
References: <CABFfbXvzRJZ-72v6EqyvZyUi4NtNVn-BforTc-2+hP6GxU_Pag@mail.gmail.com>
	<CABFfbXu9mycTn0Ctt1Rsre7JwF5+cxjkeJeLrTOOF9Cov_H9sw@mail.gmail.com>
	<7CE3B991-C200-45D5-836F-96E6DE807BCF@r-project.org>
Message-ID: <4F0C4A89.7060103@stats.ox.ac.uk>

I don't think anyone has yet mentioned that the canonical way to save 
lattice plots is to save the object produced by lattice, and print() it 
again when required.

recordPlot was only ever intended to be a temporary expedient, as the 
help page says.

On 10/01/2012 14:17, Simon Urbanek wrote:
>
> On Jan 10, 2012, at 1:06 AM, Jeroen Ooms wrote:
>
>> The underlying problem turns out to be serialization of objects of
>> class "NativeSymbolInfo". When these are serialized and unserialized,
>> the memory address turns into a nullpointer, causing the fail. To fix
>> it, one can simply create new nativeSymbolInfo objects. E.g:
>>
>> # The old example:
>> library(lattice);
>> histogram(rnorm(100));
>> x<- recordPlot();
>> saveRDS(x, "myplot.rds");
>> y<- readRDS("myplot.rds");
>>
>> # y contains nullpointers making it fail.
>> print(y);
>>
>> # To fix:
>> for(i in 1:length(y[[1]])) {
>>   if( "NativeSymbolInfo" %in% class(y[[1]][[i]][[2]][[1]]) ){
>>     y[[1]][[i]][[2]][[1]]<- getNativeSymbolInfo(y[[1]][[i]][[2]][[1]]$name);
>>   }
>> }
>> print(y);
>>
>> Now this works, but it is a bit ugly. Is there a more general method
>> of serializing objects in a way that native objects are restored where
>> possible?
>>
>
> Unfortunately R doesn't provide a way for this. Without changes to unserialization (on the wishlist for a few years now, but not easy to design) the best you can do is to check the native symbols for NULL pointers on usage and then re-fetch - that's something that could be done reasonably easily, although it's still a hack ...
>
>
>
>>
>>
>>
>>
>>
>> On Mon, Jan 9, 2012 at 12:47 AM, Jeroen Ooms<jeroen.ooms at stat.ucla.edu>  wrote:
>>>
>>> I use recordPlot() to save plots to disk that I render later to a
>>> variety of formats. This works fine for base R plots and ggplot2
>>> plots, and also used to work for lattice plots. However somewhere in
>>> version 2.14 things stopped working for lattice plots. Here is an
>>> example:
>>>
>>> library(lattice);
>>> histogram(rnorm(100));
>>> x<- recordPlot();
>>> saveRDS(x, "myplot.rds");
>>> y<- readRDS("myplot.rds");
>>> print(y);
>>> Error: NULL value passed as symbol address
>>>
>>> printing x works fine, but printing y either gives an error or prints
>>> an empty page. The problem seems to be related to serializing the
>>> recordedplot object, which contains a lot of memory pointers. Any tips
>>> or workarounds?
>>>
>>>> sessionInfo()
>>> R version 2.14.1 (2011-12-22)
>>> Platform: i686-pc-linux-gnu (32-bit)
>>>
>>> locale:
>>> [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>>> [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>>> [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>>> [7] LC_PAPER=C                 LC_NAME=C
>>> [9] LC_ADDRESS=C               LC_TELEPHONE=C
>>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>> other attached packages:
>>> [1] lattice_0.20-0
>>>
>>> loaded via a namespace (and not attached):
>>> [1] grid_2.14.1
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] serializing recordedplot object
Prof Brian Ripley