“显示"是正常的S4通用功能吗? [英] Is 'show' a normal S4 generic function?

查看:83
本文介绍了“显示"是正常的S4通用功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的类创建一个方法,该方法继承自data.frame.我最初只是希望也可以从data.frame继承'show'方法,但是我也可以编写自己的方法.我定义了我的类和显示"方法,如下所示:

I'm trying to create a method for my class, which inherits from data.frame. I was originally hoping just to inherit the 'show' method from data.frame as well, but I'm also fine with writing my own. I defined my class and 'show' method as follows:

setClass("SCvec", representation(auth = "character",
    dev = "character",
    sensor = "character",
    channel = "character",
    starttime = "character",
    endtime = "character"),
    contains="data.frame")
setMethod("show", signature(x="SCvec"), function(x) print(x))

当我在R控制台中键入show时,它会打印出来:

when I type show in the R console, it prints out:

standardGeneric,用于从包方法"中定义的显示"

standardGeneric for "show" defined from package "methods"

function (object) 
standardGeneric("show")
<bytecode: 0x0396bee8>
<environment: 0x0393ab60>
Methods may be defined for arguments: object
Use  showMethods("show")  for currently available ones.
(This generic function excludes non-simple inheritance; see ?setIs)

因此,可以肯定,我不需要自己使用StandardGeneric()将其转换为泛型.但是当我运行setMethod("show", signature(x="SCvec"), function(x) print(x))行时,我得到了错误

So it sure looks like I don't need to turn it into a generic using StandardGeneric() myself. but when I run my setMethod("show", signature(x="SCvec"), function(x) print(x)) line, I get the error

Error in match.call(definition, call, expand.dots) : 
  unused argument(s) (x = c("SCvec", ""))

我已经定义了此方法,就像定义任何其他方法一样.为什么此方法定义不起作用? 显示"与其他通用功能是否不同?

I've defined this method just like I would define any other one. Why does this method definition not work? Is 'show' different than other generic functions?

推荐答案

函数show接受参数object,因此您需要针对该形式参数定义签名和函数定义:

The function show takes an argument object, so you would need to define your signature and function definition with respect to that formal argument:

setMethod("show", signature(object="SCvec"), function(object) print(object))

您还可以通过键入

showMethods(show)

这向您展示了所有其他方法也是根据object类定义的.

And this shows you that all the other methods are defined in terms of the class of object as well.

这篇关于“显示"是正常的S4通用功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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