我在覆盖包
中的setClass和setMethod时遇到问题要求:R 3.0.1,Rtools
请在下面找到重新创建问题的步骤:
setClass(Class="AAA",
representation(
name="character",
val="numeric"
)
)
setMethod("*",
signature(e1 = "numeric", e2 = "AAA"),
definition=function (e1, e2) {
e2@val = e2@val * e1
e2
}
)
C:\ AAA.r
> setwd("C:")
> package.skeleton(name="AAA",code_files="C:\\AAA.r")
> system("R CMD build AAA")
> system("R CMD INSTALL --build AAA")
> 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)
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
}
e2
}
)
> y = new("AAA")
> y@val=25
> y@type="double"
> -1 * y
Error in -1 * y : invalid object (non-function) used as method
请告知