Questions tagged [r-s3]

One of the object oriented systems in the R language.

S3 is the main object oriented system in R.
Read more about it here

Base R uses S3.
The other two common object oriented systems are S4 and R5.

149 questions
3
votes
1 answer

Is rep really a generic?

When I type the function name of a generic in to my console, I expect to see a call to UseMethod. For example, the documentation for determinant calls it a generic and I get the following output when I type it in to my console: >…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
3
votes
2 answers

Error in getMethod("summary", signature = "FitDiff")

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 …
hamagust
  • 728
  • 2
  • 10
  • 28
3
votes
1 answer

R S4 classes with the same name from different packages

Suppose that there are two packages. Package_A has this class: setClass("Person", slots = c( name = "character", age = "numeric" ) ) setGeneric("age", function(x) standardGeneric("age")) setMethod("age",…
3
votes
1 answer

Where shall one put the ellipsis in S3 function overloads?

We have an S3 class for which we define plot and other generic functions. We are not sure where the ... has to go. There are two options: plot.hadronacf(x, col = "black", ...) plot.hadronacf(x, ..., col = "black") Similarly also for print. and…
Martin Ueding
  • 8,245
  • 6
  • 46
  • 92
3
votes
2 answers

Method dispatch for functions inside dplyr::do

How would I implement method dispatch for a function inside of dplyr::do? I've read through GitHub issues #719, #3558 and #3429 which have helpful information on how to create methods for dplyr verbs, but nothing in particular that works for…
Rappster
  • 12,762
  • 7
  • 71
  • 120
3
votes
2 answers

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

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)…
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
3
votes
0 answers

Define method for basic class

Why does this work print.character = function(x) cat("Use cat instead!", x) print("tre") # Use cat instead! tre but this throws an error? "+.character" = function(lhs, rhs) paste0(lhs, " + ", rhs) "tre" + "tri" Error in "tre" + "tri" :…
jakub
  • 4,774
  • 4
  • 29
  • 46
3
votes
1 answer

Export S3 method for a 'function' class object

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, ...){ …
jangorecki
  • 16,384
  • 4
  • 79
  • 160
3
votes
1 answer

R: Creating a generic function for ‘split’ from package ‘base’ in the global environment

For simplicity, I will use the following sample code :) I had defined a S4 class test, and then as usual I adopted setMethod to write generic function split for class test: # define a S4 class setClass( Class="test", …
Shiqing Fan
  • 688
  • 2
  • 8
  • 14
3
votes
1 answer

Making subclass of list

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" >…
Petr Matousu
  • 3,120
  • 1
  • 20
  • 32
3
votes
2 answers

How do I dispatch cat in R S3?

> foo <- structure(list(one=1,two=2), class = "foo") > cat(foo) Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be handled by 'cat' OK I'll add this to the generic cat: >…
Jeremy Leipzig
  • 1,914
  • 3
  • 21
  • 26
3
votes
1 answer

Creating an S4 Method named `Median`

Here, the top answers describes a good way to make a method dispatch on both S3 and S4 objects. However, that way only works when both methods have the same signature. Is there a way to create an S4 method for median with a different signature? For…
Jon Claus
  • 2,862
  • 4
  • 22
  • 33
3
votes
2 answers

Default element of a S3 class

I wondered about S3 classes in R, if there is an option to define a default output element and keep the remaining elements kind of hidden. As an example, lets say we have a toy function that calculates certain things and reports them back as a S3…
Daniel Fischer
  • 3,280
  • 1
  • 18
  • 29
2
votes
2 answers

Call overridden s3 method from subclass (R.oo / R.methodsS3)

I'm using setMethodS3 in package R.methodsS3 to create an S3 method. Lets say I have two classes, class Parent and class Child (R.oo object). class Child inherits from class Parent. Both have the method MyMethod(). How do I call superclass…
Suraj
  • 35,905
  • 47
  • 139
  • 250
2
votes
1 answer

R: plot: Error in as.double(x): cannot coerce type 'S4' to vector of type 'double'

Apologies that this is 6 days after a similar post, however I suspect the root cause may be different. Another similar post says to ensure the plotting package is library'd but it should be (discussed below) and worked fine until this afternoon. I…
dez93_2000
  • 1,730
  • 2
  • 23
  • 34