天道酬勤,学无止境

Is 'show' a normal S4 generic function?

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))

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

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)

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?

评论

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))

You can also see other methods that are defined for show by typing in

showMethods(show)

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

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • 混合 S3 和 S4 时的方法调度(Method dispatch when mixing S3 and S4)
    问题
  • How to properly document S4 methods using roxygen2
    I've seen some discussions in SO and other places regarding how this should be or will be done in future versions of Roxygen2. However, I am stuck. How should I go about documenting a S4 generic, as well as its methods, using Roxygen2? A working example for a brand new generic/methods, as well as an example for extending base S4 generic would be incredibly useful. I do not want to have to make separate (mostly) redundant documentation for each S4 method of the same generic. Due dilligence: I have tracked down a useful example for the "extract" method. However, it appears to be outdated and
  • 显示 R 包中函数的源代码 [重复](show source code for a function in a package in R [duplicate])
    问题 这个问题在这里已经有了答案: 9年前关闭。 可能的重复: R:显示包中 S4 函数的源代码 我下载了一个包( GEOquery )并正在使用一些功能。 其中之一称为Table ,据我所知,它能够制表S4数据集。 例如 > summary(GDS2853) # GDS2853 is a dataset I downloaded from NCBI Length Class Mode 1 GDS S4 getAnywhere(Table)显示 > getAnywhere(Table) A single object matching ‘Table’ was found It was found in the following places package:GEOquery namespace:GEOquery with value function (object) standardGeneric("Table") <environment: 0x06ad5268> attr(,"generic") [1] "Table" attr(,"generic")attr(,"package") [1] "GEOquery" attr(,"package") [1] "GEOquery" attr(,"group") list() attr(,"valueClass")
  • 显示 R 包中函数的源代码 [重复](show source code for a function in a package in R [duplicate])
    问题 这个问题在这里已经有了答案: 9年前关闭。 可能的重复: R:显示包中 S4 函数的源代码 我下载了一个包 ( GEOquery ) 并使用了一些功能。 其中之一称为Table ,据我所知,它能够制表S4数据集。 例如 > summary(GDS2853) # GDS2853 is a dataset I downloaded from NCBI Length Class Mode 1 GDS S4 getAnywhere(Table)显示 > getAnywhere(Table) A single object matching ‘Table’ was found It was found in the following places package:GEOquery namespace:GEOquery with value function (object) standardGeneric("Table") <environment: 0x06ad5268> attr(,"generic") [1] "Table" attr(,"generic")attr(,"package") [1] "GEOquery" attr(,"package") [1] "GEOquery" attr(,"group") list() attr(,"valueClass")
  • How to add class-specific alias without generic alias using Roxygen2?
    A simple example is that I have created an extension to show, which is a S4 base method. I don't want to cause a disambiguation fork by re-documenting show in my package, and I also want to consolidate the documentation of my extension to show in the documentation for the new class, myPkgSpClass, by adding an alias for show,myPkgSpClass-method. #' @export #' @aliases show,myPkgSpClass-method #' @rdname myPkgSpClass-class setMethod("show", "myPkgSpClass", function(object){ show(NA) }) The problem I'm having, is that this results in an serious warning during documentation-build by roxygen2, Rd
  • 如何使用 Roxygen2 在没有通用别名的情况下添加特定于类的别名?(How to add class-specific alias without generic alias using Roxygen2?)
    问题 一个简单的例子是我为show创建了一个扩展,它是一个 S4 基本方法。 我不想通过在我的包中重新记录show来引起歧义分叉,而且我还想合并我的扩展的文档以show在新类myPkgSpClass的文档中,通过为show,myPkgSpClass-method添加别名show,myPkgSpClass-method 。 #' @export #' @aliases show,myPkgSpClass-method #' @rdname myPkgSpClass-class setMethod("show", "myPkgSpClass", function(object){ show(NA) }) 我遇到的问题是,这会在 roxygen2 的文档构建期间产生严重警告, Rd files with duplicated alias 'show':因为在此包中show不止一个类扩展,并且 roxygen2 已自动添加所有相关*-class.Rd文件的别名列表中的通用术语: \alias{show} \alias{show,myPkgSpClass-method} 但是我觉得我不想在任何情况下的通用别名,因为这将迫使需要消歧之间show在我的包,和底座show 。 除了show之外,此问题也适用于从其他包扩展的其他 S4 方法。 如果我将所有特定于类的方法标记到同一个.Rd文件
  • How do I show the source code of an S4 function in a package?
    I used the packages topGO in R to analyze gene enrichment with the following code: sampleGOdata <- new("topGOdata", description = "Simple session", ontology = "BP", allGenes = geneList, geneSel = topDiffGenes, nodeSize = 10, annot = annFUN.db, affyLib = affyLib) resultFisher <- runTest(sampleGOdata, algorithm = "classic", statistic = "fisher") allRes <- GenTable(sampleGOdata, classicFisher = resultFisher, orderBy = "fisher", ranksOf = "classicFisher",topNodes = 10) I want to see and change the RunTest function and the GenTable function to change the ResultTable, but I don't know how to show
  • show source code for a function in a package in R [duplicate]
    This question already has answers here: Closed 9 years ago. Possible Duplicate: R: show source code of an S4 function in a package I downloaded a package (GEOquery) and was playing with some of the functions. One of them is called Table, which, to my understanding, is able to tabulate an S4 dataset. E.g. > summary(GDS2853) # GDS2853 is a dataset I downloaded from NCBI Length Class Mode 1 GDS S4 getAnywhere(Table) shows > getAnywhere(Table) A single object matching ‘Table’ was found It was found in the following places package:GEOquery namespace:GEOquery with value function (object)
  • show source code for a function in a package in R [duplicate]
    This question already has answers here: Closed 9 years ago. Possible Duplicate: R: show source code of an S4 function in a package I downloaded a package (GEOquery) and was playing with some of the functions. One of them is called Table, which, to my understanding, is able to tabulate an S4 dataset. E.g. > summary(GDS2853) # GDS2853 is a dataset I downloaded from NCBI Length Class Mode 1 GDS S4 getAnywhere(Table) shows > getAnywhere(Table) A single object matching ‘Table’ was found It was found in the following places package:GEOquery namespace:GEOquery with value function (object)
  • do.call error "second argument must be a list" with S4Vectors when the code is in a library
    This is a question raised in the context of this other one: Extract and paste together multiple columns of a data frame like object using a vector of column names I received an answer based on the usage of do.call which happens to not work when the code is part of a library, but worked well when implemented as part of the main script. Here is my attempt at making a simplified example: File example.do.call.R: library(S4Vectors) library(test.package) data <- DataFrame(A=letters[1:6], B=LETTERS[1:6]) test_do_paste <- function(data) { groups <- c("A", "B") combined_letters <- do.call(paste, data
  • 当代码在库中时,使用 S4Vectors do.call 错误“第二个参数必须是一个列表”(do.call error "second argument must be a list" with S4Vectors when the code is in a library)
    问题 这是在另一个上下文中提出的问题:使用列名向量提取并粘贴数据框的多列,如对象 我收到了一个基于do.call用法的答案,当代码是库的一部分时,它碰巧不起作用,但作为主脚本的一部分实现时效果很好。 这是我尝试制作一个简化示例: 文件example.do.call.R : library(S4Vectors) library(test.package) data <- DataFrame(A=letters[1:6], B=LETTERS[1:6]) test_do_paste <- function(data) { groups <- c("A", "B") combined_letters <- do.call(paste, data[groups]) print(combined_letters) } test_do_paste(data) #test.package::test_do_paste(data) 相同的test_do_paste函数作为单个函数出现在我根据 https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/ 组织和安装的包中。 如上所述运行Rscript example.do.call.R ,我获得了预期的结果: [1] "a A" "b B" "c C" "d D" "e
  • 将S4和S3方法结合在一个函数中(Combining S4 and S3 methods in a single function)
    问题 定义通用功能的好方法是什么?该通用功能应同时具有S3和S4类的实现? 我一直在使用这样的东西: setGeneric("myfun", function(x, ...){ standardGeneric("myfun"); }); setMethod("myfun", "ANY", function(x, ...) { if(!isS4(x)) { return(UseMethod("myfun")); } stop("No implementation found for class: ", class(x)); }); 这样成功了: myfun.bar <- function(x, ...){ return("Object of class bar successfully dispatched."); } object <- structure(123, class=c("foo", "bar")); myfun(object) 有没有一种“本机”的方式来实现这一目标? 我知道我们可以使用setOldClass为S3类定义S4方法,但是如果一个对象有多个类,这样我们就失去了S3方法分派。 例如(在一个干净的会话中): setGeneric("myfun", function(x, ...){ standardGeneric("myfun"); })
  • Creating Classes in R: S3, S4, R5 (RC), or R6? [closed]
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago. Improve this question I'm kind of posting an open question to look for advantages or disadvantages to creating new classes in R. From what I can tell, there's essentially four different paradigms that are used when creating classes in R, S3, S4, R5 (or RC), and R6. S3 is what most of the core libraries of R use, and there seems to be some merit to sticking with the simple, lightly
  • 为什么对于整数向量 x, as(x, “numeric”) 会触发加载额外的 S4 方法以进行强制?(Why, for an integer vector x, does as(x, “numeric”) trigger loading of an additional S4 method for coerce?)
    问题 虽然我的问题与最近的这个问题有关,但我怀疑它的答案将与 R 的 S4 对象系统的详细工作有关。 我期望的是: ( TLDR; -- 所有迹象都表明as(4L, "numeric")应该调度到一个函数,该函数的主体使用as.numeric(4L)将其转换为"numeric"向量。) 每当使用as(object, Class)将对象转换为所需的Class ,实际上是在触发对coerce()的幕后调用。 反过来, coerce()有一堆方法,这些方法根据函数调用的签名被分派到——这里是它的第一个和第二个参数的类。 要查看所有可用 S4 coerce()方法的列表,可以运行showMethods("coerce") 。 这样做表明只有一种方法可以转换为类"numeric" 。 它是签名from="ANY", to="numeric"的那个: showMethods("coerce") # Function: coerce (package methods) # from="ANY", to="array" # ... snip ... # from="ANY", to="numeric" # ... snip ... 该方法使用as.numeric()执行其转换: getMethod("coerce", c("ANY", "numeric")) # Method Definition
  • Method dispatch when mixing S3 and S4
    I'd like to understand the steps R goes through to find the appropriate function when mixing S3 and S4. Here's an example: set.seed(1) d <- data.frame(a=rep(c('a', 'b'), each=15), b=rep(c('x', 'y', 'z'), times=5), y=rnorm(30)) m <- lme4::lmer(y ~ b + (1|a), data=d) l <- lsmeans::lsmeans(m, 'b') multcomp::cld(l) I don't fully understand what happens when the final line gets executed. multcomp::cld prints UseMethod("cld"), so S3 method dispatch. isS4(l) shows that l is an S4 class object. It seems that, despite calling an S3 generic, the S3 dispatch system is completely ignored. Creating a
  • S4 泛型中的可选参数(Optional arguments in S4 generics)
    问题 dbGetQuery是dbGetQuery中的 S4 泛型。 它被声明为 dbGetQuery(rmongo.object, collection, query, skip=0, limit=1000) 对于 R 中这样的函数, skip和limit是可选参数。 但是,当我这样称呼它时 dbGetQuery(mongo, 'changesPerTypeEpoch', '{}', limit=10000) 我收到一个错误: (function (classes, fdef, mtable) 中的错误: 无法为签名“RMongo”、“character”、“character”、“missing”、“numeric”找到函数“dbGetQuery”的继承方法 查看源代码,我发现为泛型定义了两个签名: signature(rmongo.object="RMongo", collection="character", query="character", skip='numeric', limit='numeric') signature(rmongo.object="RMongo", collection="character", query="character", skip='missing', limit='missing') 所以为了让它在不通过skip情况下工作
  • R语言如何面向对象编程—s4类
    文章目录 1 了解R语言类2 S4类创建—setClass()3 S4类的类型检查—setValidity()4 S4类的泛型函数5 S4类的继承 1 了解R语言类 面向对象是一种对现实世界理解和抽象的方法,当代码复杂度增加难以维护的时候,面向对象就会显得非常重要。R主要面向统计计算,而且代码量一般不会很大,几十行,几百行,使用面向过程的编程方法就可以很好地完成编程的任务。 不过,伴随着越来越多的工程背景的人的加入,R语言开始向更多的领域发展。原来的少量的代码的面向过程的编码方式,会越来越难以维护海量代码的项目,所以必须有一种新的编程方式来代码原来的面向过程的编码思路,这种新的编程方式就是面向对象编程(Object Oriented Programming, OOP)。 R语言的类有S3类和S4类,S3类用的比较广,创建简单粗糙但是灵活,而S4类比较精细,具有严格的类结构,从其他语言过来的用户可能会习惯一些。S4对象系统具有明显的结构化特征,更适合面向对象的程序设计。Bioconductor社区,以S4对象系统做为基础架构,只接受符合S4定义的R包。 2 S4类创建—setClass() 由于S4对象是标准的面向对象实现方式, 有专门的类定义 setClass() 和类的实例化函数new()。 setClass函数声明如下: setClass(Class, slots
  • What is the benefit of import in a namespace in R?
    The namespace mechanism of R allows one to export functions which then are visible to the user. Furthermore, it allows to import functions from other packages. Whereas the benefit of export is obvious, I have more problems understanding the benefit of import. One benefit seems to be, that one can use functions from other packages without attaching the package and thereby saving memory. This is exemplified in section 1.6.4 in the writing R extensions manual. However, there must be other benefits of the import function. Especially, section 1.6.6 (that deals with S4 classes) shows the namespace
  • Optional arguments in S4 generics
    dbGetQuery is an S4 generic in RMongo. It is declared as dbGetQuery(rmongo.object, collection, query, skip=0, limit=1000) With a function like this in R, skip and limit are optional arguments. However, when I call it in this way dbGetQuery(mongo, 'changesPerTypeEpoch', '{}', limit=10000) I get an error: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘dbGetQuery’ for signature ‘"RMongo", "character", "character", "missing", "numeric"’ Looking at the source code, I found there are two signatures defined for the generic: signature(rmongo.object=
  • 编写带有两个参数的 S4 泛型方法(Writing an S4 generic method with two arguments)
    问题 我正在尝试使用通用方法定义我自己的 S4 类。 setClass("MultiplyObject", representation(the.factor = "numeric")) # Create a new instance of a class with the "new" method multobj <- new("MultiplyObject", the.factor = 3) # Create a new generic function definition setGeneric("ActOnNumber", function(object, n) { standardGeneric("ActOnNumber", n) }) # Define the ActOnNumber method for the MultiplyObject class setMethod("ActOnNumber", signature(object = "MultiplyObject", n = "numeric"), function(object, n) { object@the.factor * n }) cat(sprintf('ActOnNumber(multobj, 4) = %.2f\n', ActOnNumber(multobj, 4))) 但是