r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
407 stars 56 forks source link

[feature request] #61

Closed stefanfritsch closed 9 years ago

stefanfritsch commented 9 years ago

Hi,

is there already a way to get the signature of the constructor?

I.e. I want to get function(name, hair) from the object generator Person (standard example, see below), something like args(Person). Or have the print function print the signatures of functions instead of just the type.

I could use Person$new(...)$initialize but that only works if I already know enough about the constructor to get an object.

In my case I wanted the signature of Progress in Shiny, which needs a session object so I'd have to render the output, etc., etc. I'm aware that this is only an issue if the documentation something is lacking but it would be nice to have. :)

Thx, Stefan

Person <- R6Class("Person",
                  public = list(
                      name = NA,
                      hair = NA,
                      initialize = function(name, hair) {
                          if (!missing(name)) self$name <- name
                          if (!missing(hair)) self$hair <- hair
                          self$greet()
                      },
                      set_hair = function(val) {
                          self$hair <- val
                      },
                      greet = function() {
                          cat(paste0("Hello, my name is ", self$name, ".\n"))
                      }
                  )
)
gaborcsardi commented 9 years ago

I think this is just

args(Person$public_methods$initialize)
#> function (name, hair) 
#> NULL

but it might depend on whether you use portable classes or not.

stefanfritsch commented 9 years ago

Thanks a lot, I didn't know that.

I'd still like the print function; but as it's really a minor issue for an obscure use case I'll close this.

wch commented 9 years ago

@gaborcsardi's answer is good. You can also use formals() if you need to handle the args programatically:

> str(formals(Person$public_methods$initialize))
Dotted pair list of 2
 $ name: symbol 
 $ hair: symbol