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

Dispatch a custom method on S3*data.frame

I would like to define my own behavior (method) for the multiplication of a data.frame with an object of a new S3 class. But I can't figure out how to get methods dispatch to find my method. Is there a way? First, I define S3 objects 'a' (oldClass…
pangia
  • 1,164
  • 10
  • 11
6
votes
4 answers

Modify S3 object without returning it?

I'm new to object-oriented programming in R and struggle with how to properly write a function that modifies an object. This example works: store1 <- list( apples=3, pears=4, fruits=7 ) class(store1) <- "fruitstore" print.fruitstore <-…
Chris
  • 2,256
  • 1
  • 19
  • 41
5
votes
1 answer

How can I define an S3 method for an unevaluated assignment expression?

tl;dr: R CMD check complains when I implement a generic for the S3 class <-, because it thinks that the function is a replacement function with incorrect arguments. I need to define a set of S3 generics to walk an AST of unevaluated R…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
5
votes
2 answers

Unexpected log2 error when defining S3 "Math" group generics for a new class

I am trying to use S3 "Math" group generics for a custom class. However I am getting a strange result: log() works while log2 and log10 produces errors. Below is a minimal example: # simple class with just the new name lameclass <- function(x) { …
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
5
votes
1 answer

What are these square brackets for S3 classes?

I obtained this from an open source repo on git. This shows the writing of generic and methods for S3 classes. But I do not understand the notations or conventions that the functions are being assigned to. The following are my questions: The use…
Justin Thong
  • 321
  • 3
  • 12
5
votes
2 answers

checking S3 generic/method consistency ... WARNING

I have already read following two discussion: Roxygen2 - how to properly document S3 methods S3 method consistency warning when building R package with Roxygen And following two…
David Lee
  • 129
  • 1
  • 9
5
votes
2 answers

How do you extend rbind for a data.frame subclass?

My question is how do you extend rbind() to work with a data.frame subclass? I cannot seem to properly extend rbind() to work with even a very simple subclass. The following example demonstrates the issue: Subclass and method definition: new_df2 <-…
r_alanb
  • 873
  • 8
  • 21
5
votes
2 answers

is.object and the S3 class system

Using the class function allows us to determine the class of an object: > x = 5 > class(x) [1] "numeric" I also understand that we can use the is.object command to determine if an object has a class. However some object types are implicit, that…
csgillespie
  • 59,189
  • 14
  • 150
  • 185
5
votes
2 answers

S3 operator overloading for multiple classes

I defined two classes that can successfully add two of their own objects or a number and one of their own objects. a <- structure(list(val = 1), class = 'customClass1') b <- structure(list(val = 1), class = 'customClass2') `+.customClass1` <-…
takje
  • 2,630
  • 28
  • 47
4
votes
1 answer

How to override default S3 function in R?

I want to override the default predict.lm function due to a bug: library(datasets) # Just a regular linear regression fit <- lm(mpg~disp+hp+wt+drat, data=mtcars) termplot(fit, terms=2, se=T) Gives this error: Error in predict.lm(model, type =…
Max Gordon
  • 5,367
  • 2
  • 44
  • 70
4
votes
2 answers

How do I conditionally provide S3 methods for S3 generics from another package?

I'm making a package for data manipulation that uses some other libraries under the hood. Let's say that my data always has a class "custom" and that I have a function custom_select() to select some columns. I would like my package to have few…
bretauv
  • 7,756
  • 2
  • 20
  • 57
4
votes
1 answer

How to make a generic R function inherit the class of its input?

Let the following code: x <- 1:5 class(x) <- "bar" foo <- function(x) { UseMethod("foo") } foo.bar <- function(x) { y <- max(x) return(y) } I would like the output of foo(x) to have the same class as x (i.e., "bar"). The obvious way…
Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
4
votes
2 answers

How to override the implementation of a non-generic function in R

In R, I have an S3 class that emulates a 1D vector, and so I want to implement custom versions of mean, sum, max, etc. Let's say it looks like this: my_class = function(){ structure(list(), class='my_class') } All the above methods work fine, if…
Migwell
  • 18,631
  • 21
  • 91
  • 160
4
votes
1 answer

How to adjust j in `[.data.table` without breaking data.table's custom evaluation?

I'm trying to extend data.table to speed up/standardize analyses of complex survey designs. To do so, I'm trying to add a light layer on top of [.data.table where I intercept the call in j and in a few circumstances replace the operation (e.g. the…
4
votes
2 answers

Importing a package's S3 methods without importing its functions

I have the following dependency chain in a package I'm developing: My package uses a class (trajectory) defined in package A (simmer). It also uses an S3 method for that class (plot.trajectory), which is defined in package B (simmer.plot). I can…
1 2
3
9 10