如何使用 [] 或 [[]] 约定引用 bibentry 类中的特定标记

  • 本文关键字:bibentry 何使用 约定 引用 r
  • 更新时间 :
  • 英文 :


我正在尝试从号码布文件中获取特定标签的值。每当我尝试和我们entry["bibtype"]entry[["bibtype"]]时,我都会收到错误,但entry$bibtype工作正常。

entry<-bibentry(
  bibtype = "Manual",
  title = "R: A Language and Environment for Statistical Computing",
  author = person("R Development Core Team"),
  organization = "R Foundation for Statistical Computing",
  address = "Vienna, Austria",
  year = 2010,
  isbn = "3-900051-07-0",
  url = "http://www.R-project.org/")
# the first two fail
entry["bibtype"]
entry[["bibtype"]]
entry$bibtype
foo <- list("bar" = "baz")
#all of these work
foo["bar"]
foo[["bar"]]
foo$bar

我得到的错误是:

Error in switch(attr(paper, "bibtype"), Article = formatArticle(paper),  : 
  EXPR must be a length 1 vector

有人解决这个问题吗?无论如何要强迫藏书员接受这一点?

tl;dr

"bibtype"不是"bibentry"对象中的命名组件,而是作为属性实现的。这些对象的$方法在选择属性是"事物"的情况下访问该属性提供了特殊情况,而[[[方法没有此"功能"。

使用下面定义的rref

> attributes(unclass(rref)[[1]])$bibtype
[1] "Manual"
> attr(unclass(rref)[[1]], "bibtype")
[1] "Manual"

请参阅长版本以获取更广泛的阐述


您需要对象的属性,但这些属性似乎无法通过常用方法使用,例如使用 ?bibentry 中的示例:

## R reference
rref <- bibentry(bibtype = "Manual",
        title = "R: A Language and Environment for Statistical Computing",
        author = person("R Development Core Team"),
        organization = "R Foundation for Statistical Computing",
        address = "Vienna, Austria",
        year = 2010,
        isbn = "3-900051-07-0",
        url = "http://www.R-project.org/")

我们注意到str()输出中的属性"bibtype"

> str(rref)
Class 'bibentry'  hidden list of 1
 $ :List of 7
  ..$ title       : chr "R: A Language and Environment for Statistical Computing"
  ..$ author      :Class 'person'  hidden list of 1
  .. ..$ :List of 5
  .. .. ..$ given  : chr "R Development Core Team"
  .. .. ..$ family : NULL
  .. .. ..$ role   : NULL
  .. .. ..$ email  : NULL
  .. .. ..$ comment: NULL
  ..$ organization: chr "R Foundation for Statistical Computing"
  ..$ address     : chr "Vienna, Austria"
  ..$ year        : chr "2010"
  ..$ isbn        : chr "3-900051-07-0"
  ..$ url         : chr "http://www.R-project.org/"
  ..- attr(*, "bibtype")= chr "Manual"

但我们无法访问该属性

> attr(rref, "bibtype")
NULL
> attr(rref[[1]], "bibtype")
NULL

第一个失败是因为就 R 而言,类 "bibentry" 的对象在 R 中实现的方式(或者更确切地说是应用于它们的方法(attributes() attr()都看不到这个特定属性。唯一可见的属性是:

> attributes(rref)
$class
[1] "bibentry"
> attributes(rref[1])
$class
[1] "bibentry"

如果我们unclass() rref,那么我们需要意识到对象是一个列表,其中包含与双键一样多的组件。在这种情况下,rref是一个包含单个组件的列表,即类"bibentry"的对象,它是 7 个组件的列表。

一个人会天真地认为你可以这样做:

attr(rref[[1]], "bibtype")

这将从rref中获取第一个"bibentry"对象(只有一个(并查找其上的属性。这不起作用:

> attributes(rref[[1]])
$class
[1] "bibentry"

由于[[[的方法为"bibentry"对象实现的方式:

> utils:::`[[.bibentry`
function (x, i) 
{
    rval <- unclass(x)[i]
    class(rval) <- class(x)
    rval
}
<bytecode: 0x1a75938>
<environment: namespace:utils>

[单[方法的实现方式完全相同。 这意味着你可以做这样愚蠢的事情:

> rref[[1]][[1]][[1]][[1]]
R Development Core Team (2010). _R: A Language and Environment for
Statistical Computing_. R Foundation for Statistical Computing, Vienna,
Austria. ISBN 3-900051-07-0, <URL: http://www.R-project.org/>.

这没有任何意义,但所有[[1]]每次都只是指同一个对象。我不知道这是故意还是无意; ?bibentry

Note:
     The bibentry functionality is still experimental.

但它是次优的。

现在,您需要按照 utils:::`[.bibentry` 中的实现进行操作并unclass()对象,然后启动子集并访问属性:

> attributes(unclass(rref)[[1]])
$names
[1] "title"        "author"       "organization" "address"      "year"        
[6] "isbn"         "url"         
$bibtype
[1] "Manual"
> attributes(unclass(rref)[[1]])$bibtype
[1] "Manual"
> attr(unclass(rref)[[1]], "bibtype")
[1] "Manual"

utils:::`$.bibentry` 方法的实现与 utils:::`[[.bibentry` 方法的实现进行对比:

> utils:::`$.bibentry`
function (x, name) 
{
    is_attribute <- name %in% bibentry_attribute_names
    rval <- if (is_attribute) 
        lapply(unclass(x), attr, name)
    else lapply(unclass(x), "[[", name)
    if (length(rval) == 1L) 
        rval <- rval[[1L]]
    rval
}
<bytecode: 0x1b0fd70>
<environment: namespace:utils>

此方法专门设计用于处理属性。这一切在 R 中似乎有些不标准的行为。这就解释了为什么

> rref$bibtype
[1] "Manual"

有效,但(天真地(基本等同

> str(rref[["bibtype"]])
Class 'bibentry'  hidden list of 1
 $ : NULL

失败,因为未分类列表中没有名为 "bibtype" 的组件,因此 返回NULL组件,同时打印时发生错误:

> foo <- rref[["bibtype"]]
> foo
Error in switch(attr(paper, "bibtype"), Article = formatArticle(paper),  : 
  EXPR must be a length 1 vector

相关内容

最新更新