When are accessor methods for S4 objects called in R?

PirateGrunt

Not quite sure how to ask this question without going into a great deal of detail about my specific programming problem. So, this question is slightly related, but much simpler. I'm trying to understand the procedure R applies when assigning to a slot of an S4 class. I have custom access and assignment functions, "$" and "$<-".

I note two things:

  1. the assignment will work when I'm using brackets to subset a specific slot, even though I've not provided any mechanism for doing so. I trust this is merely R's eager embrace of vectorized code. This is fine, but what if I want to enable different subsetting logic? In the toy example below, assume that I'd like to subset based on a match to birthdate.
  2. When assigning to a subsetted slot, the "$" accessor will be called. Why is this?

Following is a toy example for illustration. Comment on point 1 and an answer to point 2 are much appreciated.

setClass("Person"
     , representation(FirstName = "character"
                      , LastName = "character"
                      , Birthday = "Date")
)

setMethod("$", signature(x = "Person"), function(x, name) {
  print("Just called $ accessor")
  slot(x, name)
})

setMethod("$<-", signature(x = "Person"), function(x, name, value) {
 print("Just called $ assignment")
  slot(x, name) = value
  x
})

objPeople = new("Person"
                , FirstName=c("Ambrose", "Victor", "Jules")
                , LastName=c("Bierce", "Hugo", "Verne")
                , Birthday=seq(as.Date("2001/01/01"), as.Date("2003/12/31"), by="1 year"))

# This assignment will work. When assigning, there will be a call to the "$" accessor function. Why?
objPeople$FirstName[2] = "Joe"

# This assignment will not make a call to the "$" accessor function. Why?
objPeople$FirstName = "Ambroze"
MrFlick

Remember that [ is just another function, as is [<-. So in order to do

objPeople$FirstName[2] = "Joe"

The $ is going to run first and return something that the [<- can operate on. Something like

'$<-'(objPeople, "FirstName", '[<-'( '$'(objPeople, "FirstName"), 2, "Joe"))

So in order to subset, it has to extract the first name. But with

 objPeople$FirstName = "Ambroze"

That's just a

'$<-'( objPeople, "FirstName", "Ambroze")

so you don't need to call the accessor. You are just directly calling the assignment function.

If you wanted to have a custom subsetter on your class, it would be at the Person[] level. If you wanted a custom subsetter on the FirstName slot, you would have to make the FirstName slot a class of it's own where you could re-define the [ method.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在R中子集S4矩阵

来自分类Dev

R S4 roxygen2文档

来自分类Dev

R:从S4对象获取插槽的值(ScalarIndependenceTest)

来自分类Dev

从R中的S4类输出中检索值

来自分类Dev

R中的S4类的一元加法

来自分类Dev

确保R S4中的接口符合性

来自分类Dev

R S4 setMethod'['区分缺少的参数?

来自分类Dev

在Rcpp中创建R S4类的对象?

来自分类Dev

在R中创建S4对象

来自分类Dev

转换自定义S4类的R方法

来自分类Dev

R S4初始化并调用NextMethod()

来自分类Dev

从R中的S4类输出中检索值

来自分类Dev

在R中创建S4对象

来自分类Dev

R记录S4泛型不显示用法

来自分类Dev

R S4类,成员函数无效

来自分类Dev

R 中 S4 对象的并行化错误

来自分类Dev

如何编辑 R S4 Object 摘要的代码?

来自分类Dev

Entity framework, code first. Child objects not populating when called

来自分类Dev

UITextField delegate methods not called

来自分类Dev

When does the termination checker reduce a record accessor

来自分类Dev

如何在R中自动更新S4类的插槽

来自分类Dev

R:何时在命名空间中使用setGeneric或导出s4方法

来自分类Dev

如何在R中使用<-设置插槽(S4)的值

来自分类Dev

如何获得R中S4类上定义的所有方法的列表?

来自分类Dev

R:使用foreach循环时,“类型'S4'的对象不可子集化”

来自分类Dev

有关在R中使用S4方法的问题

来自分类Dev

什么时候在R中调用S4对象的访问器方法?

来自分类Dev

R match.call()用于S4方法

来自分类Dev

动态创建嵌套的s4类插槽名称并在R中分配值