简体   繁体   中英

R: How to inherit a base data type (e.g. list, vector) in R

I'm trying to extend customized type (for example mylist) from a base type "list" in R, which contains all functions and prototype of R base "list". It should support below operators as "list":

a <- list(column1=c(1:5), column2=c(6:10))
aa <- mylist(column1=c(1:5), column2=c(6:10))
a$column1
1 2 3 4 5
aa$column1
1 2 3 4 5

All other usages of "list" in R is expected to be supported my "mylist"

My questions is : How could I create the "mylist" in R. Thanks for help.

you can append your class before the list class,

mylist <- function(...){
  structure(list(...), class = c("mylist", "list"))
}


aa <- mylist(column1=c(1:5), column2=c(6:10))
aa$column1

plot.mylist <- function(x) image(volcano)
plot(aa)

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