我将如何实现内部函数的方法分派dplyr::do
?
我已经阅读了 GitHub 问题#719、#3558和#3429,这些问题提供了有关如何为dplyr
动词创建方法的有用信息,但没有什么特别适用的信息dplyr::do
- 这有点“特殊”,因为调度不仅需要为dplyr:do
自己发生,也需要为内部调用的函数发生dplyr::do
(或者至少这就是我所追求的)
这是我尝试过的:
预赛
library(dplyr)
#>
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# Example data ------------------------------------------------------------
df <- tibble::tibble(
id = c(rep("A", 5), rep("B", 5)),
x = 1:10
)
df_custom <- df
class(df_custom) <- c("tbl_df_custom", class(df_custom))
# Reclass function --------------------------------------------------------
reclass <- function(x, result) {
UseMethod('reclass')
}
reclass.default <- function(x, result) {
class(result) <- unique(c(class(x)[[1]], class(result)))
attr(result, class(x)[[1]]) <- attr(x, class(x)[[1]])
result
}
第 1 步:尝试为 dplyr 动词定义方法
# Custom method for summarize ---------------------------------------------
summarise.tbl_df_custom <- function (.data, ...) {
message("Custom method for `summarise`")
result <- NextMethod("summarise")
ret <- reclass(.data, result)
print(class(ret))
ret
}
ret <- df_custom %>%
summarise(y = mean(x))
#> Custom method for `summarise`
#> [1] "tbl_df_custom" "tbl_df" "tbl" "data.frame"
ret %>% class()
#> [1] "tbl_df_custom" "tbl_df" "tbl" "data.frame"
第 2 步:尝试为另一个 dplyr 动词定义一个方法来测试更长的管道
# Custom method for group_by ----------------------------------------------
group_by.tbl_df_custom <- function (.data, ..., add = FALSE) {
message("Custom method for `group_by`")
result <- NextMethod("group_by")
ret <- reclass(.data, result)
print(class(ret))
ret
}
ret <- df_custom %>%
group_by(id) %>%
summarise(y = mean(x))
#> Custom method for `group_by`
#> [1] "tbl_df_custom" "grouped_df" "tbl_df" "tbl"
#> [5] "data.frame"
#> Custom method for `summarise`
#> [1] "tbl_df_custom" "tbl_df" "tbl" "data.frame"
ret %>% class()
#> [1] "tbl_df_custom" "tbl_df" "tbl" "data.frame"
第3步:尝试相同的do
# Custom method for do ----------------------------------------------------
do.tbl_df_custom <- function (.data, ...) {
message("custom method for `do`")
result <- NextMethod("do")
ret <- reclass(.data, result)
print(class(ret))
ret
}
foo <- function(df) {
UseMethod("foo")
}
foo.default <- function(df) {
message("Default method for `foo`")
df %>%
summarise(y = mean(x))
}
foo.tbl_df_custom <- function(df) {
message("Custom method for `foo`")
df %>%
summarise(y = mean(x) * 100)
}
ret <- df_custom %>%
group_by(id) %>%
do(foo(.))
#> Custom method for `group_by`
#> [1] "tbl_df_custom" "grouped_df" "tbl_df" "tbl"
#> [5] "data.frame"
#> custom method for `do`
#> Default method for `foo`
#> Default method for `foo`
#> [1] "tbl_df_custom" "grouped_df" "tbl_df" "tbl"
#> [5] "data.frame"
ret
#> # A tibble: 2 x 2
#> # Groups: id [2]
#> id y
#> <chr> <dbl>
#> 1 A 3
#> 2 B 8
ret %>% class()
#> [1] "tbl_df_custom" "grouped_df" "tbl_df" "tbl"
#> [5] "data.frame"
虽然这看起来不错,但问题是调用了默认方法而不是自定义方法。foo
由reprex 包(v0.2.1)于 2019 年 1 月 8 日创建