当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


R S3method 注册S3方法

R语言 S3method 位于 base 包(package)。

说明

在 R 脚本中注册 S3 方法。

用法

.S3method(generic, class, method)

参数

generic

命名 S3 通用函数的字符串。

class

命名 S3 类的字符串。

method

给出要注册的 S3 方法的字符串或函数。如果未给出,则使用名为generic.class 的函数。

细节

此函数只能在 R 脚本中使用:对于包代码,应该使用相应的‘⁠S3方法⁠” “NAMESPACE’指令。

例子

## Create a generic function and register a method for objects
## inheriting from class 'cls':
gen <- function(x) UseMethod("gen")
met <- function(x) writeLines("Hello world.")
.S3method("gen", "cls", met)
## Create an object inheriting from class 'cls', and call the
## generic on it:
x <- structure(123, class = "cls")
gen(x)

相关用法


注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Register S3 Methods。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。