用法:slotNames(X)

手册页上说

如果给定的参数既不是字符串也不是类 定义中,slotNames(仅限)改用类(X). 但我做了实验:

Rgames> foo <- 'whatever'
Rgames> slotNames(foo)
character(0)    #as expected
Rgames> foo <- sin
Rgames> slotNames(foo)
NULL
Rgames> class(foo)
[1] "function"
Rgames> bar <- list(a = 1, b= 2)
Rgames> slotNames(bar)
NULL
Rgames> class(bar)
[1] "list"

我是不是误解了输入参数x的描述的含义?

推荐答案

slotNames适用于中四班级.例如:

setClass("myS4class", slots = c(x = "numeric", y = "numeric"))

getSlots("myS4class")
#>         x         y 
#> "numeric" "numeric" 

如果你看一下slotNames的源代码,我们有:

function (x) {
  if (is(x, "classRepresentation")) names(x@slots) else .slotNames(x)
}

这将始终调用函数.slotNames,除非x是可能从getClass("myS4class")返回的"classRepresentation"对象.

如果我们看一下.slotNames的来源,我们会看到:

function (x) 
{
    classDef <- getClassDef(
      if (!isS4(x) && is.character(x) && length(x) == 1L) x else class(x)
    )
    if (is.null(classDef)) 
        character()
    else names(classDef@slots)
}

这意味着如果我们传递类似于sin的东西,我们将得到类"function"的形式化的S4类定义

foo <- sin
foo_class_def <- getClassDef(class(foo))

这个正式的类定义如下所示:

foo_class_def
#> Class "function" [package "methods"]
#> 
#> No Slots, prototype of class "function"
#> 
#> Extends: "OptionalFunction", "PossibleMethod"
#> 
#> Known Subclasses: 
#> Class "classGeneratorFunction", from data part
#> Class "MethodDefinition", from data part
#> Class "genericFunction", from data part
#> Class "functionWithTrace", from data part
#> Class "activeBindingFunction", from data part
#> Class "refMethodDef", from data part
#> Class "derivedDefaultMethod", by class "MethodDefinition", distance 2
#> Class "MethodWithNext", by class "MethodDefinition", distance 2
#> Class "SealedMethodDefinition", by class "MethodDefinition", distance 2
#> Class "MethodDefinitionWithTrace", by class "MethodDefinition", distance 2
#> Class "standardGeneric", by class "genericFunction", distance 2
#> Class "nonstandardGenericFunction", by class "genericFunction", distance 2
#> Class "groupGenericFunction", by class "genericFunction", distance 2
#> Class "genericFunctionWithTrace", by class "genericFunction", distance 2
#> Class "defaultBindingFunction", by class "activeBindingFunction", distance 2
#> Class "internalDispatchMethod", by class "MethodDefinition", distance 3
#> Class "MethodWithNextWithTrace", by class "MethodDefinition", distance 3
#> Class "derivedDefaultMethodWithTrace", by class "MethodDefinition", distance 3
#> Class "nonstandardGroupGenericFunction", by class "genericFunction", distance 3
#> Class "standardGenericWithTrace", by class "genericFunction", distance 3
#> Class "groupGenericFunctionWithTrace", by class "genericFunction", distance 3
#> Class "refMethodDefWithTrace", by class "refMethodDef", distance 2
#> Class "externalRefMethod", by class "refMethodDef", distance 2
#> Class "refObjectGenerator", by class "classGeneratorFunction", distance 2

它已经命名了插槽,所以.slotNames返回的插槽名称是NULL

names(foo_class_def@slots)
#> NULL

然而,如果foo是一个长度为1的字符,就像您的第一个示例一样,.slotNames将假定它是一个类名,并try 查找它的类定义.

foo <- "whatever"
foo_class_def <- getClassDef(foo)
foo_class_def
#> NULL

我们可以看到,这将导致.slotNames返回character(0)而不是NULL.

为了保持一致性,我想知道在这种情况下,函数是否应该返回NULL.

R相关问答推荐

卸载安装了BRM的模型发出的警告

计算满足R中条件的连续列

如何通过ggplot2添加短轴和删除长轴?

使用for循环和粘贴创建多个变量

计算两列中满足特定条件连续行之间的平均值

如何在ggplot2中绘制具有特定 colored颜色 的连续色轮

跨列查找多个时间报告

按时间顺序对不同事件进行分组

列名具有特殊字符时的循环回归

从R中发出的咕噜声中的BUG?

在不对R中的变量分组的情况下取两行的平均值

从线的交点创建面

是否有可能从边界中找到一个点值?

快速合并R内的值

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

如何移动点以使它们的打印不重叠

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

按镜像列值自定义行顺序

隐藏基于 case 总数的值

R data.设置函数&;连接中的列值而不使用for循环的表方法?