s4

How to overload S4 slot selector `@` to be a generic function

≯℡__Kan透↙ 提交于 2021-02-09 08:49:06
问题 I am trying to turn the @ operator in R into a generic function for the S3 system. Based on the chapter in Writing R extensions: adding new generic I tried implementing the generic for @ like so: `@` <- function(object, name) UseMethod("@") `@.default` <- function(object, name) base::`@`(object, name) However this doesn't seem to work as it breaks the @ for the S4 methods. I am using Matrix package as an example of S4 instance: Matrix::Matrix(1:4, nrow=2, ncol=2)@Dim Error in @.default

How can I set up a Class containing itself in R (for a tree)?

拥有回忆 提交于 2021-02-08 03:54:57
问题 I need a class that may or may not contain itself, for usage as a tree in R. Every Node has Side, Analytical_Matrix, MaxChi2 and P and Sons are of type Node too. The first time a Node is created, I need the Sons to be empty or NULL. But later I create them and asign them as Sons (I have a limit of max 3 sons). I have tried this for setting up the Class: setClass(Class = "Node",slots=c(Side="character",Analytical_matrix="data.frame",MaxChi2="data.frame",P="numeric",TerminalNode="logical",LSon=

non-numeric argument to binary operator when defining a data.frame method for `+` and using on ggplot object

房东的猫 提交于 2021-01-28 01:03:58
问题 I can define a S3 method like this : `+.data.frame` <- function(e1,e2){"hello"} iris + iris # [1] "hello" But this won't work if e2 is a gg object : iris + geom_point() Error in iris + geom_point() : non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("+.data.frame", "+.gg") for "+" I think it has something to do with S4 methods but I'm confused. Can you explain what's at play and how to sort it out ? desired output : iris + geom_point() # [1] "hello"

Error: $ operator not defined for this S4 class

空扰寡人 提交于 2021-01-27 04:41:30
问题 I'm trying to make a formula and I got the error: $ operator not defined for this S4 class with R. First of all, what is a S4 class? What am I doing wrong? Following the code: as.formula("ctree(d$sex ~ d$ahe , data = d)") If you want to reproduce it, the dataset (CSV file) d is available here. 回答1: You are giving as.formula the wrong input here. Only d$sex ~ d$ahe should be a formula, so: ctree(as.formula("d$sex ~ d$ahe")) Or: ctree(as.formula("sex ~ ahe"), data = d) 来源: https://stackoverflow

How to execute S4 class method in Python (using rpy2)?

我怕爱的太早我们不能终老 提交于 2020-12-13 11:17:09
问题 I have made the following S4 class in R, its constructor takes a variable called 'A', which is a string. The class has a method called 'test_method', which takes as input a string 'B' and returns whether 'A' equals 'B'. test_class <- setRefClass( "test_class", fields=list(A="character"), methods = list( test_method = function(B) { if (A==B) { return('yes') } else { return('no') } } ) ) Now, I can make an instance of this class and execute 'test_method' on it: object <- test_class(A='hello')

Error in getMethod(“summary”, signature = “FitDiff”)

天大地大妈咪最大 提交于 2020-08-09 08:46:42
问题 I am comparing lavaan objects using semTools::compareFit . It is throwing a very strange error message. I tried also the following reproducible example: data("HolzingerSwineford1939",package="lavaan") HS.modelA <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9' HS.modelB<- ' visual =~ x1 + x2 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9' fit.A<- cfa(HS.modelA, data = HolzingerSwineford1939) fit.B<- cfa(HS.modelB, data = HolzingerSwineford1939) semTools::compareFit(fit

Error in getMethod(“summary”, signature = “FitDiff”)

和自甴很熟 提交于 2020-08-09 08:46:17
问题 I am comparing lavaan objects using semTools::compareFit . It is throwing a very strange error message. I tried also the following reproducible example: data("HolzingerSwineford1939",package="lavaan") HS.modelA <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9' HS.modelB<- ' visual =~ x1 + x2 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9' fit.A<- cfa(HS.modelA, data = HolzingerSwineford1939) fit.B<- cfa(HS.modelB, data = HolzingerSwineford1939) semTools::compareFit(fit

How to use validity functions correctly with inherited S4 classes in R

狂风中的少年 提交于 2020-01-03 12:08:25
问题 let's assume you have one S4 class "A", and a subclass "B" which has additional features. Each have their own validity checks in place - B should only check the additional features. Now in the initialization of B, I would like to start out from an object of class A, and then amend it with the additional features. However, this creates problems, and I guess I am somewhere violating R's assumptions in this example. Here's the dummy code: setClass(Class="A", representation= representation(x=

convert S4 DataFrame of Rle objects to sparse matrix in R

南楼画角 提交于 2020-01-02 06:56:08
问题 In the R language, I have an S4 DataFrame consisting of Rle encoded elements. The data can be simulated using following code x = DataFrame(Rle(1:10),Rle(11:20),Rle(21:30)) Now, I want to convert this DataFrame to a sparse matrix from the Matrix package. On a usual data.frame, one can do Matrix(x,sparse=TRUE) However, this does not work for DataFrames, as it gives the following error: Matrix(x,sparse=TRUE) Error in as.vector(data) : no method for coercing this S4 class to a vector Any ideas on

Combining S4 and S3 methods in a single function

↘锁芯ラ 提交于 2019-12-28 04:08:29
问题 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 <-