One of the methods of object oriented programming in the R language.
Questions tagged [r-s4]
647 questions
7
votes
2 answers
how to set value in S4 object r using a method (no input value needed)
This is a two part Question. I want to set the value of a prototype s4 object based on a different slots value and alternatively I want to implement this as a method.
I have an object I am trying to create. It has some slots. I would like to set a…

channon
- 362
- 3
- 16
7
votes
3 answers
S4 missing or NULL arguments to methods?
Is there a way to define what happens when the argument of a method is missing or NULL?
In the example below, I'd like to call the same function no matter whether I type foo() or foo(NULL). Of course I know that I can have a method…

Florian Bw
- 746
- 1
- 7
- 16
7
votes
3 answers
S4 class [ (subset) inheritance with additional arguments
This is an extension of Using callNextMethod() within accessor function in R.
Update 2017-03-25: To illustrate how this only fails when loading the methods, but not when it's in a built package, I created a dummy package:…

ZNK
- 2,196
- 1
- 19
- 25
7
votes
1 answer
R S4 setMethod '[' distinguish missing argument?
Apologies for the title, not sure how else to phrase this question.
If I want to create setMethod on a class how can I distinguish between the similar cases of mat[i,] and mat[i]?
I know for the former I can use:
setMethod("[",
signature(x =…

cdeterman
- 19,630
- 7
- 76
- 100
7
votes
1 answer
Error: $ operator not defined for this S4 class
I'm trying to make a formula and I got the error:
$ operator not defined for this S4 class with R.
First of all, what is a S4 class? What am I doing wrong?
Following the code:
as.formula("ctree(d$sex ~ d$ahe , data = d)")
If you want to reproduce…

Reinaldo Maciel
- 73
- 1
- 1
- 3
7
votes
2 answers
S4 documentation of "[" with 'missing' arguments
This question is very similar to this question but when I try the answer I receive an addition 'NOTE' following R CMD check. Although it is just a NOTE I would really like to have a completely clean check.
* checking Rd line widths ... NOTE
Error:…

cdeterman
- 19,630
- 7
- 76
- 100
7
votes
1 answer
S4 classes: arguments passed to new() don't go into their slots
I'm building an R package with S4 classes, and I'm having trouble with the new function. I have a class called Configs
setClass("Configs",
slots = list(
burnin = "numeric",
chains = "numeric",
features = "numeric",
iterations =…

landau
- 5,636
- 1
- 22
- 50
7
votes
1 answer
does every S4 needs to be generic
Suppose we have the following dummy class
Foo <- setClass(Class = "Foo",slots = c(foo = "numeric"),
prototype = list(foo = numeric())
I thought, generics are used to overload different functions. So assume we want to implement an…

math
- 1,868
- 4
- 26
- 60
7
votes
1 answer
R: when to use setGeneric or export a s4 method in the namespace
I am writing a small R package with the idea to submit it to Bioconductor in the future, which is why I decided to try out s4 classes. Unfortunately I had problems understanding when I should use setGeneric or not in my package, and the…

JavaNewbie
- 241
- 3
- 6
7
votes
1 answer
Proper way to use cbind, rbind with s4 classes in package
I have written a package using S4 classes and would like to use the functions rbind, cbind with these defined classes.
Since it does not seem to be possible to define rbind and cbind directly as S4 methods I defined rbind2 and cbind2…

user625626
- 1,102
- 2
- 10
- 16
7
votes
1 answer
Using callNextMethod() within accessor function in R
This is related to the following post Problems passing arguments with callNextMethod() in R
I am writing accessors for two S4 classes, 'foo' and 'bar'. 'bar' inherits from foo and is extended only by a few slots. Instead of writing a full accessor…

Maxence
- 83
- 2
7
votes
1 answer
How to catch warnings sent during S4 method selection
Warnings generated when evaluation arguments of S4 generic functions can not be caught using withCallingHandlers().
Illustration of the normal behaviour of withCallingHandlers:
### simple function that sends a warning
send_warning <- function() {
…

Karl Forner
- 4,175
- 25
- 32
7
votes
4 answers
How can I get a list of all methods defined on an S4 class in R?
Is there a way in R to get a list of all methods defined on an S4 class, given the name of that class?
Edit: I know that showMethods can show me all the methods, but I want to manipulate the list programmatically, so that's no good.

Ryan C. Thompson
- 40,856
- 28
- 97
- 159
7
votes
1 answer
Add extra arguments to implicit S4 generic for a primitive function
Take the function names: that's a primitve function in R. For primitive functions, an implicit S4 generic is created, so it is possible to construct S4 methods for that function.
Take an S4 class defined as follows :
setClass("aClass",
…

Joris Meys
- 106,551
- 31
- 221
- 263
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