[Rd] Error condition in evaluating a promise

Martin Morgan mtmorgan at fhcrc.org
Wed Oct 18 05:43:52 CEST 2006


I believe the technique is to create an environment in the namespace,
and then to access that through functions:

sandbox/NAMESPACE
export(getx, setx)

sandbox/R/sandbox.R:

sandbox <- new.env(parent=emptyenv())
getx <- function() sandbox$x
setx <- function(val) sandbox$x <- val

> library(sandbox)
> environmentIsLocked(getNamespace("sandbox"))
[1] "TRUE"
> getx()
NULL
> setx(2)
> getx()
[1] 2

or otherwise:

> sbox <- sandbox:::sandbox
> sbox$x <- 3
> getx()
[1] 3
> sbox$y <- 4
> sbox$y
[1] 4

I think this works with delayedAssign.

> delayedAssign("z", { cat("hi\n"); 2}, assign.env=sbox)
> sbox$z
hi
[1] 2
> sbox$z
[1] 2

Martin


Martin Morgan <mtmorgan at fhcrc.org> writes:

>> delayMe <- function() {
> +     if (failed) {
> +         delayedAssign("x", delayMe(), assign.env=topenv())
> +         stop("init me!")
> +     } else foo
> + }
>> 
>> failed <- TRUE
>> foo <- "is me"
>> delayedAssign("x", delayMe())
>> x
> Error in delayMe() : init me!
>> x
> Error in delayMe() : init me!
>> failed <- FALSE
>> x
> [1] "is me"
>
> ??
>
> Martin
>
> Simon Urbanek <simon.urbanek at r-project.org> writes:
>
>> Is there a way to raise an error condition when a promise is  
>> evaluated such that is can be evaluated again? Right now strange  
>> things happen when the evaluation fails:
>>
>>  > delayedAssign("x", if (failed) stop("you have to initialize me  
>> first!") else foo)
>>  > foo <- "I'm foo"
>>  > failed<-TRUE
>>  > x
>> Error: you have to initialize me first!
>>  > x
>> Error: recursive default argument reference
>>
>> ^^-- from now on x is completely unusable - it has no value (i.e.  
>> cannot be passed to any function) and yet won't be evaluated again
>>
>>  > failed<-FALSE
>>  > x
>> Error: recursive default argument reference
>>  > delayedAssign("x", if (failed) stop("you have to initialize me  
>> first!") else foo)
>>  > x
>> [1] "I'm foo"
>>
>> I'd expect something like
>>  > failed<-TRUE
>>  > x
>> Error: you have to initialize me first!
>>  > x
>> Error: you have to initialize me first!
>>  > failed<-FALSE
>>  > x
>> [1] "I'm foo"
>>
>> Is there a way to achieve that? Intuitively I'd think that this is  
>> the desired behavior, because currently the promise is sort of  
>> 'broken' after an error (AFAICT the behavior is not documented  
>> anywhere) - but then, I wasn't messing with promises until now...
>>
>> Thanks,
>> Simon
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> -- 
> Martin T. Morgan
> Bioconductor / Computational Biology
> http://bioconductor.org
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin T. Morgan
Bioconductor / Computational Biology
http://bioconductor.org




More information about the R-devel mailing list