S3 类 的这些方括号是什么?
What are these square brackets for S3 classes?
我从 an open source repo on git 那里得到了这个。这显示了 S3 类 的泛型和方法的编写。但是我不明白分配给函数的符号或约定。以下是我的问题:
- 使用反引号
``
定义函数名。通常我们不会使用反引号甚至双引号来分配 variables/functions 但我看到这种情况发生了很多次。这是命名约定吗?
- 为什么
.
包含在 blob 名称之前?通常它不会被称为 blob 而一个方法将是 method.blob?
- 为什么会有
[
括号?特别是 [<-
和 [[<-
。我们是否在执行某种双重分配?
希望有人能够阐明什么是 ha
#' @export
`[.blob` <- function(x, i, ...) {
new_blob(NextMethod())
}
#' @export
`[<-.blob` <- function(x, i, ..., value) {
if (!is_raw_list(value)) {
stop("RHS must be list of raw vectors", call. = FALSE)
}
NextMethod()
}
#' @export
`[[<-.blob` <- function(x, i, ..., value) {
if (!is.raw(value) && !is.null(value)) {
stop("RHS must be raw vector or NULL", call. = FALSE)
}
if (is.null(value)) {
x[i] <- list(NULL)
x
} else {
NextMethod()
}
}
总结
如果您在 R 中创建一个您想要 'different' 子集和赋值行为的新对象,您应该为这些操作创建关联的方法。
.
以您期望的方式工作 - 方法调度
[.blob
正在覆盖 S3 [
子集运算符
[<-.blob
覆盖 S3 [<-
运算符(即 vector-subset 赋值)
[[<-.blob
覆盖 S3 [[<-
运算符(即 list-subset 赋值)
特殊符号(如反引号、括号、percent-sign、名称中有空格的变量)默认不能为"assigned to"。为此,如果用反引号包围它,它就可以工作。例如,一个名为 A B
的变量不能用 A B <- 1
赋值,而 `A B` <- 1
有效(信用@r2evans)
例子
子集
以[.blob
为例,这允许您为blob
对象创建自己的子集操作。
## Create your own blob object (class)
blob <- 1:5
attr(blob, "class") <- "blob"
## create a subset operator, which in this example just calls the next method in the s3 dispatch chain
`[.blob` <- function(x, i, j, ...) NextMethod()
因为我们在自己的子集方法中没有做任何特殊的事情,所以这就像普通的 R 向量一样工作
blob[3]
# [1] 3
然而,我们可以让子集操作做任何我们想做的事情,例如总是return向量的第一个元素
## define the function to always subset the first element
`[.blob` <- function(x, i, j, ...) { i = 1; NextMethod() }
现在您的 blob
对象只会 return 第一个元素。
blob[1]
# [1] 1
blob[2]
# [1] 1
blob[3]
# [1] 1
作业
与其中一个赋值运算符类似,如果用
重载 [<-
`[<-.blob` <- function(x, i, j, ...) { i = 5; NextMethod() }
这将始终为 blob 对象的第 5 个元素分配新值
blob[1] <- 100
blob
# [1] 1 2 3 4 100
# attr(,"class")
# [1] "blob"
反向跳动
使用了back-ticks,所以我们可以将functions/variables分配给特殊符号。
例如,尝试将向量分配给 [
符号
[ <- 1:5
# Error: unexpected '[' in "["
而用刻度线包围它让它通过(尽管不推荐这个例子)
`[` <- 1:5
`[`
# [1] 1 2 3 4 5
我从 an open source repo on git 那里得到了这个。这显示了 S3 类 的泛型和方法的编写。但是我不明白分配给函数的符号或约定。以下是我的问题:
- 使用反引号
``
定义函数名。通常我们不会使用反引号甚至双引号来分配 variables/functions 但我看到这种情况发生了很多次。这是命名约定吗? - 为什么
.
包含在 blob 名称之前?通常它不会被称为 blob 而一个方法将是 method.blob? - 为什么会有
[
括号?特别是[<-
和[[<-
。我们是否在执行某种双重分配?
希望有人能够阐明什么是 ha
#' @export
`[.blob` <- function(x, i, ...) {
new_blob(NextMethod())
}
#' @export
`[<-.blob` <- function(x, i, ..., value) {
if (!is_raw_list(value)) {
stop("RHS must be list of raw vectors", call. = FALSE)
}
NextMethod()
}
#' @export
`[[<-.blob` <- function(x, i, ..., value) {
if (!is.raw(value) && !is.null(value)) {
stop("RHS must be raw vector or NULL", call. = FALSE)
}
if (is.null(value)) {
x[i] <- list(NULL)
x
} else {
NextMethod()
}
}
总结
如果您在 R 中创建一个您想要 'different' 子集和赋值行为的新对象,您应该为这些操作创建关联的方法。
.
以您期望的方式工作 - 方法调度[.blob
正在覆盖 S3[
子集运算符[<-.blob
覆盖 S3[<-
运算符(即 vector-subset 赋值)[[<-.blob
覆盖 S3[[<-
运算符(即 list-subset 赋值)特殊符号(如反引号、括号、percent-sign、名称中有空格的变量)默认不能为"assigned to"。为此,如果用反引号包围它,它就可以工作。例如,一个名为
A B
的变量不能用A B <- 1
赋值,而`A B` <- 1
有效(信用@r2evans)
例子
子集
以[.blob
为例,这允许您为blob
对象创建自己的子集操作。
## Create your own blob object (class)
blob <- 1:5
attr(blob, "class") <- "blob"
## create a subset operator, which in this example just calls the next method in the s3 dispatch chain
`[.blob` <- function(x, i, j, ...) NextMethod()
因为我们在自己的子集方法中没有做任何特殊的事情,所以这就像普通的 R 向量一样工作
blob[3]
# [1] 3
然而,我们可以让子集操作做任何我们想做的事情,例如总是return向量的第一个元素
## define the function to always subset the first element
`[.blob` <- function(x, i, j, ...) { i = 1; NextMethod() }
现在您的 blob
对象只会 return 第一个元素。
blob[1]
# [1] 1
blob[2]
# [1] 1
blob[3]
# [1] 1
作业
与其中一个赋值运算符类似,如果用
重载[<-
`[<-.blob` <- function(x, i, j, ...) { i = 5; NextMethod() }
这将始终为 blob 对象的第 5 个元素分配新值
blob[1] <- 100
blob
# [1] 1 2 3 4 100
# attr(,"class")
# [1] "blob"
反向跳动
使用了back-ticks,所以我们可以将functions/variables分配给特殊符号。
例如,尝试将向量分配给 [
符号
[ <- 1:5
# Error: unexpected '[' in "["
而用刻度线包围它让它通过(尽管不推荐这个例子)
`[` <- 1:5
`[`
# [1] 1 2 3 4 5