Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 Class Names: What's Allowed?

Tags:

class

r

Are there any restrictions on the names of S3 classes? For instance, are spaces allowed in the name? I see that data frames have a class of "data.frame" not "data frame". If there is not a formal restriction, are there potential problems with having spaces in the name? I just don't run into anything other than the basic modes so I'm sure those of you with more experience will know.

like image 888
Bryan Hanson Avatar asked Oct 19 '11 21:10

Bryan Hanson


2 Answers

Doesn't look there are many restrictions at all. Check out this monstrosity:

`plot.44 !@#$%^&` <- function(x) {
     plot(rnorm(x), pch=16, col="red",
          main = "But why would you want to do this??")
}

dat <- 55
class(dat) <- "44 !@#$%^&"
plot(dat)

One reason not to put spaces into a class name is that it makes it a bit trickier to directly call methods for that class.

plot.44 !@#$%^&(100)   # This doesn't work 

`plot.44 !@#$%^&`(100) # You have to do this instead
like image 185
Josh O'Brien Avatar answered Sep 18 '22 14:09

Josh O'Brien


Spaces are allowed:

test = 1
class( test ) = c( class( test ) , "My Class" )

Not sure about other restrictions or consequences of having special characters in class names. Certainly having spaces/special characters in class names is not conventional (not just R, but other languages). I suggest avoiding that.

like image 21
SFun28 Avatar answered Sep 18 '22 14:09

SFun28