One of the methods of object oriented programming in the R language.
Questions tagged [r-s4]
647 questions
7
votes
3 answers
Formatting and manipulating a plot from the R package "hexbin"
I generate a plot using the package hexbin:
# install.packages("hexbin", dependencies=T)
library(hexbin)
set.seed(1234)
x <- rnorm(1e6)
y <- rnorm(1e6)
hbin <- hexbin(
x = x
, y = y
, xbin = 50
, xlab = expression(alpha)
, ylab =…

R_User
- 10,682
- 25
- 79
- 120
7
votes
3 answers
Reference Classes, tab completion and forced method definition
I am currently writing a package using reference classes. I have come across
an issue which from reading various sources:
Method initialisation in R reference classes
Can't reliably use RefClass methods in Snowfall
I gather is caused because…

jdharrison
- 30,085
- 4
- 77
- 89
7
votes
2 answers
S3 style dispatching for S3 objects using formal method definitions
Related to this question, but slightly different and hopefully more clear.
I am looking for a clean way to formally register methods for both S4 and S3 classes, but without relying on the terrible S3-dot-naming-scheme for dispatching. An…

Jeroen Ooms
- 31,998
- 35
- 134
- 207
7
votes
1 answer
Useful book(s) on learning Object Oriented Programming in R?
I was reading an interesting post on R-bloggers on ''Object Oriented Programming in R using S4 Classes". The book "Statistics and Computing" written by Venables and Ripley has some chapters introducing S3 classes and S4 classes in S and R and have…

Sam
- 4,357
- 6
- 36
- 60
6
votes
1 answer
Example of Using an S3 Class in a S4 Object
I want to include an RODBC connection as part of S4 object. It looks like RODBC is S3. For example:
setClass(
Class="Node",
representation=representation(
nodeName = "character",
connection = "RODBC"
)
)
Throws…

Kyle Brandt
- 26,938
- 37
- 124
- 165
6
votes
1 answer
How to get the list of class that have a common S4 superclass in R
In R, how do I get the list of subclass of a S4 superclass?
I found showClass("mySuperClass",complete=FALSE) but it only prints the result. I would like to store it in a vector to use it.

RockScience
- 17,932
- 26
- 89
- 125
6
votes
1 answer
Efficient way to define a class with multiple, optionally-empty slots in S4 of R?
I am building a package to handle data that arrives with up to 4 different types. Each of these types is a legitimate class in the form of a matrix, data.frame or tree. Depending on the way the data is processed and other experimental factors, some…

Paul 'Joey' McMurdie
- 7,295
- 5
- 37
- 41
6
votes
2 answers
S4 constructors and prototypes
Looking through Hadley Wickham's S4 wiki:
https://github.com/hadley/devtools/wiki/S4
setClass("Person", representation(name = "character", age = "numeric"),
prototype(name = NA_character_, age = NA_real_))
hadley <- new("Person", name =…

Jeremy Leipzig
- 1,914
- 3
- 21
- 26
6
votes
2 answers
R: How to truly remove an S4 slot from an S4 object (Solution attached!)
Let's say I define an S4 class 'foo' with two slots 'a' and 'b', and define an object x of class 'foo',
setClass(Class = 'foo', slots = c(
a = 'numeric',
b = 'character'
))
x <- new('foo', a = rnorm(1e3L), b = rep('A', times =…

tingtingzhan
- 63
- 5
6
votes
1 answer
Is it possible to have an S3 slot in an S4 class?
I'm wondering how to include an S3 object as a data member in an S4 object, i.e. using composition instead of inheritance. Here's my code snippet.
library(randomForest)
set.seed(1337)
setClass("TestManager", slots = c(
hp = "numeric",
rfObj…

biubiuty
- 493
- 6
- 17
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
1 answer
General way to transform S4 object to dataframe (R)
I have a number of S4 objects with a variable number of slots and slotnames. The data in each slot is the same length. For example the S4 object peaks:
> str(peaks)
Formal class 'MassPeaks' [package "MALDIquant"] with 4 slots
..@ snr : num…

R Greg Stacey
- 425
- 4
- 15
6
votes
1 answer
Saving S4 objects in a list of list
R is giving the following message error when you want to save an S4 object into a list of list and the element was not already defined previously.
"invalid type/length (S4/0) in vector allocation"
Why is it working with a simple list, but not with a…

Arnaud
- 377
- 1
- 2
- 11
6
votes
1 answer
S4 object with a pointer to a C struct
I have a third-party C library I am using to write an R extension. I am required to create a few structs defined in the library (and initialize them) I need to maintain them as part of an S4 object (think of these structs as defining to state of a…

pbhowmick
- 1,093
- 11
- 26
6
votes
1 answer
How to define S4 method for taking the opposite of the object?
Let's say I have an S4 class called testClass. The contents are irrelevant the purpose of this question but let's make it contain a numeric value.
#' An S4 class that stores a list.
#' @export
setClass("testClass",
…

cdeterman
- 19,630
- 7
- 76
- 100