Question
When programming in r with the s4 OOP system, when one have to use setReplaceMethod
? I don't see what's the difference with the setMethod
when adding <-
to the name of the function. Does setMethod("$<-")
and setReplaceMethod("$")
are equal?
使用s4 OOP系统在r中编程时,何时必须使用setReplaceMethod?在将< - 添加到函数名称时,我没有看到与setMethod的区别。 setMethod(“$ < - ”)和setReplaceMethod(“$”)是否相等?
Documentation
- I didn't find anything in the documentation with
?setReplaceMethod
or??setReplaceMethod
. There is nothing except the usage. - In *, there is several questions about setReplaceMethod but none helping. I started to search a answer to this question when I saw it seem's is not possible to use
roxygen2
to document methods created withsetReplaceMethod
- I didn't find anything by searching in r-project.org
我没有在文档中找到任何内容?setReplaceMethod或?? setReplaceMethod。除了用法之外什么都没有。
在*中,有几个关于setReplaceMethod的问题但没有帮助。当我看到它似乎无法使用roxygen2来记录使用setReplaceMethod创建的方法时,我开始搜索这个问题的答案
我在r-project.org上搜索没找到任何东西
Reproductible example
library(methods)
# Create a class
setClass("TestClass", slots = list("slot_one" = "character"))
# Test with setMethod -----------------------
setMethod(f = "$<-", signature = "TestClass",
definition = function(x, name, value) {
if (name == "slot_one") x@slot_one <- as.character(value)
else stop("There is no slot called",name)
return(x)
}
)
# [1] "$<-"
test1 <- new("TestClass")
test1$slot_one <- 1
test1
# An object of class "TestClass"
# Slot "slot_one":
# [1] "1"
# Use setReplaceMethod instead -----------------------
setReplaceMethod(f = "$", signature = "TestClass",
definition = function(x, name, value) {
if (name == "slot_one") x@slot_one <- as.character(value)
else stop("There is no slot called",name)
return(x)
}
)
# An object of class "TestClass"
# Slot "slot_one":
# [1] "1"
test2 <- new("TestClass")
test2$slot_one <- 1
test2
# [1] "$<-"
# See if identical
identical(test1, test2)
# [1] TRUE
Actual conclusion
setReplaceMethod
seems only to permit to avoid the <-
when creating a set method. Because roxygen2
can't document methods produced with, it's better for the moment to use setMethod
. Does I have right?
setReplaceMethod似乎只允许在创建set方法时避免< - 。因为roxygen2不能记录生成的方法,所以目前使用setMethod更好。我有权利吗?
1 个解决方案
#1
9
Here's the definition of setReplaceMethod
这是setReplaceMethod的定义
> setReplaceMethod
function (f, ..., where = topenv(parent.frame()))
setMethod(paste0(f, "<-"), ..., where = where)
<bytecode: 0x435e9d0>
<environment: namespace:methods>
It is pasting a "<-" on to the name, so is functionally equivalent to setMethod("$<-")
. setReplaceMethod conveys more semantic meaning.
它在名称上粘贴了一个“< - ”,因此在功能上等同于setMethod(“$ < - ”)。 setReplaceMethod传达了更多的语义含义。
#1
9
Here's the definition of setReplaceMethod
这是setReplaceMethod的定义
> setReplaceMethod
function (f, ..., where = topenv(parent.frame()))
setMethod(paste0(f, "<-"), ..., where = where)
<bytecode: 0x435e9d0>
<environment: namespace:methods>
It is pasting a "<-" on to the name, so is functionally equivalent to setMethod("$<-")
. setReplaceMethod conveys more semantic meaning.
它在名称上粘贴了一个“< - ”,因此在功能上等同于setMethod(“$ < - ”)。 setReplaceMethod传达了更多的语义含义。