s4

Combining S4 and S3 methods in a single function

﹥>﹥吖頭↗ 提交于 2019-12-28 04:08:28
问题 What is a good way of defining a general purpose function which should have implementations for both S3 and S4 classes? I have been using something like this: 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)); }); This succeeds: myfun.bar <- function(x, ...){ return("Object of class bar successfully dispatched."); } object <-

S4 constructor initialize and prototype

二次信任 提交于 2019-12-25 14:11:31
问题 I am trying to build a S4 object by calling the validity method in the constructor. setClass("Person", slot = c(Age = "numeric")) validityPerson<-function(object){ if(object@Age < 0)return("Age cannot be negative") TRUE } setValidity("Person", validityPerson) setMethod("initialize","Person", function(.Object,...){ validObject(.Object) .Object }) This code is problematic because I get new("Person", Age = 12) #Error in if (object@Age < 0) return("Age cannot be negative") : #argument is of

show source code for a function in a package in R [duplicate]

不问归期 提交于 2019-12-25 03:34:53
问题 This question already has answers here : Closed 7 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

How do I extract contents from a koRpus object in R?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 21:13:40
问题 I'm using the tm package, and looking to get the Flesch-Kincaid scores for a document using R. I found the koRpus package has some a lot of metrics including reading-level, and started using that. However, the object returned seems to be a very complicated s4 object I don't understand how to parse. So, I apply this to my corpus: txt <- system.file("texts", "txt", package = "tm") (d <- Corpus(DirSource(txt, encoding = "UTF-8"), readerControl = list(language = "lat"))) f <- function(x) tokenize

R: when to use setGeneric or export a s4 method in the namespace

ⅰ亾dé卋堺 提交于 2019-12-23 09:49:29
问题 I am writing a small R package with the idea to submit it to Bioconductor in the future, which is why I decided to try out s4 classes. Unfortunately I had problems understanding when I should use setGeneric or not in my package, and the documentation for the setGeneric method is for me more or less incomprehensible. Concrete example: I created a s4 class called Foo I defined a method for the [<- operator using setMethod("[","Foo", ...) I defined a method for the as.list function using

Create a vector of empty S4 objects

落花浮王杯 提交于 2019-12-22 10:52:52
问题 This maybe trivial but I haven't found anything online. Is it possible to create a vector of empty S4 objects in R? Something like: s4Vec<-rep(emptyS4Object,10) Thanks 回答1: Like this: s4Vec <- lapply( rep("yourClass", 10), new ) ? 回答2: I'd be tempted to go with .A <- setClass("A", representation(x="integer")) a <- list(.A())[rep(1, 100)] which makes a single instance (using the convenient generator returned by setClass ) and then replicates that object as R would replicate any other object in

Non-standard evaluation of expressions in S4 context

妖精的绣舞 提交于 2019-12-22 09:38:03
问题 This is borrowed from shiny's function exprToFunction which is used in their reactive function. Actual question How can I delay the evaluation of an expression (e.g. specified via arg expr ) in order to "capture" its content when calling a S4 method (as opposed to a standard R function)? Example Note that x_1 does not exist yet, that's why we want to delay the evaluation of expr and just "capture" its content. Function captureExpression : captureExpression <- function( expr, caller_offset = 1

Inconsistency of S4 dispatch behavior for R6 classes

邮差的信 提交于 2019-12-22 09:23:20
问题 Actual questions Shouldn't the fact that R6 classes inherit from (informal S3) class R6 allow the definition of S4 methods for signature arguments of that very class? As this is - AFAICT - not the case, what would be a workaround that is in line with current S3/S4 standards or that could somewhat be regarded as "best practice" in such situations? Background and example Reference Classes Consider the following example where you would like to define methods that dispatch on the superclass that

Is 'show' a normal S4 generic function?

允我心安 提交于 2019-12-22 08:29:46
问题 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))

Using callNextMethod() within accessor function in R

旧巷老猫 提交于 2019-12-21 12:22:36
问题 This is related to the following post Problems passing arguments with callNextMethod() in R I am writing accessors for two S4 classes, 'foo' and 'bar'. 'bar' inherits from foo and is extended only by a few slots. Instead of writing a full accessor function for objects of class 'bar' I want to pass the arguments to callNextMethod() when accessing a slot that is inherited by 'foo'. My code looks like this: foo <- setClass("foo", representation(x = "numeric", y = "numeric")) bar <- setClass("bar