gpt4 book ai didi

Are `$` and `[[` equivalent when accessing elements of an S4 object?(当访问S4对象的元素时,`$`和`[[`等价吗?)

转载 作者:bug小助手 更新时间:2023-10-28 10:24:59 26 4
gpt4 key购买 nike



I've always thought the $ and [[ accessors were essentially the same, based on reading various tutorials and posts( e.g., https://www.r-bloggers.com/2009/10/r-accessors-explained/). However, I am digging into an S4 object for the first time and got the surprising result below. Namely, $ works, but [[ does not.

我一直认为$和[[访问器本质上是相同的,基于阅读各种教程和帖子(例如,https://www.r-bloggers.com/2009/10/r-accessors-explained/).然而,我是第一次深入研究S4对象,得到了下面令人惊讶的结果。也就是说,$起作用,但[[不起作用。


> gse$Description %>% head(2)
[1] "cardiac muscle tissue development" "striated muscle tissue development"
> gse[["Description"]] %>% head(2)
Error in `[[.gseaResult`(gse, "Description") : input term not found...
> gse %>% slot("Description") %>% head(2)
Error in slot(., "Description") :
no slot of name "Description" for this object of class "gseaResult"
> gse@result$Description %>% head(2)
[1] "cardiac muscle tissue development" "striated muscle tissue development"
> gse@result[["Description"]] %>% head(2)
[1] "cardiac muscle tissue development" "striated muscle tissue development"

For reproducibility, here is the gse object:

为了重现性,下面是gse对象:


> gse %>% dput()
new("gseaResult", result = structure(list(ID = c("GO:0048738",
"GO:0014706", "GO:0055001", "GO:0051146", "GO:0140694"), Description = c("cardiac muscle tissue development",
"striated muscle tissue development", "muscle cell development",
"striated muscle cell differentiation", "non-membrane-bounded organelle assembly"
), setSize = c(280L, 302L, 230L, 341L, 391L), enrichmentScore = c(-0.583663510336891,
-0.543426982282193, -0.559387452878044, -0.523546550894944, -0.475084590959071
), NES = c(-2.10733279137958, -1.97184487532844, -1.97137557942708,
-1.91649873102392, -1.74800071168715), pvalue = c(1e-10, 1e-10,
1e-10, 1e-10, 1e-10), p.adjust = c(8.2175e-08, 8.2175e-08, 8.2175e-08,
8.2175e-08, 8.2175e-08), qvalue = c(7.73421052631579e-08, 7.73421052631579e-08,
7.73421052631579e-08, 7.73421052631579e-08, 7.73421052631579e-08
), rank = c(1374, 1374, 1340, 1340, 3310), leading_edge = c("tags=25%, list=7%, signal=24%",
"tags=24%, list=7%, signal=22%", "tags=23%, list=7%, signal=22%",
"tags=21%, list=7%, signal=20%", "tags=22%, list=16%, signal=19%"
)), row.names = c("GO:0048738", "GO:0014706", "GO:0055001", "GO:0051146",
"GO:0140694"), class = "data.frame"), organism = "Mus musculus",
setType = "BP", geneSets = list(), geneList = numeric(0),
keytype = "ALIAS", permScores = structure(numeric(0), dim = c(0L,
0L)), params = list(pvalueCutoff = 0.05, eps = 1e-10, pAdjustMethod = "BH",
exponent = 1, minGSSize = 10, maxGSSize = 500), gene2Symbol = character(0),
readable = FALSE, termsim = structure(numeric(0), dim = c(0L,
0L)), method = character(0), dr = list())
> gse %>% str()
Formal class 'gseaResult' [package "DOSE"] with 13 slots
..@ result :'data.frame': 5 obs. of 10 variables:
.. ..$ ID : chr [1:5] "GO:0048738" "GO:0014706" "GO:0055001" "GO:0051146" ...
.. ..$ Description : chr [1:5] "cardiac muscle tissue development" "striated muscle tissue development" "muscle cell development" "striated muscle cell differentiation" ...
.. ..$ setSize : int [1:5] 280 302 230 341 391
.. ..$ enrichmentScore: num [1:5] -0.584 -0.543 -0.559 -0.524 -0.475
.. ..$ NES : num [1:5] -2.11 -1.97 -1.97 -1.92 -1.75
.. ..$ pvalue : num [1:5] 1e-10 1e-10 1e-10 1e-10 1e-10
.. ..$ p.adjust : num [1:5] 8.22e-08 8.22e-08 8.22e-08 8.22e-08 8.22e-08
.. ..$ qvalue : num [1:5] 7.73e-08 7.73e-08 7.73e-08 7.73e-08 7.73e-08
.. ..$ rank : num [1:5] 1374 1374 1340 1340 3310
.. ..$ leading_edge : chr [1:5] "tags=25%, list=7%, signal=24%" "tags=24%, list=7%, signal=22%" "tags=23%, list=7%, signal=22%" "tags=21%, list=7%, signal=20%" ...
..@ organism : chr "Mus musculus"
..@ setType : chr "BP"
..@ geneSets : list()
..@ geneList : num(0)
..@ keytype : chr "ALIAS"
..@ permScores : num[0 , 0 ]
..@ params :List of 6
.. ..$ pvalueCutoff : num 0.05
.. ..$ eps : num 1e-10
.. ..$ pAdjustMethod: chr "BH"
.. ..$ exponent : num 1
.. ..$ minGSSize : num 10
.. ..$ maxGSSize : num 500
..@ gene2Symbol: chr(0)
..@ readable : logi FALSE
..@ termsim : num[0 , 0 ]
..@ method : chr(0)
..@ dr : list()

Is the failure of gse[["Description"]] expected? I am surprised because I had the impression $ "is handiest when doing interactive programming but should be discouraged for more production oriented code because of its limitations, namely the inability to interpolate the names or use integer indices." Does this priciple not hold for S4 objects?

预计GSE[[“Description”]]会失败吗?我很惊讶,因为我的印象是,$“在进行交互式编程时是最方便的,但应该不鼓励使用更多面向产品的代码,因为它的局限性,即无法插入名称或使用整数索引。”这一原则不适用于S4对象吗?


Relatedly, this tutorial (http://adv-r.had.co.nz/S4.html) says, "To access slots of an S4 object you use @, not $. Or if you have a character string giving a slot name, you use the slot function. This is the equivalent of [[." Is there a way to tell when $ will work (my example) vs. when @ is used?

与此相关的是,本教程(http://adv-r.had.co.nz/S4.html)说,“要访问S4对象的槽,您可以使用@而不是$。或者,如果您有一个给出槽名称的字符串,则可以使用槽函数。这相当于[[。”有没有一种方法来判断$何时起作用(我的例子),以及何时使用@?


Thanks!

谢谢!


更多回答
优秀答案推荐

$ and [[ are not exactly equivalent when accessing slots in an S4 object in R.

在R中访问S4对象中的槽时,$和[[不是完全等价的。


The key differences are:

主要区别在于:



  • $ directly accesses the slots in the S4 object, without any dispatch or validation. This is why it works in your example to get the Description column.

    $直接访问S4对象中的槽,不需要任何调度或验证。这就是在您的示例中获取Description列的原因。



  • [[ goes through the S4 dispatch mechanism, looking for a method like [[.gseaResult. This allows more control/validation, but means it will fail if no S4 method is defined.

    [[通过S4调度机制,查找类似[[.gsear Result.这允许更多的控制/验证,但这意味着如果没有定义S4方法,它将失败。




So in your case, gse$result$Description works because it directly accesses the slots. But gse[["Description"]] fails because there is no [[ method defined for the gseaResult S4 class.

因此,在您的示例中,gse$result$Description可以工作,因为它直接访问插槽。但是gse[[“Description”]]失败,因为没有为gseaResult S4类定义[[方法。


The @ operator is preferred for S4 because it goes through dispatch properly. gse@result would work in your example.

对于S4,@运算符是首选的,因为它可以正确地通过分派。Gse@Result将在您的示例中工作。


As a rule of thumb:

作为一条经验法则:



  • Use $ for quick interactive programming with S4 objects

  • Use @ and [[ for more robust production code

  • Define [[ methods on your S4 classes if you want [[ access


更多回答

pwoltschk, thanks for the explanation. I'll have to do some reading on dispatch and validation. Also, did you mean to say "in your case, gse@result$Description works"? Replacing the @ with $ and using gse$result$Description throws "Error in [.data.frame(x@result, , name) : undefined columns selected".

Pwoltschk,谢谢你的解释。我将不得不阅读一些关于调度和验证的内容。另外,您的意思是说“在您的情况下,gse@Result$Description有效”吗?将@替换为$并使用GSE$RESULT$DESCRIPTION会在[.data.Frame(x@Result,,name):选定的未定义列中引发错误“。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com