commit 1b1a8daf7393c0d6baa7853fd5a270ebf2f5ec85 Author: Eric Coissac Date: Wed Jan 13 09:55:08 2016 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81b7f76 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +/man/ diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..655d63e --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,13 @@ +Package: ROBIUtils +Type: Package +Title: What the package does (short line) +Version: 1.0 +Date: 2014-11-08 +Author: Who wrote it +Maintainer: Who to complain to +Description: More about what it does (maybe more than one line) +License: What license is it under? +Collate: + 'ROBIUtils.R' + 'classS3.R' +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..a0ad5d2 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,5 @@ +# Generated by roxygen2: do not edit by hand + +export(addS3Class) +export(createS3Class) +export(rmS3Class) diff --git a/R/ROBIUtils.R b/R/ROBIUtils.R new file mode 100644 index 0000000..e69de29 diff --git a/R/classS3.R b/R/classS3.R new file mode 100644 index 0000000..0ab16f2 --- /dev/null +++ b/R/classS3.R @@ -0,0 +1,125 @@ +#' @include ROBIUtils.R +NULL + +#' Adds a class into the class hierarchie attribute. +#' +#' \code{addS3Class} adds a new class name to the vector +#' of class associated to the object. This the way to +#' assign an object to an S3 class. \code{addS3Class} add +#' the new class name in front of the class vector +#' +#' @param object the object to modify +#' @param classname the name of the new class +#' +#' @return the object given as parametter casted to the new +#' class +#' +#' @examples +#' x = c(1,3,2,5) +#' x = addS3Class(x,"my.vector") +#' class(x) +#' +#' @seealso \code{\link{rmS3Class}} +#' +#' @note for efficiency purpose no check is done on the input +#' parametters +#' +#' @keywords system function +#' +#' @author Eric Coissac +#' @export +#' +addS3Class = function(object,classname) { + class(object) = c(classname,class(object)) + return(object) +} + +#' Removes a class from the class hierarchie attribute. +#' +#' \code{rmS3Class} removes a class name from the vector +#' of class associated to the object. This the way to +#' remove the association between an object and a S3 class. +#' +#' @param object the object to modify +#' @param classname the name of the class to remove +#' +#' @return the object given as parametter. +#' +#' @examples +#' x = c(1,3,2,5) +#' x = addS3Class(x,"my.vector") +#' class(x) +#' x = rmS3Class(x,"my.vector") +#' class(x) +#' +#' @seealso \code{\link{addS3Class}} +#' +#' @note for efficiency purpose no check is done on the input +#' parametters +#' +#' @keywords system function +#' +#' @author Eric Coissac +#' @export +#' +rmS3Class = function(object,classname) { + c = class(object) + if (! is.null(c)) + index = match(classname,c) + class(object)=c[-index] + return(object) +} + +#' create basic functions to manipulate a new S3 class +#' +#' createS3Class function create in the \code{package:ROBITools} +#' environment an \code{is.xxx} function and an \code{as.xxx} function +#' allowing to test if an abject belong the class \code{xxx} and to add +#' the class \code{xxx} to the class list of an object. \code{xxx} is a +#' generic class name that is specified through the \code{classname} +#' argument of the function. +#' +#' @param classname a \code{character string} indicating the name +#' of the new class. +#' +#' @examples +#' +#' # Create a new S3 class named mynewclass +#' createS3Class('mynewclass') +#' +#' #create a new vector object +#' x=c(1,4,6) +#' +#' # test if it belongs the new class, that is false +#' is.mynewclass(x) +#' +#' # Associate x to the new class +#' as.mynewclass(x) +#' +#' # test again if x belongs the new class, that is now true +#' is.mynewclass(x) +#' +#' @seealso \code{\link{rmS3Class}} +#' +#' @note Take care that the new functions are created in the +#' \code{package:ROBITools} environment. +#' +#' @keywords system function +#' +#' @author Eric Coissac +#' @export +#' +createS3Class = function(classname,envir=NULL) { + if (is.null(envir)) + envir=parent.frame() + + is.class = function(object) any(class(object)==classname) + as.class = function(object) return(addS3Class(object,classname)) + + assign(paste('is',classname,sep="."),is.class,envir=envir) + assign(paste('as',classname,sep="."),as.class,envir=envir) + +} + + + diff --git a/ROBIUtils.Rproj b/ROBIUtils.Rproj new file mode 100644 index 0000000..9f96499 --- /dev/null +++ b/ROBIUtils.Rproj @@ -0,0 +1,18 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: knitr +LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace