One of the methods of object oriented programming in the R language.
Questions tagged [r-s4]
647 questions
9
votes
1 answer
R texreg: How can I select the gof statistics to be displayed?
I'm using texreg to produce output tables for panel regressions with plm. I would like to surpress the output of all gof statistics. That is rather than showing R2, adj R2 and N. I would like to only show adj R2. Does anyone know of a simple way…

Andy
- 295
- 1
- 8
9
votes
1 answer
What's the difference between setMethod("$<-") and set setReplaceMethod("$")?
Question
When programming in r with the s4 OOP system, when one have to use setReplaceMethod? I don't see what's the difference with the setMethod when adding <- to the name of the function. Does setMethod("$<-") and setReplaceMethod("$") are…

jomuller
- 1,032
- 2
- 10
- 19
9
votes
1 answer
Automatically document all methods of an S4 generic, using roxygen2
I am writing an r package using roxygen2 for the documentation. I am having some trouble documenting S4 methods. I have defined a generic s4 method (e.g. myGeneric) and few methods that implement it.
Question: Is there a way to automatically…

alko989
- 7,688
- 5
- 39
- 62
9
votes
1 answer
Converting spatial polygon to regular data frame without use of gpclib tools
I working with spatial data in R for a commercial application and would like to use ggplot2 for data visualization. If you run the Hadley's example at https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles you find that in order to run…

aaron
- 6,339
- 12
- 54
- 80
9
votes
4 answers
R code examples/best practices
I'm new to R and having a hard time piecing together information from various sources online related to what is considered a "good" practice with writing R code. I've read basic guides but I've been having a hard time finding information that is…

Bob Albright
- 2,242
- 2
- 25
- 32
9
votes
1 answer
Dispatching an argument's default value from an S4 generic function to its associated methods
Suppose all of your S4 methods associated to a specific S4 generic function/method share a formal argument that is supposed to have a specific default value. Intuitively, I would state such an argument in the definition of the S4 generic (as opposed…

Rappster
- 12,762
- 7
- 71
- 120
8
votes
2 answers
S4 method fails when also set in another package
I have encountered a curious problem when I try to set a method that was also defined in another package. An example package which demonstrates this problem can be found here.
The key is the typeof method that I try to set. I use the setMethod…

cdeterman
- 19,630
- 7
- 76
- 100
8
votes
1 answer
dputting an S4 object
How would a person dput() an S4 object? I tried this
require(sp)
require(splancs)
plot(0, 0, xlim = c(-100, 100), ylim = c(-100, 100))
poly.d <- getpoly() #draw a pretty polygon - PRETTY!
poly.d <- rbind(poly.d, poly.d[1,]) # close the polygon…

Roman Luštrik
- 69,533
- 24
- 154
- 197
8
votes
1 answer
Unary plus for S4 class in R
I am experimenting with S4 classes in R and I was trying to define a plus (+) operator for my objects, i.e. overload the plus operator. I managed to overload the binary +, but I cannot figure out how to overload the unary plus. Here is a minimal…

Gumeo
- 891
- 1
- 13
- 26
8
votes
2 answers
How can I remove all custom methods and classes from an R workspace?
I've been experimenting a lot with S4 classes lately, and it is a pain to restart R in order to clear all class definitions and custom methods from my workspace. Obviously rm(list=ls(all.names=TRUE)) is of no use. I could manually remove all classes…

Will Beason
- 3,417
- 2
- 28
- 46
8
votes
3 answers
Make S4 object act as an S3 class?
I would like to write an S4 object such that it can be passed to methods that only take an S3 object. (It seems like setOldClass() might be related to this but it's not clear to me from the documentation?)
e.g. for a minimal example imagine I have…

cboettig
- 12,377
- 13
- 70
- 113
8
votes
1 answer
Avoiding consideration of enclosing frames when retrieving field value of a S4 Reference Class
I'm a huge fan of S4 Reference Classes as they allow for a hybrid programming style (functional/pass-by-value vs. oop/pass-by-reference; example) and thus increase flexibility dramatically.
However, I think I just came across an undesired behavior…

Rappster
- 12,762
- 7
- 71
- 120
8
votes
1 answer
Adding S4 dispatch to base R S3 generic
I am trying to add a spatial method to merge which needs to be S4 (since it dispatches on the types of two different objects).
I have tried using an earlier solution as follows:
#' Merge a SpatialPolygonsDataFrame with a data.frame
#' @param SPDF A…

Ari B. Friedman
- 71,271
- 35
- 175
- 235
7
votes
1 answer
Problems passing arguments with callNextMethod() in R
My question:
Why is callNextMethod() not passing arguments as expected to the next method?
Situation:
Say I have two hierarchical classes foo and bar (bar is subclass of foo) for which I have a method foobar that can dispatch for both classes (i.e.,…

Henrik
- 14,202
- 10
- 68
- 91
7
votes
0 answers
Concatenating elements to the slot of an S4 class in Rcpp
Consider the following three Rcpp functions:
void f(Rcpp::S4 A) {
Rcpp::NumericVector x = A.slot("x");
x[0] = 42;
}
void g(Rcpp::S4 A) {
Rcpp::NumericVector x = A.slot("x");
x.push_back(42);
}
void h(Rcpp::S4 A) {
Rcpp::NumericVector x =…

Ian
- 71
- 1