From xie at yihui.name  Wed May 23 17:41:04 2012
From: xie at yihui.name (Yihui Xie)
Date: Wed, 23 May 2012 10:41:04 -0500
Subject: [Rd] [R] how to remove the 'promise' attribute of an R object
	(.Random.seed)?
In-Reply-To: <4FBC7DB3.4020809@stats.ox.ac.uk>
References: <CANROs4cWDYJyWCQwbWmaxg1GOFD8fuSMewZp6A6n9AUrx0qvjQ@mail.gmail.com>
	<alpine.LFD.2.02.1205221245280.1888@nokomis.stat.uiowa.edu>
	<CANROs4c=gCOt6CJwywJhuJoX3zGHSoGLZ+4eGQUrAGB47iPYjA@mail.gmail.com>
	<4FBC7DB3.4020809@stats.ox.ac.uk>
Message-ID: <CANROs4c3GxODPwuYO+Yv89jAtML95As66kFPc2PG6EoHXgHoaQ@mail.gmail.com>

OK, I switched to r-devel.

I'm caching the random seed by saving and lazy loading .Random.seed,
so I think it should always be valid (except that it becomes a promise
which is essentially an integer vector).

The complete process is as follows:

set.seed(1)  # R generates .Random.seed now
tools:::makeLazyLoadDB(globalenv(), 'random_db', variables = '.Random.seed')
lazyLoad('random_db')
rnorm(1)
# Error in rnorm(1) :
#  .Random.seed is not an integer vector but of type 'promise'

As I explained earlier, the purpose is for caching when an Sweave
document involves with RNG; instead of running set.seed() again, I
just lazy-load the seed. I'm not sure if this is a convincing enough
application. You might frown on the use of :::, though.

Thanks!

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Wed, May 23, 2012 at 1:03 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> On 22/05/2012 22:26, Yihui Xie wrote:
>>
>> I did not use delayedAssign() directly; I was using lazyLoad() and
>> .Random.seed was saved in a database. The real story out there is the
>> caching of my knitr package -- lazyLoad() was used to speed up the
>> compilation of Sweave documents.
>>
>> I know lazyLoad() is supposed to be used only by R itself, but it is
>> very helpful for the purpose of caching as well.
>>
>> Or is it possible to change the code in src/main/RNG.c so that the
>> seed is force()d if it is a promise?
>
>
> It is possible, but we need to be convinced that it is desirable. ?The main
> point is that you should not be setting .Random.seed (see its help page),
> and all the ways R itself sets it create a object of the correct internal
> type. ?The test is there because of people who failed to follow the advice
> and set it incorrectly (usually as a double vector).
>
> If you want to make a case for a change, R-devel would be the appropriate
> list and you would need to explain in detail why you thinking setting
> .Random.seed is appropriate and exactly how you do it.
>
>
>> Anyway, I can use save() and load() in this case instead of lazyLoad()
>> if the above is not going to happen. Thanks a lot!
>>
>> Regards,
>> Yihui
>> --
>> Yihui Xie<xieyihui at gmail.com>
>> Phone: 515-294-2465 Web: http://yihui.name
>> Department of Statistics, Iowa State University
>> 2215 Snedecor Hall, Ames, IA
>>
>>
>> On Tue, May 22, 2012 at 12:49 PM,<luke-tierney at uiowa.edu> ?wrote:
>>>
>>> On Tue, 22 May 2012, Yihui Xie wrote:
>>>
>>>> Hi,
>>>>
>>>> The problem arises when I lazyLoad() the .Random.seed from a
>>>> previously saved database. To simplify the process of reproducing the
>>>> problem, see the example below:
>>>>
>>>> ## this assignment may not really make sense, but illustrates the
>>>> problem
>>>> delayedAssign('.Random.seed', 1L)
>>>>
>>>> typeof(.Random.seed)
>>>> # [1] "integer"
>>>>
>>>> rnorm(1)
>>>> # Error in rnorm(1) :
>>>> # ?.Random.seed is not an integer vector but of type 'promise'
>>>>
>>>> typeof(.Random.seed)
>>>> # [1] "integer"
>>>>
>>>> So there must be an "attribute" "promise" somewhere attached to
>>>> .Random.seed, and I cannot find it. The R function typeof() does not
>>>> reveal it, but the TYPEOF() function in src/main/RNG.c says it is a
>>>> 'promise'.
>>>>
>>>> My question is, how to make R use the real value of .Random.seed
>>>> instead of complaining about the promise? Thanks!
>>>
>>>
>>>
>>> Siple answer: Don't creat the promise in the first place, i.e. don't
>>> use delayedAssign.
>>>
>>> What is the real context where this arises? Knowing that may help us
>>> decide whether the internals should address this possibility.
>>>
>>> Best,
>>>
>>> luke
>>>
>>>>
>>>> Regards,
>>>> Yihui
>>>> --
>>>> Yihui Xie<xieyihui at gmail.com>
>>>> Phone: 515-294-2465 Web: http://yihui.name
>>>> Department of Statistics, Iowa State University
>>>> 2215 Snedecor Hall, Ames, IA
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>> --
>>> Luke Tierney
>>> Chair, Statistics and Actuarial Science
>>> Ralph E. Wareham Professor of Mathematical Sciences
>>> University of Iowa ? ? ? ? ? ? ? ? ?Phone: ? ? ? ? ? ? 319-335-3386
>>> Department of Statistics and ? ? ? ?Fax: ? ? ? ? ? ? ? 319-335-3017
>>> ? Actuarial Science
>>> 241 Schaeffer Hall ? ? ? ? ? ? ? ? ?email: ? luke-tierney at uiowa.edu
>>> Iowa City, IA 52242 ? ? ? ? ? ? ? ? WWW: ?http://www.stat.uiowa.edu
>>
>>
>> ______________________________________________
>> 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.
>
>
>
> --
> 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] [R] how to remove the 'promise' attribute of an R object (.Random.seed)?
Yihui Xie