s4

tbl_df is transformed as list in S4 class

你说的曾经没有我的故事 提交于 2019-12-06 06:33:46
问题 When I tried to use tbl_df in S4 classes, tbl_df slots seems to be transformed into list . library('tibble') setOldClass(c('tbl_df', 'tbl', 'data.frame')) setClass(Class = 'TestClass', slots = c(name = 'character'), contains = 'tbl_df') tmp1 <- new('TestClass', tibble(x = 1:5, y = 1, z = x ^ 2 + y), name = 'firsttest') tmp1@.Data [[1]] [1] 1 2 3 4 5 [[2]] [1] 1 1 1 1 1 [[3]] [1] 2 5 10 17 26 Can I visit the tmp1@.Data just like a tbl_df object? like tmp1@.Data # A tibble: 5 x 3 x y z * <int>

Non-standard evaluation of expressions in S4 context

≡放荡痞女 提交于 2019-12-06 03:28:44
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, brackets = TRUE ) { out <- eval(substitute(substitute(expr)), parent.frame(caller_offset)) if

Inconsistency of S4 dispatch behavior for R6 classes

社会主义新天地 提交于 2019-12-05 16:11:52
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 all instances of Reference Classes inherit from ( envRefClass ): TestRefClass <- setRefClass(

Is 'show' a normal S4 generic function?

不羁的心 提交于 2019-12-05 14:05:39
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

Does R copy unevaluated slots in S4 classes on assignment?

时间秒杀一切 提交于 2019-12-05 08:44:05
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('foo') f@second.slot <- 2 f <- setFirstSlot(f, 1) On the last line, will the values of both the first and

What's the recommended package build workflow with packages that contain S4 classes? [duplicate]

纵饮孤独 提交于 2019-12-05 04:43:16
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to properly document S4 class slots using Roxygen2 I would like to build a package that contains S4 classes using R Studio and roxygen2 . I had already documented all my functions using roxygen2 syntax when I introduced a couple of S4 classes to my package. Now I realized that there's no '@slot' functionality out-of-the-box. So I wonder how can I keep all my documentation work for the other functions and

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

时光毁灭记忆、已成空白 提交于 2019-12-05 02:23:45
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" But if I don't speficy the default value to be NULL, then the default value will be a recursive Node

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

跟風遠走 提交于 2019-12-04 18:56:17
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.0617 1 0.8864 0.5361 0 0.2473 Slot "consensus": [1] "mTCAnrTTwCm" Slot "alignList": [[1]] An object of

How to access the slots of an S4 object in R

北战南征 提交于 2019-12-04 16:25:05
问题 I'm working with wavelets on a program and I'm used the package wavelets to create the DWT of a time series using the function dwt . This function returns an object of class dwt , which is a S4 object with many slots: W , V , levels , filter , and so on. How can I access the W 's as a vector? 回答1: @ will let you access the slots of an S4 object. So if your object is called wave , then wave@W should get you your vector. Note that often the best way to do this is to not access the slot directly

Optional arguments in S4 generics

℡╲_俬逩灬. 提交于 2019-12-04 14:18:20
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=