Questions tagged [r-s3]

One of the object oriented systems in the R language.

S3 is the main object oriented system in R.
Read more about it here

Base R uses S3.
The other two common object oriented systems are S4 and R5.

149 questions
137
votes
6 answers

What does "S3 methods" mean in R?

Since I am fairly new to R, I do not know what the S3 methods and objects are. I found that there are S3 and S4 object systems, and some recommend to use S3 over S4 if possible (See Google's R Style Guide at…
jiggysoo
  • 1,498
  • 2
  • 10
  • 9
36
votes
1 answer

S3 method consistency warning when building R package with Roxygen

I have created a roxygen file for a function that uses S3 an class. I roxygenize and then build and check and get a warning: * checking S3 generic/method consistency ... WARNING common: function(word.list, ...) common.list: function(word.list,…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
34
votes
1 answer

Safely creating S3 Generics in R

Henrik Bengtsson has provided the internet with a nice way of creating S3 generics in R without having to bother whether they were already created before... in 2002. What his function setGenericsS3 does, is basically: check whether the name is…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
19
votes
1 answer

Defining custom print methods for arrays and atomic vectors

I would like to define a different print method for arrays, but I’m afraid I’m not understanding something about S3 dispach. My custom print method is called if I call print(x) explicitly, but is not called if I just type x at the console. However,…
t-kalinowski
  • 1,420
  • 11
  • 21
18
votes
1 answer

Proper way to implement S3 dispatch on R6 classes

I have an R6 class and I want to add an S3 method for it. The documentation I found mentioned briefly that in order to use S3 dispatch on R6 you must have class = TRUE, but I couldn't find an example of how it should be done. I did see empirically…
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
15
votes
1 answer

How to use S3 methods from another package which uses export rather than S3method in its namespace without using Depends or library()

I'm working on an R package at present and trying to follow the best practice guidelines provided by Hadley Wickham at http://r-pkgs.had.co.nz. As part of this, I'm aiming to have all of the package dependencies within the Imports section of the…
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
13
votes
2 answers

Getting the object name for S3 print method failing

Define an object of S3 class "bar" and a print method: foo=list(1) class(foo) <- c("bar") print.bar <- function(x,...){ cat("print.bar says this was ",deparse(substitute(x)),"\n") } Now print(foo) does this: > print(foo) print.bar says this was …
Spacedman
  • 92,590
  • 12
  • 140
  • 224
12
votes
1 answer

Extending dplyr and use of internal functions

I'm working on a fork of the RSQLServer package and am trying to implement joins. With the current version of the package, joins for any DBI-connected database are implemented using sql_join.DBIConnection. However, that implementation doesn't work…
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
11
votes
1 answer

Defining a new class of functions in R

So I'm changing the class of some functions that I'm building in R in order to add a description attribute and because I want to use S3 generics to handle everything for me. Basically, I have a structure like foo <- function(x) x + 1 addFunction <-…
AHP
  • 147
  • 8
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
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
1 answer

S3 method help (roxygen2)

I am trying to use a S3 method in a package and thought I understood how to set it up after asking a question here: S3 method consistency warning when building R package with Roxygen But now I get results I don't expect. If I run the code below…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
10
votes
1 answer

override S3 methods in base R

I am attempting to over-ride the print.anova() function from the R stats package within a local package that I use when teaching. Basically, I want to remove the printing of the heading and add a "total" row without creating a new function (e.g.,…
D Ogle
  • 407
  • 2
  • 13
9
votes
1 answer

Why doesn't class(data.frame(...)) show list inheritance?

It's often said that data.frame inherits from list, which makes sense given many common paradigms for accessing data.frame columns ($, sapply, etc.). Yet "list" is not among the items returned in the class list of a data.frame object: dat <-…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
8
votes
2 answers

What's the preferred means for defining an S3 method in an R package without introducing a dependency?

I have an R package (not currently on CRAN) which defines a couple of S3 methods of generic functions from other packages (specifically knitr::knit_print and huxtable::as_huxtable). However, they're not a key part of my package, so I'd prefer not to…
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
1
2 3
9 10