标签: reference-class

对引用类方法使用"[[]]"表示法

虽然在RI中尝试使用新的引用类,但如果对方法使用"[[]]"符号(X [["doSomething"]]而不是X $ doSomething),则会发现一些奇怪的行为.这种表示法适用于字段,但我最初认为它不适用于方法,直到我发现如果你执行"class(X $ doSomething)",然后你可以使用"[[]]".下面的简单例子说明了这一点.

setRefClass("Number", 
  fields = list(
    value = "numeric"
  ),
  methods = list(
    addOne = function() {
      value <<- value + 1
    }
  )
)

X <- new("Number", value = 1)

X[['value']]         # 1

X[["addOne"]]()      # Error: attempt to apply non-function
class(X[["addOne"]]) # NULL

class(X$addOne)      # "refMethodDef"

# Now the following works!
X[["addOne"]]()      # sets X$value = 2
class(X[["addOne"]]) # "refMethodDef"
Run Code Online (Sandbox Code Playgroud)

我遇到这个的原因是因为我想在列表中将我的对象组合在一起并创建一个"applyMethod"函数,该函数在其中的每个对象上应用指定的方法.因此,我需要将方法指定为字符串.有没有人有任何想法我怎么能做到这一点?

r reference-class

7
推荐指数
1
解决办法
757
查看次数

引用类,选项卡完成和强制方法定义

我目前正在使用引用类编写包.我通过阅读各种来源遇到了一个问题:

R参考类中的方法初始化

在Snowfall中无法可靠地使用RefClass方法

我收集是因为引用方法并非全部复制到类中的每个对象,而是在首次访问时复制它们.

https://stat.ethz.ch/pipermail/r-devel/2011-June/061261.html

作为一个例子定义:

test <- setRefClass("TEST",
                fields = list( a = "numeric"),
                methods = list(
                   addone = function(){
                                        a <<- a+1
                                      },
                   initialize = function(){
                                            a <<- 1
                                          }
                              )
               )

example <- test$new()
Run Code Online (Sandbox Code Playgroud)

所以示例是类的新对象TEST.example$在控制台中键入和标签给出

> example$
# example$.->a         example$.refClassDef example$.self        
# example$a            example$initialize 
Run Code Online (Sandbox Code Playgroud)

所以该方法addone不作为选项提供.但它可以打电话:

example$addone()
Run Code Online (Sandbox Code Playgroud)

现在标签再次显示

# > 
# > example
# Reference class object of class "TEST"
# Field "a":
# [1] 2
# > example$
# example$.->a         example$.refClassDef …
Run Code Online (Sandbox Code Playgroud)

r s4 reference-class

7
推荐指数
1
解决办法
657
查看次数

如何记录R参考类?

如何记录引用类的成员函数的使用?

如果我Rd用一个\usage块写一个文件,我该如何避免WARNING

Functions/methods with usage in documentation object 'XmlDoc' but not in code:
  $ new
Run Code Online (Sandbox Code Playgroud)

我希望这个\usage块允许我写下这样的东西:

obj <- ClassName$new(par1, par2, ...)
obj$method1(oth1, ...)
Run Code Online (Sandbox Code Playgroud)

然后我会记录\arguments块中的参数.

如果我这样做,R CMD check抱怨

Assignments in \usage in documentation object 'ClassName':
Run Code Online (Sandbox Code Playgroud)

并且不会将方法识别为我需要文档的代码对象.

截至目前,我正在编写Rd没有\usage块的文件,并在块中编写上面的代码\examples,但是我没有地方记录参数,这种方式check实际上很少检查.由于我对此不满意,我现在向社区询问当前的常见做法.

documentation r rd reference-class

6
推荐指数
2
解决办法
1475
查看次数

R参考类问题

我想在R中创建一个简单的引用类.这是我的代码(R beginner):

MyClass <- setRefClass("MyClass",
                       fields = list(a = "numeric",
                                     b = "numeric"),

                       methods = list(
                         initialize <- function(){
                           print("Initializing")
                           a <<- 1
                           b <<- 2
                         },

                         printValues <- function(){
                           print(a)
                           print(b)
                         }
                         )
                       )

a <- MyClass$new()
a$printValues()
Run Code Online (Sandbox Code Playgroud)

这会为最后一行产生以下错误:$ printValues:

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : 
  "printValues" is not a valid field or method name for reference class “MyClass”
Run Code Online (Sandbox Code Playgroud)

此外,初始化方法没有被调用?

有人能指出我的问题在哪里吗?提前谢谢了.

oop r reference-class

6
推荐指数
1
解决办法
1177
查看次数

参考类的并行计算

我有一个相当大的对象列表,我想并行应用一个复杂的函数,但我当前的方法使用了太多的内存.我认为引用类可能会有所帮助,但使用mcapply它们来修改它们似乎不起作用.

该函数修改了对象本身,因此我用新的对象覆盖原始对象.由于该对象是一个列表,我只修改了它的一小部分,我希望R的复制修改语义可以避免生成多个副本; 然而,在运行它时,似乎并不是我正在做的事情.这是我一直使用的基本R方法的一个小例子.它正确地将余额重置为零.

