扩展小技巧:让新的S3类坚持下去

时间:2018-08-05 10:10:39

标签: r dplyr tibble

对于ropensci/drake#489,我想为tibbles类的"drake_plan"定义新的打印方法。我读了guide on extending tibble,这对某些要求很有帮助。但是新的"drake_plan" S3类会在dplyr操作期间删除。

#' @title Mark a data frame as a `drake` workflow plan
#' @description Used for pretty printing only (coming soon).
#'   You do not actually have to mark plans as such.
#'   You can keep them as ordinary data frames.
#' @export
#' @keywords internal
#' @param x object to mark as a `drake` plan
#' @param ... other arguments to the method
#' @examples
#' plan <- list(target = "x", command = "get_data()")
#' class(plan)
#' plan <- as_drake_plan(plan)
#' class(plan)
as_drake_plan <- function(x){
  UseMethod("as_drake_plan")
}

as_drake_plan_ <- function(x, ...){
  tibble::new_tibble(x, ..., subclass = "drake_plan")
}

#' @export
`[.drake_plan` <- function(...){
  as_drake_plan_(NextMethod())
}

#' @export
#' @rdname as_drake_plan
as_drake_plan.data.frame <- as_drake_plan_

#' @export
#' @rdname as_drake_plan
as_drake_plan.list <- as_drake_plan_

#' @export
#' @rdname as_drake_plan
as_drake_plan.tbl_df <- as_drake_plan_

library(dplyr)
library(drake)
plan <- drake_plan(
  x = get_data(),
  y = analyze_data(x)
) %>%
  as_drake_plan() %>%
  print
#> # A tibble: 2 x 2
#>   target command        
#> * <chr>  <chr>          
#> 1 x      get_data()     
#> 2 y      analyze_data(x)
class(plan)
#> [1] "drake_plan" "tbl_df"     "tbl"        "data.frame"
filter(plan, target == "x") %>%
  class()
#> [1] "tbl_df"     "tbl"        "data.frame"

除了dplyr函数之外,还有其他情况可以从tibble s中删除子类吗?

2 个答案:

答案 0 :(得分:0)

我刚刚发现这已经记录在Result :SubmitResult{engineResult='tefPAST_SEQ', engineResultCode=-190, engineResultMessage='This sequence number has already passed.', txBlob='1200002200000000240000000061D4838D7EA4C680000000000000000000000000005553440000000000C882FD6AB9862C4F90E57E1BA15C248CABAD5BF96840000000000F42407321033BF063167F21FF6C01045B4E2F03F519879B552D2611F0E885E01F08C88D15247446304402202E90609AAFBF4C105408CFF2377D48085879BEE3C7DE57AF125F73926284362A022002D7A487F5929F9A3E1050FC2B5D6AE1DD5384647AD1ABF6D322765F0ABE0A498114C882FD6AB9862C4F90E57E1BA15C248CABAD5BF983148DC6B336C7D3BE007297DB086B1D3483DEA24C2A', txJson=TxJson{transactionType='Payment', account='rKHDh61BpcojAoiATgJgDaVwdSJ64fGNwF', destination='rDveJyEotoUp9jCD1Ghi2ktEBnhHiA6RBB', amount=Amount{currency='USD', value=1, issuer='rKHDh61BpcojAoiATgJgDaVwdSJ64fGNwF'}, fee='1000000', flags=0, sequence=0, signingPubKey='033BF063167F21FF6C01045B4E2F03F519879B552D2611F0E885E01F08C88D1524', txnSignature='304402202E90609AAFBF4C105408CFF2377D48085879BEE3C7DE57AF125F73926284362A022002D7A487F5929F9A3E1050FC2B5D6AE1DD5384647AD1ABF6D322765F0ABE0A49', hash='ECAB482EB34177FA1B1E6C724F038C42308004B1F307A169FAEA88C825E11642'}} 开发页面:https://github.com/tidyverse/dplyr/issues/2532中。显然,没有计划尝试保留data %>% group_by(press_id, time_state) %>% mutate(start_flag=ifelse(time_state=='start_time' & timestamp==min(timestamp),1,0), end_flag=ifelse(time_state=='end_time' & timestamp==max(timestamp),1,0)) %>% filter(start_flag==1 | end_flag==1) # A tibble: 4 x 7 # Groups: press_id, time_state [2] press_id time_state time_state_val timestamp acc_mag start_flag end_flag <int> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> 1 1 start_time 1.64e14 1.64e14 10.5 1 0 2 1 end_time 1.64e14 1.64e14 10.5 0 1 3 1 start_time 1.64e14 1.64e14 10.5 1 0 4 1 end_time 1.64e14 1.64e14 10.5 0 1 子类。

答案 1 :(得分:0)

tibble包现在具有new_tibble()构造函数,@ landau也指出了该构造函数:

https://tibble.tidyverse.org/reference/new_tibble.html

# The nrow argument is always required:
new_tibble(list(a = 1:3, b = 4:6), nrow = 3)
#> # A tibble: 3 x 2
#>       a     b
#>   <int> <int>
#> 1     1     4
#> 2     2     5
#> 3     3     6