s4

tbl_df is transformed as list in S4 class

家住魔仙堡 提交于 2019-12-04 14:14:33
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> <dbl> <dbl> 1 1 1 2 2 2 1 5 3 3 1 10 4 4 1 17 5 5 1 26 S3 objects, just for simplification, are lists

R: what are Slots?

北城以北 提交于 2019-12-04 07:35:20
问题 Does anyone know what a slot is in R? I did not find the explanation of its meaning. I get a recursive definition: "Slot function returns or set information about the individual slots of an objects" Help would be appreciated, Thanks - Alley 回答1: Slots are linked to S4 objects. A slot can be seen as a part, element or a "property" of an object. Say you have a car object, then you can have the slots "price", "number of doors", "type of engine", "mileage". Internally, that is represented a list.

Using callNextMethod() within accessor function in R

混江龙づ霸主 提交于 2019-12-04 06:54:53
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", representation(distance = "numeric"), contains = "foo") setMethod("[", "bar", function(x, i, j, drop

How to specify in which order to load S4 methods when using roxygen2

你离开我真会死。 提交于 2019-12-04 03:14:14
I ran into following problem already multiple times. Say you have two classes, classA and classB described in the following files classA.R : #' the class classA #' #' This is a class A blabla #' \section{Slots}{\describe{\item{\code{A}}{a Character}}} #' @ name classA #' @rdname classA #' @exportClass classA setClass("classA",representation(A="character")) And classB.R #' the class classB #' #' This is a class B blabla #' \section{Slots}{\describe{\item{\code{B}}{an object of class A}}} #' @ name classB #' @rdname classB #' @exportClass classB setClass("classB",representation(B="classA")) I

sum of S4 objects in R

让人想犯罪 __ 提交于 2019-12-03 19:25:02
问题 I have a S4 class and I would like to define the linear combination of these objects. Is it possible to dispatch * and + functions on this specific class? 回答1: here is an example: setClass("yyy", representation(v="numeric")) setMethod("+", signature(e1 = "yyy", e2 = "yyy"), function (e1, e2) e1@v + e2@v) setMethod("+", signature(e1 = "yyy", e2 = "numeric"), function (e1, e2) e1@v + e2) then, > y1 <- new("yyy", v = 1) > y2 <- new("yyy", v = 2) > > y1 + y2 [1] 3 > y1 + 3 [1] 4 回答2: The +

Converting package using S3 to S4 classes, is there going to be performance drop?

白昼怎懂夜的黑 提交于 2019-12-03 11:47:40
I have an R package which currently uses S3 class system, with two different classes and several methods for generic S3 functions like plot , logLik and update (for model formula updating). As my code has become more complex with all the validity checking and if/else structures due to to the fact that there's no inheritance or dispatching based on two arguments in S3 , I have started to think of converting my package to S4 . But then I started to read about the advantages and and disadvantages of S3 versus S4 , and I'm not so sure anymore. I found R-bloggers blog post about efficiency issues

Is it bad practice to access S4 objects slots directly using @?

让人想犯罪 __ 提交于 2019-12-03 05:51:00
问题 This one is almost a philosophical question: is it bad to access and/or set slots of S4 objects directly using @ ? I have always been told it was bad practice, and that users should use "accessor" S4 methods, and that developers should provide their users with these. But I'd like to know if anybody knows the real deal behind this? Here's an example using the sp package (but could be generalised for any S4 class): > library(sp) > foo <- data.frame(x = runif(5), y = runif(5), bar = runif(5)) >

Reference Class with custom field classes in R?

久未见 提交于 2019-12-03 03:41:56
I would like to use a custom reference class inside another reference class, but this code fails: nameClass <- setRefClass("nameClass", fields = list(first = "character", last = "character"), methods = list( initialize = function(char){ chunks <- strsplit(char,"\\.") first <<- chunks[[1]][1] last <<- chunks[[1]][2] }, show = function(){ cat("Special Name Class \n:") cat("First Name:") methods::show(first) cat("Last Name:") methods::show(last) } )) # this works fine nameClass$new("tyler.durden") When I try to add a second class that has a field of class nameClass this class cannot be initiated.

Include an S4 object from an existing package as a slot in a new S4 class

帅比萌擦擦* 提交于 2019-12-02 07:14:28
问题 I am writing an S4 class called Expression and would like to include an S4 object, DESeq2 = "DESeqDataSet" as a slot: setClass( Class = "Expression", representation = representation ( species = "character", edgeR = "DGEList", DESeq2 = "DESeqDataSet", lengths = "matrix", individuals = "vector", treatments = "vector", id = "vector", samples = "vector", sample_prep = "vector", genome_type = "vector", molecule_type = "vector", blast_hit = "vector", rRNA = "vector", protein = "vector" )) When I

Problems passing arguments with callNextMethod() in R

喜欢而已 提交于 2019-12-01 04:00:35
My question: Why is callNextMethod() not passing arguments as expected to the next method? Situation: Say I have two hierarchical classes foo and bar ( bar is subclass of foo ) for which I have a method foobar that can dispatch for both classes (i.e., has methods for both classes). Furthermore, the method for the (sub)class bar calls the method for foo after some calculations with callNextMethod() . Both methods have the same additional argument (with default) that should be passed to the method for foo , where only it is relevant. setClass("foo", representation(x = "numeric")) setClass("bar",