[R] how to create a new data type

cls59 chuck at sharpsteen.net
Thu Oct 22 16:42:33 CEST 2009




Markus Weisner-2 wrote:
> 
> I am working on a new package to do fire department analysis.  I am
> working
> with emergency dispatch data from different agencies that all contain the
> same information but have slightly different formats.  Typically the
> variable names and date-time formats are different.  I am getting pretty
> good at reading in and rearranging the data so that it is essentially in
> the
> same format, but it is common for me to have an occasional wrong variable
> name or data type.  These slight deviations wreak havoc on all my
> functions
> that are setup to automatically analyze the data (and require specific
> variable names and data types).
> 
> I would like to create a new data type that has defined variable names and
> types (where I would be forced to have the data in the correct format). 
> If
> I had my own unique data type, each of my analysis functions could check
> to
> make sure that provided data has the correct data type ... thus
> eliminating
> all these little debugging operations I have to keep doing to get the
> functions to work right.
> 
> Any suggestions on how to do something like this?  I have done some
> research
> online, but could not find any simple explanations of creating user
> defined
> object types.  Thanks in advance for your help.
> 
> Best,
> Markus
> 
> 

Well, you can get this functionality buy defining an S4 class:

  require( methods )
  setClass( 'myClass', representation = representation( from = 'character',
to = 'character', 
    number = 'numeric' ),
    prototype = prototype( from = character(), to = character(), number = 0
),
    validity = function( object ){
     
       if( object at number < 0 ){
         
          return("The number must be positive!")

       }

        return( TRUE )

    }
  )

That will define a class containing three components-- two character vectors
named "from" and "to" and a numeric vector named "number". You can give the
names of any class, such as 'list' or 'data.frame', as components of a S4
class. The prototype specifies that each value will default to a vector of
length 0 except in the case of the number which defaults to 0. The validity
is an optional custom function that performs specific checks on the class
members-- in this case it ensures that the value given for the number is
non-negative. Returning anything but TRUE from the validity function will be
interpreted as an error.

New objects of a given class are created using new():

  myObject <- new( 'MyClass', from = 'foo', to = 'bar', number = 3 )

Components, known as "slots", are accessed using the @ operator in much the
same way that list items are accessed using $:

  myObject at number

[1] 3


I have to run, but for more info you check the documentation of the methods
package and the R wiki page on S4 has some pointers to other references.

Hope this helps!

-Charlie



-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/how-to-create-a-new-data-type-tp26010194p26011194.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list