R:命名列表和描述列表
R有两个不常用的类:"Dlist"和"namedList".
R has two classes not so commonly used: "Dlist" and "namedList".
关于第一个,它是针对 Sys.getenv()
提及的,如果缺少其参数,则返回类"Dlist"的结果,以进行漂亮的打印.实际上,该类有一个 print.Dlist
方法.还有一个显然相关的 formatDL
函数用于格式化描述列表.但是,我不知道如何创建"Dlist"类的对象.
As regard the first, it is mentioned with respect to Sys.getenv()
, which returns a result of class "Dlist" if its argument is missing, for nice printing. There is in fact a print.Dlist
method for the class. There is also an apparently related formatDL
function to format description lists. However I do not know how to create an object of class "Dlist".
关于"namedList",它是由手册定义的:
As regards "namedList", it is defined by the manual:
保留名称属性的列表"替代方法
the alternative to "list" that preserves the names attribute
在这种情况下,我无法创建这种类型的对象,也没有找到使用该对象的任何实例.
In this case I am unable to create an object of this type and I have not found any instance where it is used.
如何创建"Dlist"或"namedList"?
您能否提供一个示例,其中"namedList"比普通的"命名列表" 更方便?(意为列表 L
,其中 names(L)
不是 NULL
))
How do you create a "Dlist" or a "namedList"?
Can you provide an example where the "namedList" is more convenient of an ordinary named list? (intended as a list L
, where names(L)
is not NULL
)
Dcode 是R的Base包中定义的一个非正式类,其存在的唯一目的是漂亮地打印命名的字符向量.根本不是列表,而是一种字符向量:
A Dlist
is an informal class defined in R's Base package that exists for the sole purpose of pretty-printing a named character vector. It's not a list at all, but a type of character vector:
a <- Sys.getenv()
class(a)
# [1] "Dlist"
typeof(a)
# [1] "character"
只需将 Dlist
写入命名字符向量的class属性即可创建一个
You can make one just by writing Dlist
to a named character vector's class attribute:
hello_world <- c(Hello = "World", Hi = "Earth", Greetings = "planet");
class(hello_world) <- "Dlist"
hello_world
# Hello World
# Hi Earth
# Greetings planet
您可以使用 formatDL
选择格式设置选项:
You can select formatting options with formatDL
:
cat(formatDL(hello_world, style = "list", width = 20), sep = "\n")
# Hello: World
# Hi: Earth
# Greetings: planet
Dlist仅在base R中用于将环境变量打印到控制台.除非您希望以这种方式打印命名的字符矢量,否则就不需要Dlist.
The Dlist is only used in base R for printing environmental variables to the console. Unless you wanted to have a named character vector printed this way, you don't need a Dlist.
另一方面,namedList是在 methods
包中定义的正式S4对象(也预装在R中).它从 list
继承其属性,并定义一个方法-通用S4 show
的自身版本.您可能会使用它作为基类,从中创建新的S4类,以继承命名列表(即具有 names
属性的普通列表)的属性,尽管尚不清楚用户为什么要进行足够的高级操作创建S4类不只是自己做.它是在此处定义的>.
On the other hand, a namedList is a formal S4 object as defined in the methods
package (also preloaded in R). It inherits its attributes from list
and defines a single method - its own version of the generic S4 show
.
You might use it as a base class from which to make new S4 classes that inherit the properties of a named list (i.e. a normal list with a names
attribute), although it's not clear why a user advanced enough to create S4 classes wouldn't just do that themselves. It's defined here.
您可以使用 new
创建一个namedList:
You can create a namedList with new
:
n <- new("namedList", list(a=1, b=2))
n
# An object of class "namedList"
# $`a`
# [1] 1
#
# $b
# [1] 2
isS4(n)
# [1] TRUE