One of the methods of object oriented programming in the R language.
Questions tagged [r-s4]
647 questions
25
votes
2 answers
How to define the subset operators for a S4 class?
I am having trouble figuring out the proper way to define the [, $, and [[ subset operators for an S4 class.
Can anyone provide me with a basic example of defining these three for an S4 class?

Kyle Brandt
- 26,938
- 37
- 124
- 165
22
votes
4 answers
Is it bad practice to access S4 objects slots directly using @?
This one is almost a philosophical question: is it bad to access and/or set slots of S4 objects directly using @?
I have always been told it was bad practice, and that users should use "accessor" S4 methods, and that developers should provide their…

Pierre
- 1,015
- 1
- 9
- 19
21
votes
3 answers
How to properly document S4 "[" and “[<-“ methods using roxygen?
Below I posted a mini example in which I want do write documentation for
an “[“ method for a S4 class. Does someone know how to properly document a method for the generic "[" using roxygen and S4?
I get a warning when checking the package after…

Mark Heckmann
- 10,943
- 4
- 56
- 88
21
votes
1 answer
S4 Classes: Multiple types per slot
Is it possible to create an S4 class, where one or more of the slots can be of multiple classes? For example. Let's say that you had a situation where data could be either a vector, or a data.frame.
exampleClass <- setClass("exampleClass",
…

Brandon Bertelsen
- 43,807
- 34
- 160
- 255
16
votes
1 answer
How to use data.table as super class in S4
In the R-Package data.table the manual entry for ?data.table-class says that 'data.table' can be used for inheritance in a class definition, i.e. in the contains argument in a call to setClass:
library("data.table")
setClass("Data.Table", contains =…

Sebastian
- 840
- 6
- 11
16
votes
1 answer
Inheritance in R
With regards to R, Can someone explain to me, with regards to object inheritance, if I have S4 object X, which contains Y, if Y has an initializer, how can that initializer be called from within the initializer of X, when X is constructed.

Nicholas Hamilton
- 10,044
- 6
- 57
- 88
14
votes
1 answer
How to add class-specific alias without generic alias using Roxygen2?
A simple example is that I have created an extension to show, which is a S4 base method. I don't want to cause a disambiguation fork by re-documenting show in my package, and I also want to consolidate the documentation of my extension to show in…

Paul 'Joey' McMurdie
- 7,295
- 5
- 37
- 41
14
votes
1 answer
Combining S4 and S3 methods in a single function
What is a good way of defining a general purpose function which should have implementations for both S3 and S4 classes? I have been using something like this:
setGeneric("myfun", function(x, ...){
…

Jeroen Ooms
- 31,998
- 35
- 134
- 207
13
votes
1 answer
Importing S4 functions from the Matrix package
The Matrix package defines a whole bunch of S4 methods for multiplying matrices, that are dispatched by the S4 generic functions %*%, crossprod, and tcrossprod.
How do I import the "%*%" methods, for use in my own package? This piece of code fails…

Zach
- 29,791
- 35
- 142
- 201
12
votes
2 answers
sum of S4 objects in R
I have a S4 class and I would like to define the linear combination of these objects.
Is it possible to dispatch * and + functions on this specific class?

RockScience
- 17,932
- 26
- 89
- 125
12
votes
1 answer
Is there an S4 equivalent to unlist()?
I have some experience working with S4 objects and their slots, so I know how to access specific slots and sub-slots. What I'd like to learn is how to "de-slotify" an object in the way that unlist takes apart an S3 list.
My immediate goal is to…

Carl Witthoft
- 20,573
- 9
- 43
- 73
11
votes
2 answers
How to set default value of a slot as NULL in R?
I'm new to R.
I'm trying to define a class similar to a tree node, that is , it has a left node and right node, which should be of the same class as the parent node. So I define the class as follows:
setClass('Node',…

Derrick Zhang
- 21,201
- 18
- 53
- 73
11
votes
2 answers
dispatching S4 methods with an expression as argument
I'm trying to convince an S4 method to use an expression as an argument, but I always get an error returned. A trivial example that illustrates a bit what I'm trying to do here…

Joris Meys
- 106,551
- 31
- 221
- 263
11
votes
1 answer
Generalizing `...` (three dots) argument dispatch: S4 methods for argument set including `...`
Actual question
Is it possible to define methods for a set of signature arguments that includes ... (as opposed to exclusively for ...)? It's not possible "out-of-the-box", but would it theoretically be possible at all (involving some tweaks) or is…

Rappster
- 12,762
- 7
- 71
- 120
11
votes
1 answer
Inspect S4 methods
How can I view the definition of a S4 function? For instance, I would like to see the definition of TSconnect in package TSdbi. The command
showMethods("TSconnect")
reveals that there is, among others, a function for drv="histQuoteDriver",…

Karsten W.
- 17,826
- 11
- 69
- 103