swh:1:snp:a4c99a50dc49f82b591f268001b320f8c3ca0041
Raw File
Tip revision: dc000f2a5f006d137f66716b086025d618bf8306 authored by John M Chambers on 14 July 2008, 00:00:00 UTC
version 1.0-5
Tip revision: dc000f2
scaleVisual.R
## Definitions for a class union, "scaleVisual" to be used in
## generating  data visualizations suitable for a positive scale
## variable


## Two obvious scale variables are  ordered factors & positive numbers

setClass("numericScale", contains = "numeric",
         validity = function(object)identical(all(object>0), TRUE)
         )

setClassUnion("scaleVisual", c("numericScale", "ordered"))

# any class extending scaleVisual must provide a method
# to generate numberical values for display

setGeneric("scaleAsNumeric",
           function(object) standardGeneric("scaleAsNumeric"),
           valueClass = "numeric")

requireMethods("scaleAsNumeric", "scaleVisual")

setMethod("scaleAsNumeric", "numericScale",
          function(object)object
          )

setMethod("scaleAsNumeric", "ordered",
          ## use the levels if they are positive ordered numbers,
          ## otherwise just treat the scale as seq on levels
          function(object) {
             numLevs <- scan(textConnection(levels(object)))
             if(is.numeric(numericLevs) &&
                identical(all(numericLevs > 0), TRUE) &&
                identical(all(diff(numericLevs)>0), TRUE))
               numericLevs
             else seq(along = numericLevs)
           }
          )

## now an application of setIs with a test.

## class "numericVisual" is just an extended "numeric" for which we
## define dispaly methods.  It's needed because the setIs below
## is not allowed for "numeric", for the (good) reason that that class
## is sealed, and can only be extended as a member of a classUnion

setClass("numericVisual", contains="numeric")

setIs("numericVisual", "numericScale",
      test = function(object)
        identical(all(object > 0), TRUE)
 # needed?     , coerce = function(object)new("numericScale", object)
      )

## examples
back to top