#library(knitr)
knit("3. Class methods.Rmd")
##
##
## processing file: 3. Class methods.Rmd
##
|
| | 0%
|
|......... | 8%
|
|.................. | 15% (unnamed-chunk-12)
|
|........................... | 23%
|
|................................... | 31% (unnamed-chunk-13)
|
|............................................ | 38%
|
|..................................................... | 46% (unnamed-chunk-14)
|
|.............................................................. | 54%
|
|....................................................................... | 62% (unnamed-chunk-15)
|
|................................................................................ | 69%
|
|........................................................................................ | 77% (unnamed-chunk-16)
|
|................................................................................................. | 85%
|
|.......................................................................................................... | 92% (unnamed-chunk-17)
|
|...................................................................................................................| 100%
## output file: 3. Class methods.md
## [1] "3. Class methods.md"
#Adding members to a class
Person$set(which = "public", name="color", value = NA, overwrite = TRUE)
Person
## <Person> object generator
## Public:
## name: NULL
## age: NULL
## desire: NULL
## color: NA
## initialize: function (name = NA, age = NA, desire = NA)
## set_age: function (val)
## set_desire: function (val)
## say_hello: function ()
## clone: function (deep = FALSE)
## set_color: function (val)
## Parent env: <environment: R_GlobalEnv>
## Locked objects: TRUE
## Locked class: FALSE
## Portable: TRUE
#Writing a method to adding person
Person$set(which = "public", name = "set_color", value = function(val){
self$color <- val
cat(self$name, " favorite color: ", val, ".\n", sep="")
}, overwrite = TRUE
)
#calling a new method
Ana <- Person$new(name = "Ana", age = 28, "the good of all ")
## Hello, my name is Ana.
## age of 28.
## desire of the good of all .
Ana$set_color(" green")
## Ana favorite color: green.