Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写包R 3.0.1中的setClass和setMethod_R_S4 - Fatal编程技术网

重写包R 3.0.1中的setClass和setMethod

重写包R 3.0.1中的setClass和setMethod,r,s4,R,S4,我在重写包中的setClass和setMethod时遇到问题 要求:R 3.0.1,Rtools 请找到以下重新创建问题的步骤: 1创建类和方法 2/将文件保存在 C:\AAA.r 3/构建一个包 4/测试 5/重写类和方法 6/测试 请告知您忘了从新方法返回某些内容。你的意思是把e2作为最后一行吗?是的,我修复了这个例子-但错误仍然是相同的方法(“Arith”、…请参阅?Ops和?callgeneric如果我使用“Arith”方法,那么它会在一个函数中包含多个运算符(+“、”-“、“*”、“^

我在重写包中的setClass和setMethod时遇到问题

要求:R 3.0.1,Rtools

请找到以下重新创建问题的步骤:

1创建类和方法 2/将文件保存在 C:\AAA.r

3/构建一个包 4/测试 5/重写类和方法 6/测试
请告知

您忘了从新方法返回某些内容。你的意思是把
e2
作为最后一行吗?是的,我修复了这个例子-但错误仍然是相同的方法(“Arith”、…请参阅?Ops和?callgeneric如果我使用“Arith”方法,那么它会在一个函数中包含多个运算符(+“、”-“、“*”、“^”、“%%”、“%/%”、“/”?。我只需要该方法上的“*”运算符
setClass(Class="AAA",
           representation(
           name="character",
           val="numeric"
         )
)

setMethod("*", 
      signature(e1 = "numeric", e2 = "AAA"), 
      definition=function (e1, e2) {
          e2@val = e2@val * e1
      e2       
    }
)
>  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