在 print.tbl 中使用个人打印方法
use a personnal print method inside print.tbl
我创建了一个名为 time
的 class。这是一个虚拟示例,其中 return 秒为分钟。这很好用,但是 print.time
函数没有使用 inside a tbl.
任何 ide 我如何调整显示 inside a tbl ?
此致
在此处查看 reprex
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
as.time <- function(x){
class(x)<-"time"
x
}
print.time<-function(x,...){
print.default(unclass(x/60))
invisible(x)
}
60 %>% as.time() %>% print()
#> [1] 1
tribble(~a,~time,
"a",123,
"b",234,
"c",456
) %>% mutate(time = as.time(time))
#> # A tibble: 3 x 2
#> a time
#> <chr> <dbl>
#> 1 a 123
#> 2 b 234
#> 3 c 456
由 reprex package (v0.2.1)
于 2019-02-07 创建
快速而肮脏的 hack
您可以覆盖 print.tbl
方法来执行自定义操作,然后调用 tibble:::print.tbl
函数:
library(tidyverse)
as.time <- function(x){
class(x)<-"time"
x
}
is.time <- function(x) "time" %in% class(x)
res <- tribble(~a,~time,
"a",123,
"b",234,
"c",456
) %>% mutate(time = as.time(time))
print.tbl <- function(x, ...){
res <- mutate_if(x, is.time, ~ .x / 60)
tibble:::print.tbl(res, ...)
}
res
#> # A tibble: 3 x 2
#> a time
#> <chr> <dbl>
#> 1 a 2.05
#> 2 b 3.9
#> 3 c 7.6
# Or if you want to make it ok for CRAN
print.tbl <- function(x, ...){
res <- mutate_if(x, is.time, ~ .x / 60)
print_tbl <- getFromNamespace("print.tbl", "tibble")
print_tbl(res, ...)
}
res
#> # A tibble: 3 x 2
#> a time
#> <chr> <dbl>
#> 1 a 2.05
#> 2 b 3.9
#> 3 c 7.6
由 reprex package (v0.2.1)
于 2019-02-07 创建
编辑:正确的方法
您需要定义几个方法:print
& format
、pillar_shaft
和type_sum
。
library(tibble)
library(pillar)
time <- function(x) {
as_time(x)
}
as_time <- function(x) {
structure(x, class = "time")
}
c.time <- function(x, ...) {
as_time(NextMethod())
}
`[.time` <- function(x, i) {
as_time(NextMethod())
}
format.time <- function(x, ...) {
ret <- unclass(x / 60)
format(ret)
}
print.time <- function(x, ...) {
cat(format(x), sep = "\n")
invisible(x)
}
time(360)
#> 6
type_sum.time <- function(x) {
"time"
}
pillar_shaft.time <- function(x, ...) {
out <- format(x)
pillar::new_pillar_shaft_simple(out)
}
data <- tibble(
loc = time(360)
)
data
#> # A tibble: 1 x 1
#> loc
#> <time>
#> 1 6
由 reprex package (v0.2.1)
于 2019-02-07 创建
查看更多信息:https://cran.r-project.org/web/packages/tibble/vignettes/extending.html
我创建了一个名为 time
的 class。这是一个虚拟示例,其中 return 秒为分钟。这很好用,但是 print.time
函数没有使用 inside a tbl.
任何 ide 我如何调整显示 inside a tbl ?
此致
在此处查看 reprex
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
as.time <- function(x){
class(x)<-"time"
x
}
print.time<-function(x,...){
print.default(unclass(x/60))
invisible(x)
}
60 %>% as.time() %>% print()
#> [1] 1
tribble(~a,~time,
"a",123,
"b",234,
"c",456
) %>% mutate(time = as.time(time))
#> # A tibble: 3 x 2
#> a time
#> <chr> <dbl>
#> 1 a 123
#> 2 b 234
#> 3 c 456
由 reprex package (v0.2.1)
于 2019-02-07 创建快速而肮脏的 hack
您可以覆盖 print.tbl
方法来执行自定义操作,然后调用 tibble:::print.tbl
函数:
library(tidyverse)
as.time <- function(x){
class(x)<-"time"
x
}
is.time <- function(x) "time" %in% class(x)
res <- tribble(~a,~time,
"a",123,
"b",234,
"c",456
) %>% mutate(time = as.time(time))
print.tbl <- function(x, ...){
res <- mutate_if(x, is.time, ~ .x / 60)
tibble:::print.tbl(res, ...)
}
res
#> # A tibble: 3 x 2
#> a time
#> <chr> <dbl>
#> 1 a 2.05
#> 2 b 3.9
#> 3 c 7.6
# Or if you want to make it ok for CRAN
print.tbl <- function(x, ...){
res <- mutate_if(x, is.time, ~ .x / 60)
print_tbl <- getFromNamespace("print.tbl", "tibble")
print_tbl(res, ...)
}
res
#> # A tibble: 3 x 2
#> a time
#> <chr> <dbl>
#> 1 a 2.05
#> 2 b 3.9
#> 3 c 7.6
由 reprex package (v0.2.1)
于 2019-02-07 创建编辑:正确的方法
您需要定义几个方法:print
& format
、pillar_shaft
和type_sum
。
library(tibble)
library(pillar)
time <- function(x) {
as_time(x)
}
as_time <- function(x) {
structure(x, class = "time")
}
c.time <- function(x, ...) {
as_time(NextMethod())
}
`[.time` <- function(x, i) {
as_time(NextMethod())
}
format.time <- function(x, ...) {
ret <- unclass(x / 60)
format(ret)
}
print.time <- function(x, ...) {
cat(format(x), sep = "\n")
invisible(x)
}
time(360)
#> 6
type_sum.time <- function(x) {
"time"
}
pillar_shaft.time <- function(x, ...) {
out <- format(x)
pillar::new_pillar_shaft_simple(out)
}
data <- tibble(
loc = time(360)
)
data
#> # A tibble: 1 x 1
#> loc
#> <time>
#> 1 6
由 reprex package (v0.2.1)
于 2019-02-07 创建查看更多信息:https://cran.r-project.org/web/packages/tibble/vignettes/extending.html