0

I would like to know how to create a class in R so that the only parameter that its constructor would accept can be actually used to populate two fields of that class. Example:

myClass <- setClass("myClass", slots = list (a = "numeric", b = "numeric"))
# When I do:
myClass(a = 1) 
# I would like here to populate b with a + 1 (or any more complicated logic)

This is the description of the exact issue I'm dealing with. Basically I have a class that represents acyclic directed graphs. Such a graph can be represented at least in two ways: a list of parent/child relationships among nodes or an adjacency matrix. So I want to build each instance by passing that list in and when I do so, I want the corresponding adjacency matrix to be built for me in another field of that class, so I can retrieve any of those as I need them.

Mariano
  • 65
  • 4
  • Could you explain more what you want and why you need it? Right now the problem you have is rather vague. – Paul Hiemstra Sep 30 '16 at 06:19
  • R is not a classical OO language like Java or C++. Usually such constructors, which are ubiquitous in OO languages, are not used in R. See, e.g., [this answer](http://stackoverflow.com/a/9523049/4770166) for a discussion on the topic. – RHertel Sep 30 '16 at 06:21
  • 1
    R has _many_ object oriented systems, including some that actually **do work** like Java or C++. You should consider using [R6](https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html) classes if you want or need fully private members (both methods and fields) and it supports parameterized constructors like you need. I'd avoid reference classes given the memory and speed hit that comes with them. – hrbrmstr Sep 30 '16 at 10:50
  • @hrbrmstr Are you seriously comparing Java to R in terms of OO? Try programming a design pattern and feel the difference. ;-) Of course, nobody ever doubted that it is *possible* to create *some* constructs that, as you say, "do work" in a way that is similar to Java, but the fact that you are able to do something in one programming language does not mean that it is necessary, commonly used or even of fundamental nature, as it is in Java. – RHertel Sep 30 '16 at 13:10
  • The OP isn't asking for a fully idiomatic Java comparison. They want a constructor with (I'm assuming) private fields. R has both just enough and too much OO in general, and I don't think I'd want the cruft that the Java OO brings to the table. But it has just enough OO to be useful without being pedantic. – hrbrmstr Sep 30 '16 at 13:14
  • Thank you for your comments. Basically I have a class that represents acyclic directed graphs. Such a graph can be represented at least in two ways: a list of parent/child relationships among nodes or an adjacency matrix. So I want to build each instance by passing that list in and when I do so, I want the corresponding adjacency matrix to be built for me in another field of that class, so I can retrieve any of those based on what I need... Hopefully I made myself clearer this time. Thanks! – Mariano Sep 30 '16 at 16:14

0 Answers0