Reputation: 2922
I'm newbie in R and I have defined an object with one method called "show". In this method I modify the value of a slot and then I print to show its value. The value is correct.
method(show)
setMethod("show", "menu", function(object){
while (TRUE){
#Clean console
cat("\014")
cat("COMPARATIVA ENTRE EQUIPOS DE LA MISMA COMPETICION\n")
cat("-------------------------------------------------\n\n")
cat("1. Comparativa entre clubes de Liga DIA\n")
cat("2. Comparativa entre clubes de Liga Femenina 2 - Grupo 'A'\n")
cat("3. Comparativa entre clubes de Liga Femenina 2 - Grupo 'B'\n")
cat("0. Salir\n\n")
option <- readline("Selecciona opción: ")
option <- suppressWarnings(as.numeric(option))
if (!is.na(option)){
if (option == 1){
object@competition <- 14
}
if (option == 2){
object@competition <- 22
}
if (option == 3){
object@competition <- 23
}
print(object@competition)
readline("Espera ...")
if (option == 0)
break
}else{
readline("No es un número. Pulsa una tecla para introducir otra opción.")
}
}
})
But If I try to access to the slot outside this method I've got the initial value.
x <- menu(competition=0, stats=0)
x
print(x@competition)
When I call "x" I'm calling the "show" method too and inside of them I give a value to "competition" different to 0. But, later, when I try to print the value with print(x@competition) I've got like result 0.
> print(x@competition)
[1] 0
I would like to get the value assigned inside "show" method but I've got the value when I create the object. How can I modify the value of the slot correctly?
Upvotes: 0
Views: 371
Reputation: 5281
Okay so here are a couple of lines that helped for me:
myFun <- function(object) 0
setGeneric("myFun")
setClass("myClass", slots = c("competition", "stats"))
I then proceed with your code (slightly modified the the setMethod
):
setMethod("myFun", "myClass", function(object){
while (TRUE){
#Clean console
cat("\014")
cat("COMPARATIVA ENTRE EQUIPOS DE LA MISMA COMPETICION\n")
cat("-------------------------------------------------\n\n")
cat("1. Comparativa entre clubes de Liga DIA\n")
cat("2. Comparativa entre clubes de Liga Femenina 2 - Grupo 'A'\n")
cat("3. Comparativa entre clubes de Liga Femenina 2 - Grupo 'B'\n")
cat("0. Salir\n\n")
option <- readline("Selecciona opción: ")
option <- suppressWarnings(as.numeric(option))
if (!is.na(option)){
if (option == 1){
object@competition <- 14
break
}
if (option == 2){
object@competition <- 22
break
}
if (option == 3){
object@competition <- 23
break
}
print(object@competition)
readline("Espera ...")
if (option == 0)
break
}else{
readline("No es un número. Pulsa una tecla para introducir otra opción.")
}
}
return(object)
})
Here is my output:
x <- new("myClass", competition = 0, stats = 0)
# printing just x yields:
An object of class "myClass"
Slot "competition":
[1] 0
Slot "stats":
[1] 0
# Here is what myFun(x) yields:
COMPARATIVA ENTRE EQUIPOS DE LA MISMA COMPETICION
-------------------------------------------------
1. Comparativa entre clubes de Liga DIA
2. Comparativa entre clubes de Liga Femenina 2 - Grupo 'A'
3. Comparativa entre clubes de Liga Femenina 2 - Grupo 'B'
0. Salir
Selecciona opción:
# hits 1
An object of class "myClass"
Slot "competition":
[1] 14
Slot "stats":
[1] 0
Now setting y <- myFun(x)
, we obtain (again hitting 1
in the console):
> print(y@competition)
[1] 14
Upvotes: 1