Andy Pranata
2013-Jul-11 05:55 UTC
[R] Override setClass and setMethod in a package R 3.0.1
Hi,
I am having problem to override setClass and setMethod in a package
Requirement: R 3.0.1, Rtools
Please find below the steps to recreate the problem:
*
*
*#1. Create class and method*
*setClass(Class="AAA",*
* representation(*
* name="character",*
* val="numeric"*
* )*
*)*
*setMethod("*", *
* signature(e1 = "numeric", e2 = "AAA"), *
* definition=function (e1, e2) {*
* e2@val = e2@val * e1*
* e2 *
* }*
*)*
*# 2/ save the file in *
C:\\AAA.r
*# 3/ build a package*
*> setwd("C:")*
*> package.skeleton(name="AAA",code_files="C:\\AAA.r")
*
*> system("R CMD build AAA")*
*> system("R CMD INSTALL --build AAA")*
*#4/ testing*
*> library(AAA)*
*> x = new("AAA")*
*> x@val = 100*
*> -1 * x*
*An object of class "AAA"*
*Slot "name":*
*character(0)*
*
*
*Slot "val":*
*[1] -100*
*
*
*Slot "type":*
*character(0)*
*#5/ override class and method*
*setClass(Class="AAA",*
* representation(*
* name="character",*
* val="numeric",*
* type="character",*
* desc="character"*
* )*
*)*
*
*
*setMethod("*",
*
* signature(e1 = "numeric", e2 = "AAA"), *
* definition=function (e1, e2) {*
* if (e2@type == "double"){*
* e2@val = e2@val * (2 * e1) *
* } else {*
* e2@val = e2@val * e1 *
* }*
* }*
* )*
*
*
*#6/ testing*
*> y = new("AAA")*
*> y@val=25*
*> y@type="double"*
*> -1 * y*
*Error in -1 * y : invalid object (non-function) used as method*
Please advise
--
-- Andy --
[[alternative HTML version deleted]]