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
8
votes
1 answer

type/origin of R's 'as' function

R's S3 OO system is centered around generic functions that call methods depending on the class of the object the generic function is being called on. The crux is that the generic function calls the appropriate method, as opposed to other OO…
PejoPhylo
  • 469
  • 2
  • 11
8
votes
3 answers

Using sd as a generic function in R

If I a have a class called foo, then it is straightforward to overload the summary function summary.foo = function(x, ...) print("bar") However this technique does not work with the sd function, that is > bar = createFooClass() > sd.foo =…
csgillespie
  • 59,189
  • 14
  • 150
  • 185
8
votes
2 answers

defining custom dplyr methods in R package

I have a package with custom summary(), print() methods for objects that have a particular class. This package also uses the wonderful dplyr package for data manipulation - and I expect my users to write scripts that use both my package and…
Andrew
  • 9,090
  • 8
  • 46
  • 59
8
votes
3 answers

emulating multiple dispatch using S3 for "+" method - possible?

I have two classes (a and b) and I want to define the + method for them. I need different methods for the four possible combinations of the two classes, i.e.: a + a method 1 a + b method 2 b + a method 3 b + b method 4 I know I could use S4 for…
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
7
votes
2 answers

How to write a c() function for custom S3 class in R

I'm writing an S3 class in R that is just an integer with some attributes attached to it. If x1 and x2 are objects of this class (call it "myclass"), then I would like c(x1, x2) to return a vector of myclass objects with the original class…
Abiel
  • 5,251
  • 9
  • 54
  • 74
7
votes
1 answer

Operator overload stops working in R package

I have a container class that is basically a list. Because I wanted to support subsetting, I have overloaded the subset [ operator (likely poorly implemented). #' Constructor for spectra object .spectra = function(n_spectrum = 0) { object …
dudu
  • 675
  • 6
  • 15
7
votes
3 answers

Show methods associated with a particular class

I'd like to see a list of all methods associated with a certain object class. E.g. if I see that some model fit routine returns an object of class "foo", I'd like to know if the package (or any other package) have defined methods such as…
cboettig
  • 12,377
  • 13
  • 70
  • 113
6
votes
2 answers

Why do packages loaded inside `test_that()` provide their methods outside of `test_that()` and how can I prevent that?

In my understanding, anything put inside test_that() should be compartmentalized, meaning that if I load a package in test_that(), its functions and methods shouldn't be available in other test_that() calls. In the example below, there are 3 (empty)…
bretauv
  • 7,756
  • 2
  • 20
  • 57
6
votes
1 answer

matrix losing class attribute in R

Consider the following code: A <- matrix(1:12, ncol=4) colnames(A) <- letters[1:4] class(A) <- c("foo", "matrix") when A is subset, it loses the "foo" class label: class(A[1:2,]) # [1] "matrix" The same happens with vectors. Yet, the same doesn't…
gappy
  • 10,095
  • 14
  • 54
  • 73
6
votes
2 answers

R S3 classes - changing class and pass back to same method

I have a question about the "right" way to do something with S3 classes in R. What I want to do is have a method that changes the class and then calls the same method on the new class. Something like this: my_func <- function(.x, ...) { …
seth127
  • 2,594
  • 5
  • 30
  • 43
6
votes
1 answer

How do you get S3 methods to work with S4 objects?

I'm writing an S3 method that I want to work with any R object, including S4 objects. The first thing I don't understand is that S4 classes don't appear to derive from an S4 base class, so given f <- function(x) UseMethod("f") I can't just declare…
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
6
votes
2 answers

Stuck with definition of S3 method for autoplot

I'm stuck with defining S3 method for autoplot. I have the following (full code here): #' Autoplot for bigobenchmark object #' #' @importFrom ggplot2 autoplot #' #' @param object #' #' @return A ggplot2 plot #' @export #' #' @examples #' # Create…
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
6
votes
1 answer

How not to overwrite generic methods

I am trying to get my head around the s3 class system in R. The documentation says that I need to create a generic function for the method I want to create. Let's say I want to create a method foo for class XYZ. With all the packages R has, how can…
Stephen Lien
  • 221
  • 1
  • 2
  • 3
6
votes
2 answers

function to return all S3 methods applicable to an object

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…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
6
votes
1 answer

Assigning multiple values to an environment at once

Given an environment x, a convenient shorthand for assign(x, value, envir = e) is to write e[[x]] <- value. Currently, there is not an analog to the subset operator for assigning multiple objects at once: > e = new.env(parent = emptyenv()) >…
Jon Claus
  • 2,862
  • 4
  • 22
  • 33
1
2
3
9 10