R: Change the fields' (slots') values of the class assigning a value to some other field
Posted
by
Max Li
on Stack Overflow
See other posts from Stack Overflow
or by Max Li
Published on 2012-07-05T21:58:21Z
Indexed on
2012/07/09
21:15 UTC
Read the original article
Hit count: 159
r
Assgning a value to one field, how can I make the other fields changing.
Consider the following R5 class
C<-setRefClass("C",
fields=list(a="numeric",b="numeric")
, methods=list(
seta = function(x){
a<<-x
b<<-x+10
cat("The change took place!")
}
) # end of the methods list
) # end of the class
Now create the instance of the class
c<-C$new()
This command
c$seta(10)
will result in that c$a is 10 and c$b is 20.
So it actually works, however, I want to achieve this result by the command
c$a<-10
(i.e. after that I want c$b to be equal to 20 as defined in the class in the logic of seta() function)
How can I do it?
© Stack Overflow or respective owner