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
4
votes
1 answer

How to create custom print method for S4 object in R

I created a custom class and print method: #custom class myClass <- setClass(Class = "myClass", slots = c(a = "character"), prototype = list(a = character())) #custom print method print.myClass <-…
yusuzech
  • 5,896
  • 1
  • 18
  • 33
4
votes
0 answers

R CMD check not recognizing S3 generic methods

I'm writing a package and if I run the code in my Rstudio it runs but when I give it to R CMD check to run, it doesn't recognize the S3 methods. I have a generic method: count_kmers <- function(obj, klen = 6, parallel = TRUE, …
armen
  • 403
  • 5
  • 16
4
votes
1 answer

Recursive S3 calls in a package

What I want to accomplish I have an R package with some internal R functions (called f and g in the example below) that are used in a recursive manner. In the minimal example the functions just return the length of their argument, but in the real…
Thomas
  • 292
  • 2
  • 6
4
votes
1 answer

Creating S3 class in an R Package

I have written code that very simply creates an S3 class for a package. I seek to create a new S3 class so that I can develop, e.g., custom print() methods. I have tested the code in a simple R script, but as soon as the function is wrapped into a…
Locutus
  • 135
  • 7
4
votes
2 answers

R object's name carrying through multiple functions

In line with my reading of Hadley's advice on building S3 objects I am using a helper function, a constructor function, and a validator function. A simple reproducible example: test_object <- function(x, y, z) { new_test_object(x, y,…
jamse
  • 344
  • 2
  • 14
4
votes
1 answer

R S3 class: Decide between overwriting vs appending the class name of the class attribute

I want to create an S3 class. How do I decide which way of setting the class attribute is the right one (since it makes a difference)? 1) Overwrite the class attribute object <- data.frame(field1 = "a", field2 = 2) class(object) # [1]…
R Yoda
  • 8,358
  • 2
  • 50
  • 87
4
votes
1 answer

Is it unwise to modify the class of functions in other packages?

There's a bit of a preamble before I get to my question, so hang with me! For an R package I'm working on I'd like to make it as easy as possible for users to partially apply functions inline. I had the idea of using the [] operators to call my…
user3474009
  • 311
  • 1
  • 4
4
votes
1 answer

R CMD Check non-S3-methods with dots in the name where part of name before dot coincides with a generic in utils

This question is related to but distinct from Exporting non-S3-methods with dots in the name using roxygen2 v4. From that post I learned that one needs to use @export function.name in order for the NAMESPACE to be written correctly by roxygen. I…
swihart
  • 2,648
  • 2
  • 18
  • 42
4
votes
1 answer

Variable scope in S3 generics

I have the following snippet: y <- 1 g <- function(x) { y <- 2 UseMethod("g") } g.numeric <- function(x) y g(10) # [1] 2 I do not understand, why it is possible to have access to y in g.numeric <- function(x) y. To my understanding y's scope is…
ruedi
  • 5,365
  • 15
  • 52
  • 88
4
votes
1 answer

How do I re-register S3 method inside R package?

I am working on a tool for R and I can't figure out how to replace S3 methods inside packages. Let's take print.aov for example. I want to replace its body, but inside stats namespace. If I just reassign the function in the namespace > reassignInEnv…
romants
  • 3,660
  • 1
  • 21
  • 33
3
votes
2 answers

Implementing an arithmetic system in R

I started to implement a kind of numbers in R. I have a function to add them, multiply them, etc. Now I want to do a convenient interface for the arithmetic on these numbers. That is, I don't the want the user to type multiply(x, add(y, z)), but x *…
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
3
votes
2 answers

R generic dispatching to attached environment

I have a bunch of functions and I'm trying to keep my workspace clean by defining them in an environment and attaching the environment. Some of the functions are S3 generics, and they don't seem to play well with this approach. A minimum example of…
spuddie
  • 33
  • 5
3
votes
0 answers

"no applicable method" for S3 generic despite existence of default method

This is tricky because this problem only happens in the context of a package -- everything works as expected when defined in the global namespace. I've defined an S3 generic called coerce_na_range(), which has two methods, coerce_na_range.factor()…
zephryl
  • 14,633
  • 3
  • 11
  • 30
3
votes
1 answer

How to unregister a S3 method?

Let's say I've registered a new S3 method: .S3method("model_package", "lm") Later on, I want to unregister (deregister or remove) the same S3 method, something like: .S3method("model_package", "lm", NULL) # does not work! How do I do it?
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
3
votes
2 answers

How to use ggplot_add inside another package

I'm trying to build a package for data visualisation that relies heavily on ggplot2, but has some custom shortcuts for some of the day to day problems I face. I am able to use ggplot_add function to extend the functionality of + for custom classes…