## make a list of accounts, each with a balance
## and a function to reset the balance
foo <- lapply(1:5, function(x) list(balance=x))
reset1 <- function(x) {x$balance <- 0; x}
foo[[4]]$balance
## 4 ## BEFORE reset
foo <- mclapply(foo, reset1)
foo[[4]]$balance
## 0 ## AFTER reset
Run Code Online (Sandbox Code Playgroud)

似乎使用引用类可能会有所帮助,因为它们是可变的,并且在使用lapply它时确实按照我的预期进行; 余额重置为零.

Account <- setRefClass("Account", fields=list(balance="numeric"),
                       methods=list(reset=function() {balance <<- 0}))

foo <- lapply(1:5, function(x) Account$new(balance=x))
foo[[4]]$balance
## 4
invisible(lapply(foo, function(x) x$reset()))
foo[[4]]$balance
## 0
Run Code Online (Sandbox Code Playgroud)

但是当我使用时mclapply,它没有正确重置.请注意,如果您使用的是Windows …

parallel-processing r reference-class

6
推荐指数
1
解决办法
326
查看次数

如何在R中创建虚拟引用类?

我在虚拟/抽象类中找不到多少help(ReferenceClasses)- 有人能提供创建一个基本的例子吗?此外,如何指定虚方法并强制子类必须实现它?

oop r class reference-class

6
推荐指数
1
解决办法
957
查看次数

相当于R中的'this'或'self'

我在R中寻找等效的python的'self'关键字或java的'this'关键字.在下面的例子中,我从一个不同的S4对象的方法制作一个S4对象,需要将指针传递给我自己.语言中是否有东西可以帮助我做到这一点?

MyPrinter <- setRefClass("MyPrinter",
  fields = list(obj= "MyObject"),
  methods = list(
    prettyPrint = function() {
      print(obj$age)
      # do more stuff
    }
  )
)

MyObject <- setRefClass("MyObject",
  fields = list(name = "character", age = "numeric"),
  methods = list(
    getPrinter = function() {
      MyPrinter$new(obj=WHAT_GOES_HERE) #<--- THIS LINE
    }
  )
)
Run Code Online (Sandbox Code Playgroud)

我可以用一个独立的方法做到这一点,但我希望在R中做一个很好的面向对象的方法来做这个操作.谢谢

oop r reference-class

6
推荐指数
1
解决办法
1944
查看次数

R引用类 - 如何确定您是否在继承方法中?

对于给定的引用类方法,如何确定它是否被继承?更一般地说,如何确定我的继承树有多远?

例如,如果我的设置是:

A <- setRefClass("A",
        methods = list(foo = function() whosMethod())
    )
B <- setRefClass("B",
        contains = "A",
        methods = list(bar = function() whosMethod())
    )
b <- B()
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想whosMethod()给我一些类似的东西

> b$foo()
[1] "A"         # or maybe a numeric value like: [1] 1L

> b$bar()
[1] "B"         # or maybe a numeric value like: [1] 0L
Run Code Online (Sandbox Code Playgroud)

请注意class(.self),这"B"与上面的例子总是会有所不同.

动机 - 自定义事件

我希望除了方法之外的其他东西也有类似继承的行为,例如自定义事件.我的方法可能raise(someEvent)并且在实例化期间我传递事件处理程序来处理那些事件,例如

MyDatabase <- setRefClass(....)
datasourceA <- MyDatabase(....,
    eventHandlers = list(
        someEvent = function() …
Run Code Online (Sandbox Code Playgroud)

oop inheritance r reference-class

5
推荐指数
1
解决办法
200
查看次数

R 包 - 我应该导入 `methods` 包吗?

setRefClass用来创建类,因为它是methods包的一部分,所以我认为您需要将此依赖项声明为import.

但是,以下最小示例Rcmd.exe checkimporting时失败methods

#' @docType package
#' @import methods
A <- setRefClass("A")
Run Code Online (Sandbox Code Playgroud)

出现以下错误(我的包被称为Test):

==> Rcmd.exe check Test_1.0.tar.gz

<Lots of checks here...>

* checking package dependencies ... ERROR
Namespace dependency not required: 'methods'

See the information on DESCRIPTION files in the chapter 'Creating R
packages' of the 'Writing R Extensions' manual.

Exited with status 1.
Run Code Online (Sandbox Code Playgroud)

因此,据我所知,似乎有人告诉我删除importformethods并因此隐藏包对methods. 我的解释是否正确,如果正确,为什么要隐藏对 的依赖methods

我的设置: …

methods r package reference-class

5
推荐指数
1
解决办法
1521
查看次数

通过RStudio访问引用类的方法时抛出的警告

代码和警告:

tinyclass <- setRefClass("TinyClass", methods = list(doNothing=function(){}))
tc <- tinyclass()
tc$doNothing()
NULL
Warning messages:
1: In installClassMethod(value, self, field, selfEnv, thisClass) :
  method .objectPackage from class TinyClass was not processed into a class method until being installed.  Possible corruption of the methods in the class.
2: In installClassMethod(value, self, field, selfEnv, thisClass) :
  method .objectParent from class TinyClass was not processed into a class method until being installed.  Possible corruption of the methods in the class.
Run Code Online (Sandbox Code Playgroud)

我也从帮助页面中提供的代码setRefClass以及我尝试制作的任何其他类中获得此代码.我在自己安装的运行R 3.2.2的Mac上可靠地得到它,并且在IT部门安装的几台运行R …

r reference-class

5
推荐指数
0
解决办法
556
查看次数