r-s3

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

Export S3 method for a 'function' class object

时光总嘲笑我的痴心妄想 提交于 2021-01-27 15:15:56
问题 Function objects seems to work well with dispatching of S3 methods. But for some reason they cannot be exported in NAMESPACE file. Below code works with dispatching to *.function method: as.abc = function(x, ...){ UseMethod("as.abc") } as.abc.list = function(x, ...){ stopifnot(is.list(x)) structure(x, class="abc") } as.abc.function = function(x, ...){ stopifnot(is.function(x)) structure(x, class="abc") } # list l = as.abc(list(1)) str(l) #List of 1 # $ : num 1 # - attr(*, "class")= chr "abc"

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

Documenting generic S3 methods in R so that user can see documentation?

喜欢而已 提交于 2020-06-29 03:36:03
问题 I am extending the print() function by a method print.dmdSchemeSet() . This functions has additional arguments which I obviously have documented in the source files using roxygen2: #' Generic print function for \code{dmdSchemeSet} #' #' @param x object of type \code{dmdSchemeSet} #' @param ... additional arguments - not used here #' @param printAttr default \code{TRUE} - attributes are printed #' @param printExtAttr default \code{FALSE} - additional attributes are not printed (e.g. \code

Documenting generic S3 methods in R so that user can see documentation?

走远了吗. 提交于 2020-06-29 03:35:17
问题 I am extending the print() function by a method print.dmdSchemeSet() . This functions has additional arguments which I obviously have documented in the source files using roxygen2: #' Generic print function for \code{dmdSchemeSet} #' #' @param x object of type \code{dmdSchemeSet} #' @param ... additional arguments - not used here #' @param printAttr default \code{TRUE} - attributes are printed #' @param printExtAttr default \code{FALSE} - additional attributes are not printed (e.g. \code

function to return all S3 methods applicable to an object

丶灬走出姿态 提交于 2020-02-14 05:46:13
问题 Has anyone put together/found a good method for listing all the S3 methods available for a given object ? The built-in methods() function will give all available methods for a specified class, or for a specified generic function, but not for an object. The example I have in mind is a glm object, which is of (minor?) class "glm" but also inherits from "lm" g <- glm(y~x,data=data.frame(x=1:10,y=1:10)) class(g) ## [1] "glm" "lm" There are 35 methods for class "lm" and 22 for "glm". I'm

Proper way to implement S3 dispatch on R6 classes

流过昼夜 提交于 2020-01-23 04:53:10
问题 I have an R6 class and I want to add an S3 method for it. The documentation I found mentioned briefly that in order to use S3 dispatch on R6 you must have class = TRUE , but I couldn't find an example of how it should be done. I did see empirically that simply writing an S3 method in the form s3generic.r6class worked, but I wanted to know if that is indeed to right way to write an S3 method for R6 . For example, say I have an R6 class that enhances a list library(R6) R6list <- R6Class(

R S3 class: Decide between overwriting vs appending the class name of the class attribute

你离开我真会死。 提交于 2019-12-23 20:45:21
问题 I want to create an S3 class. How do I decide which way of setting the class attribute is the right one (since it makes a difference)? 1) Overwrite the class attribute object <- data.frame(field1 = "a", field2 = 2) class(object) # [1] "data.frame" class(object) <- "MyClass" # set the class by overwriting the existing one class(object) # [1] "MyClass" 2) Append the class attribute I could also append the class name (at the beginning or end): object2 <- data.frame(field1 = "a", field2 = 2)

Making subclass of list

非 Y 不嫁゛ 提交于 2019-12-22 09:49:45
问题 I have the following code obj <- list(list(a=4,f=5,g=5),list(a=44,f=54,g=54)) class(obj) <- "mysubclass" class(obj[1]) class(obj[2]) class(obj[1:2]) class(obj) resulting in: > class(obj[1]) [1] "list" > class(obj[2]) [1] "list" > class(obj[1:2]) [1] "list" > class(obj) [1] "mysubclass" What would be proper solution to not lose the class by subsetting? FOr example, class(obj[1:2]) results in mysubclass and still behaves as a list. 回答1: The problem is that the generic [ method is stripping off