An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131109/e15a4377/attachment.pl>
S4; Setter function is not chaning slot value as expected
8 messages · Simon Zehnder, Hadley Wickham, Martin Morgan +1 more
If you want to set a slot you have to refer to it: ac at CustomerID <- ?54321? or you use your setter: ac <- CustomerID(ac, ?54321?) What you did was creating a new symbol CustomerID referring to the String ?54321? CustomerID <- ?54321? CustomerID [1] ?54321? Best Simon
On 09 Nov 2013, at 15:31, daniel schnaider <dschnaider at gmail.com> wrote:
It is my first time programming with S4 and I can't get the setter fuction
to actually change the value of the slot created by the constructor.
I guess it has to do with local copy, global copy, etc. of the variable -
but, I could't find anything relevant in documentation.
Tried to copy examples from the internet, but they had the same problem.
# The code
setClass ("Account" ,
representation (
customer_id = "character",
transactions = "matrix")
)
Account <- function(id, t) {
new("Account", customer_id = id, transactions = t)
}
setGeneric ("CustomerID<-", function(obj,
id){standardGeneric("CustomerID<-")})
setReplaceMethod("CustomerID", "Account", function(obj, id){
obj at customer_id <- id
obj
})
ac <- Account("12345", matrix(c(1,2,3,4,5,6), ncol=2))
ac
CustomerID <- "54321"
ac
#Output
ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
# CustomerID is value has changed to 54321, but as you can see it does't
CustomerID <- "54321" ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Help!
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 11/09/2013 06:31 AM, daniel schnaider wrote:
It is my first time programming with S4 and I can't get the setter fuction
to actually change the value of the slot created by the constructor.
I guess it has to do with local copy, global copy, etc. of the variable -
but, I could't find anything relevant in documentation.
Tried to copy examples from the internet, but they had the same problem.
# The code
setClass ("Account" ,
representation (
customer_id = "character",
transactions = "matrix")
)
Account <- function(id, t) {
new("Account", customer_id = id, transactions = t)
}
setGeneric ("CustomerID<-", function(obj,
id){standardGeneric("CustomerID<-")})
Replacement methods (in R in general) require that the final argument (the
replacement value) be named 'value', so
setGeneric("CustomerID<-",
function(x, ..., value) standardGeneric("CustomerID"))
setReplaceMethod("CustomerID", c("Account", "character"),
function(x, ...., value)
{
x at customer_id <- value
x
})
use this as
CustomerID(ac) <- "54321"
setReplaceMethod("CustomerID", "Account", function(obj, id){
obj at customer_id <- id
obj
})
ac <- Account("12345", matrix(c(1,2,3,4,5,6), ncol=2))
ac
CustomerID <- "54321"
ac
#Output
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
# CustomerID is value has changed to 54321, but as you can see it does't
> CustomerID <- "54321"
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Help!
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
Modelling a mutable entity, i.e. an account, is really a perfect example of when to use reference classes. You might find the examples on http://adv-r.had.co.nz/OO-essentials.html give you a better feel for the strengths and weaknesses of R's different OO systems. Hadley
On Sat, Nov 9, 2013 at 9:31 AM, daniel schnaider <dschnaider at gmail.com> wrote:
It is my first time programming with S4 and I can't get the setter fuction
to actually change the value of the slot created by the constructor.
I guess it has to do with local copy, global copy, etc. of the variable -
but, I could't find anything relevant in documentation.
Tried to copy examples from the internet, but they had the same problem.
# The code
setClass ("Account" ,
representation (
customer_id = "character",
transactions = "matrix")
)
Account <- function(id, t) {
new("Account", customer_id = id, transactions = t)
}
setGeneric ("CustomerID<-", function(obj,
id){standardGeneric("CustomerID<-")})
setReplaceMethod("CustomerID", "Account", function(obj, id){
obj at customer_id <- id
obj
})
ac <- Account("12345", matrix(c(1,2,3,4,5,6), ncol=2))
ac
CustomerID <- "54321"
ac
#Output
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
# CustomerID is value has changed to 54321, but as you can see it does't
> CustomerID <- "54321"
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Help!
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Chief Scientist, RStudio http://had.co.nz/
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131110/cfed9da7/attachment.pl>
On 11/10/2013 03:54 AM, daniel schnaider wrote:
Thanks Martin. It worked well. Two new questions related to the same subject. 1) Why create this semantic of a final argument name specifically names value?
I do not know. It is a requirement of replacement methods in R in general, not
just S4 methods. See section 3.4.4 of RShowDoc("R-lang").
2) Regarding performance. When CustomerID(ac) <- "54321" runs, does it only change the slot from whatever it was to 54321, or it really create another object and change all the value of all slots, keeping technically all the other values equal and changing 54321?
Copying is tricky in R. It behaves as though a copy has been made of the entire object. Whether a copy is actually made, or just marked as necessary on subsequent modification, requires deep consideration of the code. This is the way R works, not just the way S4 classes work. If instead of a single account you modelled 'Accounts', i.e., all accounts, then updating 1000 account id's would only make one copy, whereas if you model each account separately this would require 1000 copies. Martin
thanks..
On Sat, Nov 9, 2013 at 4:20 PM, Martin Morgan <mtmorgan at fhcrc.org
<mailto:mtmorgan at fhcrc.org>> wrote:
On 11/09/2013 06:31 AM, daniel schnaider wrote:
It is my first time programming with S4 and I can't get the setter fuction
to actually change the value of the slot created by the constructor.
I guess it has to do with local copy, global copy, etc. of the variable -
but, I could't find anything relevant in documentation.
Tried to copy examples from the internet, but they had the same problem.
# The code
setClass ("Account" ,
representation (
customer_id = "character",
transactions = "matrix")
)
Account <- function(id, t) {
new("Account", customer_id = id, transactions = t)
}
setGeneric ("CustomerID<-", function(obj,
id){standardGeneric("__CustomerID<-")})
Replacement methods (in R in general) require that the final argument (the
replacement value) be named 'value', so
setGeneric("CustomerID<-",
function(x, ..., value) standardGeneric("CustomerID"))
setReplaceMethod("CustomerID", c("Account", "character"),
function(x, ...., value)
{
x at customer_id <- value
x
})
use this as
CustomerID(ac) <- "54321"
setReplaceMethod("CustomerID", "Account", function(obj, id){
obj at customer_id <- id
obj
})
ac <- Account("12345", matrix(c(1,2,3,4,5,6), ncol=2))
ac
CustomerID <- "54321"
ac
#Output
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
# CustomerID is value has changed to 54321, but as you can see it does't
> CustomerID <- "54321"
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Help!
[[alternative HTML version deleted]]
________________________________________________
R-help at r-project.org <mailto:R-help at r-project.org> mailing list
https://stat.ethz.ch/mailman/__listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>
PLEASE do read the posting guide
http://www.R-project.org/__posting-guide.html
<http://www.R-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.
--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M1 B861
Phone: (206) 667-2793
--
Daniel Schnaider
SP Phone: +55-11-9.7575.0822
ds at scaigroup.com <mailto:ds at scaigroup.com>
skype dschnaider
Linked In: http://www.linkedin.com/in/danielschnaider
w <http://www.arkiagroup.com/>ww.scaigroup.com <http://ww.scaigroup.com/>
Depoimentos de clientes <http://www.scaigroup.com/Projetos/depoimentos>
Casos de Sucesso & Refer?ncias <http://www.scaigroup.com/Projetos>
SCAI Group no Facebook <http://facebook.scaigroup.com/>
SCAI Group no Twitter <http://twitter.scaigroup.com/>
SCAI Group no Google Plus <http://plus.scaigroup.com/>
Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
On 11/09/2013 11:31 PM, Hadley Wickham wrote:
Modelling a mutable entity, i.e. an account, is really a perfect example of when to use reference classes. You might find the examples on http://adv-r.had.co.nz/OO-essentials.html give you a better feel for the strengths and weaknesses of R's different OO systems.
Reference classes provide less memory copying and a more familiar programming paradigm but not necessarily fantastic performance, as illustrated here http://stackoverflow.com/questions/18677696/stack-class-in-r-something-more-concise/18678440#18678440 and I think elsewhere on this or the R-devel list (sorry not to be able to provide a more precise recollection). Martin
Hadley On Sat, Nov 9, 2013 at 9:31 AM, daniel schnaider <dschnaider at gmail.com> wrote:
It is my first time programming with S4 and I can't get the setter fuction
to actually change the value of the slot created by the constructor.
I guess it has to do with local copy, global copy, etc. of the variable -
but, I could't find anything relevant in documentation.
Tried to copy examples from the internet, but they had the same problem.
# The code
setClass ("Account" ,
representation (
customer_id = "character",
transactions = "matrix")
)
Account <- function(id, t) {
new("Account", customer_id = id, transactions = t)
}
setGeneric ("CustomerID<-", function(obj,
id){standardGeneric("CustomerID<-")})
setReplaceMethod("CustomerID", "Account", function(obj, id){
obj at customer_id <- id
obj
})
ac <- Account("12345", matrix(c(1,2,3,4,5,6), ncol=2))
ac
CustomerID <- "54321"
ac
#Output
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
# CustomerID is value has changed to 54321, but as you can see it does't
> CustomerID <- "54321"
> ac
An object of class "Account"
Slot "customer_id":
[1] "12345"
Slot "transactions":
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Help!
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131110/8c993c8b/attachment.pl>