Questions tagged [r-s4]

One of the methods of object oriented programming in the R language.

647 questions
11
votes
3 answers

Converting package using S3 to S4 classes, is there going to be performance drop?

I have an R package which currently uses S3 class system, with two different classes and several methods for generic S3 functions like plot, logLik and update (for model formula updating). As my code has become more complex with all the validity…
Jouni Helske
  • 6,427
  • 29
  • 52
11
votes
2 answers

How to create a dataframe of user defined S4 classes in R

I want to create a data.frame of different variables, including S4 classes. For a built-in class like "POSIXlt" (for dates) this works fine: as.data.frame(list(id=c(1,2), …
Patrick Roocks
  • 3,129
  • 3
  • 14
  • 28
10
votes
2 answers

Getting a slot's value of S4 objects?

So I have a spatialpolygons object in R; but I am not sure why I am unable to retrieve the "area" slot from it. Here's my R session: > spatialpolygons An object of class "SpatialPolygons" Slot "polygons": [[1]] An object of class "Polygons" Slot…
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
10
votes
1 answer

How can I write a `%*%` method for a base matrix S3 subclass?

I'd like to write a %*% method for a base matrix subclass. My subclass is an S3 class and the documentation of help("%*%") says that %*% is a S4 generic and that S4 methods need to be written for a function of two arguments named x and y. I've…
Trevor
  • 434
  • 4
  • 8
10
votes
2 answers

What is setReplaceMethod() and how does it work?

I'm confused about the usage of setReplaceMethod(). Looking at ?setReplaceMethod does not provide an explanation and Googling is less than helpful. QUESTION : Please explain setReplaceMethod(), it's usage and how it works (preferably with an…
irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60
10
votes
1 answer

What are S1 and S2 classes?

In R there are S3, S4 and with R 2.12, reference classes (unofficially called S5 classes). Is there such a thing as a S1 (or S2) class? If so, what are they? If not, why start with S3?
csgillespie
  • 59,189
  • 14
  • 150
  • 185
10
votes
2 answers

printing a data.frame that contains a list-column of S4 objects

Is there a general problem with printing a data.frame when it has a list-column of S4 objects? Or am I just unlucky? I ran across this with objects from the git2r package but maintainer Stefan Widgren points out this example from Matrix as well. I…
jennybryan
  • 2,606
  • 2
  • 18
  • 33
10
votes
1 answer

Why does print of S4 class call `show` without namespacing it?

I have a package shinyjs with a function called show. Today a user reported to me that this introduces problems when using S4 objects because "print"-ing an S4 object uses the show method, which is masked by my package when it's…
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
10
votes
1 answer

How to implement subset replacement for S4 methods

I'm writing an S4 class in which the internal data is stored in a database and the class is mostly a gatekeeper for accessing and modifying information in the database. The class would have methods such as getInfoA and getInfoA<- for extracting and…
ThomasP85
  • 1,624
  • 2
  • 15
  • 26
10
votes
1 answer

How to create a sub-class of data.frame with additional features

I want to create a class that is pretty much a data frame, with a couple of enhancements (extra functions, extra properties), and am wondering what the best way to do it is. The class is basically a data frame, but with some additional attributes…
prabhasp
  • 528
  • 3
  • 18
10
votes
2 answers

Is S4 method dispatch slow?

My S4 class has a method that is called many times. I noticed that the execution time is much slower than it would be if a similar function was called independently. So I added a slot with type "function" to my class and used that function instead…
Soldalma
  • 4,636
  • 3
  • 25
  • 38
10
votes
2 answers

overloading operators when using setOldClass not works as wanted compared to an S4 class

Can someone explain why overloading an operator on an old-style S3 class which is registered does not work as expected while when defining a new class and overloading the operators does work. As shown in the following examples. This does not…
user1600826
10
votes
1 answer

How to specify in which order to load S4 methods when using roxygen2

I ran into following problem already multiple times. Say you have two classes, classA and classB described in the following files classA.R : #' the class classA #' #' This is a class A blabla #' \section{Slots}{\describe{\item{\code{A}}{a…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
9
votes
1 answer

Programmatically distinguishing S3 and S4 objects in R

If given an object x, is there a way to classify whether or not it is S3 or S4 (or "other")? I have looked at is.object() and isS4(), and can identify that something is an object (or not) and that it is an S4 object (or not). However, it doesn't…
Iterator
  • 20,250
  • 12
  • 75
  • 111
9
votes
0 answers

R: Documenting ellipsis arguments with roxygen2 in S4 generic

I have a number of functions that have related methods with different arguments that I want to document together within the generic. Is there a standard method for documenting arguments passed through an ellipsis to the different methods by an S4…
ssokolen
  • 468
  • 3
  • 13
1 2
3
43 44