s4

What is setReplaceMethod() and how does it work?

落爺英雄遲暮 提交于 2019-12-08 16:37:56
问题 I'm confused about the usage of setReplaceMethod() . Looking at ?setReplaceMethod does not provide an explanation and Googling is less than helpful. QUESTION : Please explain setReplaceMethod() , it's usage and how it works (preferably with an example). 回答1: Here is what I found. As pointed by @Hong Ooi in the comments setReplaceMethod("fun") is the same as setMethod("fun<-") , so setReplaceMethod is used to create a method for a generic replacement function in the S4 object system of R. What

Why does print of S4 class call `show` without namespacing it?

故事扮演 提交于 2019-12-08 16:37:46
问题 I have a package shinyjs with a function called show . Today a user reported to me that this introduces problems when using S4 objects because "print"-ing an S4 object uses the show method, which is masked by my package when it's attached. Example: library(shinyjs) setClass("testS4Object", representation( ID = "numeric", Name = "character" ), prototype( ID = NA_real_, Name = NA_character_ ) ) x = new("testS4Object") x There's an error because when we print the value of x , it seems to call

Automated porting of R library for Renjin

こ雲淡風輕ζ 提交于 2019-12-08 11:15:01
问题 I have some local R libraries that I would like include in my renjin Java application. Some of the libraries are written entirely in R, some libraries have C++ dependencies and some libraries have S4 classes. Ideally, I don't want to maintain two copies of each library. I am wondering if there is any automated way to take a local R library(or its sources) and generate a Renjin compatible version? 回答1: All CRAN and Bioconductor packages listed at packages.renjin.org are compiled to Java

Class slots vs. initialize signature mismatch

左心房为你撑大大i 提交于 2019-12-08 04:19:57
问题 Consider the following S4 class: setClass('Foo', representation(model='data.frame')) setMethod('initialize', 'Foo', function(.Object, a, b) { .Object@model <- data.frame(a, b) .Object }) It can be instantiated with: new('Foo', a=1:4, b=4:7) So far so good. However, when I try to subclass Foo I get an error. setClass('Bar', contains='Foo') >>> Error in data.frame(a, b) : argument "a" is missing, with no default Personally, I would prefer to instantiate class Foo with explicit arguments because

Does R copy unevaluated slots in S4 classes on assignment?

浪尽此生 提交于 2019-12-07 05:49:45
问题 Suppose I have an S4 class with two slots. I then create a method that sets one of the slots to something and returns the result. Will the other slot also be copied on assignment? For example, setClass('foo', representation(first.slot = 'numeric', second.slot = 'numeric')) setGeneric('setFirstSlot', function(object, value) {standardGeneric('setFirstSlot')}) setMethod('setFirstSlot', signature('foo', 'numeric'), function(object, value) { object@first.slot = value return(object) }) f <- new(

How to define an S4 prototype for inherited slots

孤街浪徒 提交于 2019-12-07 04:37:49
问题 I have a base class (let's call it "A") whose representation is common to many other classes. Therefore I define other classes, such as "B", to contain this class. I would like to set the prototype of these other classes (B) to include the default values for the slots inherited from A. I thought this would be natural: setClass("A", representation(a="character")) setClass("B", contains="A", prototype(a = "hello")) But it produces the error: Error in representation[!slots] : object of type 'S4'

How to set default value of a slot as NULL in R?

柔情痞子 提交于 2019-12-06 19:16:48
问题 I'm new to R. I'm trying to define a class similar to a tree node, that is , it has a left node and right node, which should be of the same class as the parent node. So I define the class as follows: setClass('Node', representation=(left='Node',right='Node', ...)) I want to set the default value of Node to be NULL by setting a prototype, but R says the following: invalid class "Node" object: invalid object for slot "left" in class "bicluster": got class "NULL", should be or extend class "Node

Copying S4 base class instance into a derived object

五迷三道 提交于 2019-12-06 14:13:46
问题 I have two simple classes: .A1 <- setClass("A1", representation=representation( x1 ="numeric"), prototype = prototype(x1 = 10)) .A2 <- setClass("A2", contains="A1", representation=representation(x2="numeric"), prototype = prototype(x2 = 10)) setMethod("initialize", "A2", function(.Object, ..., x2 = .Object@x2) { callNextMethod(.Object, ..., x2 = x2) }) Using this code everything works: a1 <- .A1(x1 = 3) initialize(a1) a2 <- .A2(x1 = 2, x2 = 4) initialize(a2, x2 = 3) .A2(a1, x2 = 2) An object

How to split and write to a file for S4 object in R

南笙酒味 提交于 2019-12-06 12:52:31
问题 I have an object of S4 class like below: > gadem Object of class 'gadem' This object has the following slots: motifs,pwm,consensus,align,name,seq,chr,start,end,strand,seqID,pos,pval,fastaHeader > gadem[[1]] An object of class "motif" Slot "pwm": 1 2 3 4 5 6 7 8 9 10 11 A 0.3404 0 0.0000 0.6375 0.2723 0.3173 0 0.0002 0.3126 0 0.4969 C 0.4281 0 0.8708 0.1474 0.0767 0.1122 0 0.0000 0.0981 1 0.2558 G 0.1414 0 0.0000 0.0361 0.4153 0.5088 0 0.1134 0.0532 0 0.0000 T 0.0901 1 0.1292 0.1790 0.2357 0

Optional arguments in S4 generics

大兔子大兔子 提交于 2019-12-06 07:27:59
问题 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