Is it possible to call or set values for more then one slot?
A<-setClass(Class="A",slot=c(name="character",type="character"))
a<-A()
slot(object,c("name","type"),check=T)
Do I have to write own getSlot and setSlot methods? And how to that in R5?
AB <- setRefClass("AB", fields=c(name="character"),
methods=list(getName=AB.getName)
)
AB.getName<-function(object){
object$name
}
a<-AB(name="abc")
AB.getName(a)
This answer applies to reference classes.
Let's start with the simplest definition of
AB
, without any methods.You can retrieve the value of the
name
field in the same way you would a list.It is possible to autogenerate accessor methods to get and set the name field.
This is really pointless though since it has the exactly same behaviour as before, but with more typing. What can be useful is to do input checking (or other custom behaviour) when you set a field. To do this, you can add a
setName
method that you write yourself.It is also possible (and usually more useful) to define this method when you assign the reference class template.
Response to comment:
Yes that works, but:
getFieldNames
is more maintainable if implemented asnames(AB$fields())
.When defining fields in
setRefClass
, use a list. For example,list(name="character", var2="character")
.When assigning an instance of a reference class, use
new
. For example,AB$new(name="abc",var2="abc")