简体   繁体   中英

Include base type (e.g. list) as a class attribute?

I am writing an R package which revolves around analyzing some around data stored in a list. For example,

myData <- list(x = "x vector", y = "another vector", 
    z = "function(x,y)", meta = "info about this dataset")

The character values are just descriptions here since the values are not relevant. I have a few different kinds of data stored in a list format, but there is some overlap so I'm giving each kind a class attribute, like this

class(myData) <- "datatype1"

Now I've tested this, and seen that things like

length(myData) ; myData$x

still work, and I am under the impression that this is because the storage mode is still a list, and the operations I've tried are .Primitve or .Internal, but I don't know when/why this works.

My question is the following: Is there any circumstance in which I should worry about getting rid of 'list' as a class name? Should I instead use:

class(myData) <- c("datatype1", "list")

?

Basically I don't know if/when function dispatch fails if the class 'list' is not found but the storage mode is still a list. I've tried to read documentation and haven't seen this particular issue. If someone can point me to the relevant info, I'm happy to read the manual. Thanks!

In general some functions in some packages may be dispatched based on "list" class. For example as.data.frame or within in base R.

list1 <- list(a = 1)
class(list1) <- "nolist" 
within(list1, a <- 4)

Error in UseMethod("within") : no applicable method for 'within' applied to an object of class "nolist"

For a "list" dependent methods check

methods(class = "list")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM