optional setValidity()

View: New views
3 Messages — Rating Filter:   Alert me  

optional setValidity()

by Robin Hankin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi


Suppose I have an S4 class "foo" and a validity checking
function  ".checkfoo()":

setClass("foo",  representation=representation("numeric"))
setValidity("foo" , .checkfoo)

is fine; in my application, .checkfoo() verifies that a bunch
of necessary conditions are met.

But .checkfoo() is very time consuming and I want
to give users the option of switching it off.

Most foo objects that one deals with fall into two or three standard  
types
and in these cases one doesn't need to execute  .checkfoo()
because one can show algebraically that the conditions are
automatically met.

But OTOH, I want the check to be performed "by default" to
stop anyone (me) from being too clever and defining
a non-standard foo object that doesn't meet .checkfoo().

What is best practice here?

Are there any examples I could copy?





--
Robin Hankin
Uncertainty Analyst and Neutral Theorist,
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: optional setValidity()

by Sklyar, Oleg (MI London) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You could add a flag to your class and check if it set as a first thing
in the validity as in the example below:

setClass("foo",
  representation("numeric", .validate="logical"),
  prototype(.validate=TRUE),
  validity=function(object) {
    if (!object@.validate) TRUE
    ## do lengthy checks
  }
)

setGeneric("foo", function(x, ...) standardGeneric("foo"))
setMethod("foo", signature(x="numeric"),
  function(x, ..., validate=TRUE) new("foo", ..., .validate=validate)
)

a = foo(runif(10), validate=FALSE)
## or
b = new("foo", runif(10), .validate=FALSE)
## or do validate
d = foo(runif(10))
e = new("foo", runif(10))

The downside is that you carry unnecessary information around in your
objects.

Best,

Dr Oleg Sklyar
Technology Group
Man Investments Ltd
+44 (0)20 7144 3803
osklyar@...

> -----Original Message-----
> From: r-devel-bounces@...
> [mailto:r-devel-bounces@...] On Behalf Of Robin Hankin
> Sent: 07 May 2008 15:44
> To: R-devel@...
> Subject: [Rd] optional setValidity()
>
> Hi
>
>
> Suppose I have an S4 class "foo" and a validity checking
> function  ".checkfoo()":
>
> setClass("foo",  representation=representation("numeric"))
> setValidity("foo" , .checkfoo)
>
> is fine; in my application, .checkfoo() verifies that a bunch
> of necessary conditions are met.
>
> But .checkfoo() is very time consuming and I want to give
> users the option of switching it off.
>
> Most foo objects that one deals with fall into two or three
> standard types and in these cases one doesn't need to execute
>  .checkfoo() because one can show algebraically that the
> conditions are automatically met.
>
> But OTOH, I want the check to be performed "by default" to
> stop anyone (me) from being too clever and defining a
> non-standard foo object that doesn't meet .checkfoo().
>
> What is best practice here?
>
> Are there any examples I could copy?
>
>
>
>
>
> --
> Robin Hankin
> Uncertainty Analyst and Neutral Theorist, National
> Oceanography Centre, Southampton European Way, Southampton
> SO14 3ZH, UK
>   tel  023-8059-7743
>
> ______________________________________________
> R-devel@... mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>


**********************************************************************
The contents of this email are for the named addressee(s...{{dropped:22}}

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: optional setValidity()

by Sklyar, Oleg (MI London) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

sorry I forgot the return statement, it should be
if (!object@.validate) return(TRUE)

Dr Oleg Sklyar
Technology Group
Man Investments Ltd
+44 (0)20 7144 3803
osklyar@...

> -----Original Message-----
> From: r-devel-bounces@...
> [mailto:r-devel-bounces@...] On Behalf Of Sklyar,
> Oleg (MI London)
> Sent: 07 May 2008 16:13
> To: Robin Hankin; R-devel@...
> Subject: Re: [Rd] optional setValidity()
>
> You could add a flag to your class and check if it set as a
> first thing in the validity as in the example below:
>
> setClass("foo",
>   representation("numeric", .validate="logical"),
>   prototype(.validate=TRUE),
>   validity=function(object) {
>     if (!object@.validate) TRUE
>     ## do lengthy checks
>   }
> )
>
> setGeneric("foo", function(x, ...) standardGeneric("foo"))
> setMethod("foo", signature(x="numeric"),
>   function(x, ..., validate=TRUE) new("foo", ..., .validate=validate)
> )
>
> a = foo(runif(10), validate=FALSE)
> ## or
> b = new("foo", runif(10), .validate=FALSE) ## or do validate
> d = foo(runif(10)) e = new("foo", runif(10))
>
> The downside is that you carry unnecessary information around
> in your objects.
>
> Best,
>
> Dr Oleg Sklyar
> Technology Group
> Man Investments Ltd
> +44 (0)20 7144 3803
> osklyar@...
>
> > -----Original Message-----
> > From: r-devel-bounces@...
> > [mailto:r-devel-bounces@...] On Behalf Of Robin Hankin
> > Sent: 07 May 2008 15:44
> > To: R-devel@...
> > Subject: [Rd] optional setValidity()
> >
> > Hi
> >
> >
> > Suppose I have an S4 class "foo" and a validity checking function  
> > ".checkfoo()":
> >
> > setClass("foo",  representation=representation("numeric"))
> > setValidity("foo" , .checkfoo)
> >
> > is fine; in my application, .checkfoo() verifies that a bunch of
> > necessary conditions are met.
> >
> > But .checkfoo() is very time consuming and I want to give users the
> > option of switching it off.
> >
> > Most foo objects that one deals with fall into two or three
> standard
> > types and in these cases one doesn't need to execute
> >  .checkfoo() because one can show algebraically that the conditions
> > are automatically met.
> >
> > But OTOH, I want the check to be performed "by default" to
> stop anyone
> > (me) from being too clever and defining a non-standard foo
> object that
> > doesn't meet .checkfoo().
> >
> > What is best practice here?
> >
> > Are there any examples I could copy?
> >
> >
> >
> >
> >
> > --
> > Robin Hankin
> > Uncertainty Analyst and Neutral Theorist, National Oceanography
> > Centre, Southampton European Way, Southampton
> > SO14 3ZH, UK
> >   tel  023-8059-7743
> >
> > ______________________________________________
> > R-devel@... mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>
> **********************************************************************
> The contents of this email are for the named
> addressee(s...{{dropped:22}}
>
> ______________________________________________
> R-devel@... mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>


**********************************************************************
The contents of this email are for the named addressee(s...{{dropped:22}}

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel