我正在尝试从bib文件中获取特定标记的值。每当我尝试使用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
有人解决了这个问题吗?无论如何强迫bibentries接受这个?
答案 0 :(得分:5)
<强> TL;博士强>
"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/")
我们在"bibtype"
str()
> 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
,那么我们需要意识到该对象是一个包含与bibentries一样多的组件的列表。在这种情况下,rref
是一个包含单个组件的列表,它是“bibentry”类的对象,它是7个组件的列表。
一个天真的人会认为你可以这样做:
attr(rref[[1]], "bibtype")
将从"bibentry"
获取第一个rref
对象(只有一个)并查找其中的属性。这不起作用:
> 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