Home > Net >  Can we find all classes within base R?
Can we find all classes within base R?

Time:12-23

Is there a way to get all the classes that are defined within base R? My expected output is a character vector containing the class names. Something like this:

"character" "factor" "function" "dataframe" "list" ...

Background: I made a function that is supposed to be applied to as many classes as possible. I can't explain what I'm doing in detail but consider functions like str that as far as I know can be applied to any class. On the other hand, there are functions like sum that expect certain input, i.e., numeric input. I want to know all predefined classes so that I can see on what classes my function works and which ones don't.

CodePudding user response:

1) Assuming this refers to S3 classes start a fresh session, load any packages you want included and run this:

sort(unique(sub(".*?\\.", "", ls(.__S3MethodsTable__.))))

2) A different approach assuming this refers to package "base" (it can be repeated for other base packages if needed) and S3 classes this finds all names in the base package that have one or more dots in them and then the unique values after the dot giving res1 if there is one dot and res2 if there are two or more dots and not in res1. This is only an approximation so you would have to manually go through these to check which ones actually represent classes.

nms <- grep("\\.", ls(asNamespace("base")), value = TRUE)
no_dots <- lengths(gregexpr(".", nms, fixed = TRUE))

res1 <- sort(unique(sub(".*\\.", "", nms[no_dots == 1])))
res2 <- setdiff(sort(unique(sub(".*?\\.", "", nms[no_dots > 1]))), res1)

CodePudding user response:

It will become tricky even the function sum supports more than you think, also depending on class you get an issue on matrix or an array where you need to use typeof as well to see if its content is either numeric, integer for example. Also note that NA is considered logical by default and NULL gives the class NULL.

Here some examples on different class variables and its behaviour with sum

a <- 1L
class(a)
[1] "integer"

b <- 1
class(b)
[1] "numeric"

c <- FALSE
class(c)
[1] "logical"

d <- as.factor(1L)
class(d)
[1] "factor"

e <- matrix(1:2)
class(e)
[1] "matrix"
typeof(e)
[1] "integer"

f <- c(1:2)
class(f)
[1] "integer"

g <- NA
class(g)
[1] "logical"

h <- NULL
class(h)
[1] "NULL"

i <- -Inf
class(i)
[1] "numeric"

j <- array(1:2)
class(j)
[1] "array"
typeof(j)
[1] "integer"

class(sum(a, a))
[1] "integer"

class(sum(a, b))
[1] "numeric"

class(sum(b, b))
[1] "numeric"

class(sum(c, c))
[1] "integer"

class(sum(c, a))
[1] "integer"

class(sum(c, b))
[1] "numeric"

class(sum(d   d))
[1] "integer"
Warning message:
In Ops.factor(d, d) : ‘ ’ not meaningful for factors

class(sum(d, d))
Error in Summary.factor(1L, 1L, na.rm = FALSE) : 
  ‘sum’ not meaningful for factors

class(sum(e, d))
[1] "integer"

class(sum(e, e))
[1] "integer"

class(sum(f, f))
[1] "integer"

class(sum(g, h))
[1] "integer"

class(sum(h, h))
[1] "integer"

class(sum(i, i))
[1] "numeric"

class(sum(e, j))
[1] "integer"
  • Related