0

Are there more ways to call a function by name like I do below via do.call?

setClass(Class = "MyClass",
         representation = representation(name = "character",
                                         type = "character"
         )
)
MyClass <- function(...) new("MyClass",...)
cC<-"MyClass"
do.call(cC,list())
Klaus
  • 1,946
  • 3
  • 19
  • 34
  • @SimonO101 Loosely speaking I have a config file from where I read Class Names like MyClass and want to create an Object of class MyClass by this String. – Klaus Sep 04 '13 at 10:49
  • OK, so I don't understand why you don't just use `new("MyClass",...)`? I think I am not getting something. – Simon O'Hanlon Sep 04 '13 at 10:51
  • Because It looks nicer if you call your Object by name like `a<-MyClass()` or `b<-list()`. – Klaus Sep 04 '13 at 10:55
  • 1
    `setClass` returns a generator function so no need to create one of your own and no need to call the generator function by a new name, so the common ways are `MyClass <- setClass(Class = "MyClass", representation = representation(name = "character", type = "character")); MyClass(); do.call("MyClass", list())` – Martin Morgan Sep 04 '13 at 12:33
  • @MartinMorgan Many thx you are completly right, I got this strategy from [here](http://www.stamats.de/S4KlassenUndMethoden.R) – Klaus Sep 04 '13 at 13:28

1 Answers1

1

Another way:

cstor <- get(cC)
cstor()
Karl Forner
  • 4,175
  • 25
  • 32