0

I'd like to create a class in R that is like a data.frame but with fixed columns names.

For example this following data.frame could be the model of my class.

  min max name_intervalles
1   0  10     Intervalle 1

I tried stuffs like this

setClass("interval2",contains="data.frame",slots=c(names=c("min","max","name_Intervalles")))

But it does not work.

Any idea ?

The goal is to check within a function if the input argument is correct to what expected. Checking if it's a data.frame is a too wide check. And checking the name of the columns seems not alright to me.

Thanks

jakzr
  • 129
  • 1
  • 1
  • 11
  • 1
    It seems like you need a function to check if a data frame is the correct format, not a class. – Gregor Thomas Jul 12 '17 at 18:32
  • another option might be to pass the column names as arguments to the function, instead of hard-coding them within the function. You can supply default column names while doing so, as well. – user5359531 Jul 12 '17 at 19:37
  • @Gregor I could code that but I thought creating a class would be more efficient and rigourous ? – jakzr Jul 13 '17 at 08:32
  • @user5359531 This would make too many arguments for the function, the advantage of using a data.frame is that one object contains many columns and infos, right? – jakzr Jul 13 '17 at 08:33
  • In object-oriented languages where classes are well supported with rigorous constructors, yes, making a class would be efficient and rigorous. In R, which is a functional language with several different object-oriented systems, I think it becomes painful and unnecessary. A data frame is an S3 class, but S3 classes won't work for your purpose, so you need an S4 or other class system. But even S4 classes seem to be designed around the main purpose of method dispatch. Your use case is about validating input, not method dispatch. It could be a simple function or a very complicated class. – Gregor Thomas Jul 13 '17 at 16:07
  • See also `fortunes::fortune(288)`, `fortunes::fortune(121)`, [S3 vs S4 classes](https://stackoverflow.com/a/6451272/903061), [S4 constructors and prototypes](https://stackoverflow.com/q/40024922/903061), [Classes in R: S3, S4, RC, R6](https://stackoverflow.com/q/27219132/903061). – Gregor Thomas Jul 13 '17 at 16:25

0 Answers0