0

I am trying to understand RC classes in R (unsuccessfully). I have a simple function which returns an object of class Example. I would now like to use this function in an RC class, where I implement some methods for it. As of now I don't understand how to structure my code (e.g if the function is one r-script and the RC class I am trying to create another or if they should be in the same r-script), and how to correctly access the objects of class Example in order to get the desired output.

Below is an example:

example <- function(x, y){
  tripplex <- x*3
  doubbley <- y*2
  xy_sum <- x+y
  
  Example <- list(trippled.x = tripplex, doubbled.y = doubbley, sum =xy_sum)
  class(Example) <- "Example"
  Example
}

### I now want to use this function in an RC class and write some methods for it. ###
testClass <- setRefClass("testClass",
                         fields=list(x = "numeric",y = "numeric"),
                         methods = list(
                           initialize = function(){
                             # I don't know what to initialize it as.
                           },
                           show = function() {
                             cat(paste0("Call:\n", "x=", .self$x ,"\n", "y=", .self$y))
                             # This doesn't print proper x,y values.
                             }))

My idea is that I want something like this output:

>object <- example(5, 10)
>print(object)
Call: 
x=5
y=10
>
>object$trippled.x
15

Regards

OLGJ
  • 331
  • 1
  • 7

1 Answers1

0

Are you looking for something like this?

testClass <- setRefClass("testClass",
               fields   = list(
                  x = "numeric", 
                  y = "numeric"
               ),
                methods = list(

                  initialize = function(x, y) {
                    .self$x = x
                    .self$y = y
                  },

                  tripled_x  = function() {
                    return(3 * .self$x)
                  },
                  
                  show       = function() {
                    cat(paste0("Call:\n", "x=", .self$x ,"\n", "y=", .self$y))

                  }))

 object <- testClass(5, 10)
 print(object)
#> Call:
#> x=5
#> y=10
 object$tripled_x()
#> [1] 15

Created on 2021-09-20 by the reprex package (v2.0.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Maybe it is! Since I don't quite understand the structure it's hard to say. I've tried some experimenting with this myself - and it leads me to a question. The `.self` (variables or objects?) - do they have to be specified in the fields list? Or would it be possible to create a new `.self$` in e.g tripled_x, or how could we reference to that function/method and add it to the show method? – OLGJ Sep 20 '21 at 14:58
  • If you want a variable to be permanently stored in `object`, then `testClass` must have that variable declared in `fields`. If you want to add a calculated variable (such as `.self$triple_x`), then just calculate it during the `initialize` function and store it - something like `.self$triple_x = x * 3`. Perhaps if you could explain what you are trying to achieve in more detail in your question it would be easier to give a concrete answer. – Allan Cameron Sep 20 '21 at 15:25
  • Ok! It's an assignment in school so I don't want to get a correct answer to that assignment. I'm sorry if this is causing me to be a bit vague. But the general concept is starting to make more sense to me now! As of now it's just the methods. With the show-method above we can customize the print(object) statement. Let's say we want to implement two different methods: `plot` & `sums`. To call these we define them in the methods list and then use `object$plot()` and `object$sums`? Or are there more built in methods to access them like `plot(object)` and `sums(object)`? – OLGJ Sep 20 '21 at 16:49
  • 2nd question, kind of similar. Is there a method to change the result of running the generic `summary(object)` function? – OLGJ Sep 20 '21 at 17:23