r如何保留自定义类的打印方法 [英] r how to keep print method for custom class

查看:59
本文介绍了r如何保留自定义类的打印方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一种使用类test打印矢量的方法:

i have defined a method for printing a vector with the class test:

print.test <- function(x,  ...) {
    x <- formatC(
        as.numeric(x),
        format = "f",
        big.mark = ".",
        decimal.mark = ",",
        digits = 1
        )
    x[x == "NA"] <- "-"
    x[x == "NaN"] <- "-"
    print.default(x)
}

适用于以下

a <- c(1000.11, 2000.22, 3000.33)
class(a) <- c("test", class(a))
print(a)
[1] "1.000,11" "2.000,22" "3.000,33"

这也有效:

round(a)
[1] "1.000,0" "2.000,0" "3.000,0"

这不是:

median(a)
[1] 2000.22
class(median(a))
[1] "numeric"

现在是我的问题是:我是否需要编写自定义方法d,此类使用中位数,例如如果是这样,它是什么样子还是有另一种方式(因为我只是希望此类以某种格式打印数据)?

now my question is: do i need to write a custom method for this class to use median e.g. and if so what would it look like or is there another way (as i simply would like this class to print the data in a certain format)?

推荐答案

问题是 median.default 返回的类为 numeric 的对象,因此将自动打印返回的对象对象不会调用您的自定义 print 方法。

下面将执行此操作。

The problem is that median.default returns an object of class numeric therefore autoprinting of the returned object does not call your custom print method.
The following will do so.

median.test <- function(x, na.rm = FALSE, ...){
    y <- NextMethod(x, na.rm = na.rm, ...)
    class(y) <- c("test", class(y))
    y
}

median(a)
#[1] "2.000,2"

对于 NA的处理值,我将首先为基本R函数定义另一种方法。如果经常使用类 test 的对象,则不是严格需要的,但是可以节省一些代码行。

As for the handling of NA values, I will first define another method for a base R function. It is not strictly needed but save some code lines if objects of class test are used frequently.

c.test <- function(x, ...){
    y <- NextMethod(x, ...)
    class(y) <- c("test", class(y))
    y
}


b <- c(a, NA)
class(b)
#[1] "test"    "numeric"

median(b)
#[1] "-"

median(b, na.rm = TRUE)
#[1] "2.000,2"

编辑。

以下内容定义了通用函数 wMedian ,默认方法和用于类<$的对象的方法c $ c>货币 ,这是OP在评论中的要求。

The following defines a generic function wMedian, a default method and a method for objects of class "currency", as requested by the OP in a comment.

请注意,必须有一个方法 print.currency ,由于它是完全相同的,所以我不对其进行重新定义。如上述 print.test 。至于其他方法,我借助新功能 as.currency 使它们更简单。

Note that there must be a method print.currency, which I don't redefine since it's exactly the same as print.test above. As for the other methods, I have made them simpler with the help of a new function, as.currency.

median.currency <- function(x, na.rm = FALSE, ...){
  y <- NextMethod(x, na.rm = na.rm, ...)
  as.currency(y)
}

c.currency <- function(x, ...){
  y <- NextMethod(x, ...)
  as.currency(y)
}

as.currency <- function(x){
  class(x) <- c("currency", class(x))
  x
}

wMedian <- function(x, ...) UseMethod("wMedian")
wMedian.default <- function(x, ...){
    matrixStats::weightedMedian(x, ...)
}

wMedian.currency <- function(x, w = NULL, idxs = NULL, na.rm = FALSE, interpolate = is.null(ties), ties = NULL, ...) {
  y <- NextMethod(x, w = w, idxs = idxs, na.rm = na.rm, interpolate = interpolate, ties = ties, ... ) 
  as.currency(y)
}


set.seed(1)
x <- rnorm(10)
wMedian(x, w = (1:10)/10)
#[1] 0.4084684
wMedian(as.currency(x), w = (1:10)/10)
#[1] "0,4"

这篇关于r如何保留自定义类的打印方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