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

purrr::map executes mapping function in different environments for named functions and formula functions

I'm developing a plumber API and need to serialize a custom S3 class. As part of defining a custom serializer, I map over a list of instances of the S3 class. I have implemented a S3 method for as.list so that I can pass objects that…
Marcus
  • 3,478
  • 1
  • 7
  • 16
2
votes
2 answers

Force execution of a specified S3 method

I am using the {patchwork} package internally in my package. I would like to utilize the patchwork arithmetic operators (ie |, \, etc). I export methods for my class of objects. For example: "|.ggsurvfit" <- function(e1, e2) { build_and_wrap(e1) |…
Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
2
votes
1 answer

How does an S3 object invoke a method?

I have been trying to develop my S3 learnings. Have I used object correctly here? I want to create a summary and print and plot classes. I'm using a t-test for the moment but the function itself is not important - getting the S3 code right is. Note:…
ceallac
  • 107
  • 9
2
votes
1 answer

s3 is there a way to combine prop.table for character variables?

Noob here, I'm stuck trying to use S3 to summarise proportion data for a data.frame where there are four columns of character data. My goal is to build a summary method to show the proportions for every level of every variable at one time. I can see…
iKcon
  • 71
  • 7
2
votes
1 answer

How does R find S3 methods? Why can't R find my S3 `+` method?

I would like to create and attach an environment containing S3 methods and have R find them in the search path, by using the method name (i.e., I should be able to get infix-style a + b to work and not have to write prefix-style "+.Foo"(a, b)). What…
Ana Nimbus
  • 635
  • 3
  • 16
2
votes
1 answer

Difference between 'generic' and 'method'?

I noticed that the following functions in R have two slightly different classifications: sloop::ftype(t.test) #> [1] "S3" "generic" sloop::ftype(t.data.frame) #> [1] "S3" "method" Created on 2021-04-21 by the reprex package (v1.0.0) One…
Johnny
  • 285
  • 1
  • 7
2
votes
2 answers

R: how to use and extend data.table in an S3 class

I'd like to create an S3 class that extends data.table by adding attributes that would be used by other methods of that class. In the example below I'm adding an attribute colMeas that holds the name of the column with the…
mattek
  • 903
  • 1
  • 6
  • 18
2
votes
1 answer

Group generic methods for Ops (for time-series)

I try to define a class Ops inheritance on a S3 class that is a list and has a time serie inside the list. tsnewobject_a <- structure(list(data=ts(1:10,frequency=4,start=2010)), class="newclass") tsnewobject_b <-…
Arnaud Feldmann
  • 761
  • 5
  • 17
2
votes
1 answer

Extending rep to matrices?

If you call rep on a matrix, it repeats its elements rather than the entire matrix. The traditional fix is to call rep(list(theMatrix),...). I want to extend rep so that it does this automatically. I attempted to use rep.matrix<-function(x,...)…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
2
votes
1 answer

How do user defined S3 Group Generic Functions work in R?

I'm reading Advanced R by Hadley Wickham and I'm confused at section 13.7.3 Group Generics. I was a bit confused by the phrasing, "...you cannot define your own group generic... defining a single group generic for your class..." but I think this…
Andrew
  • 93
  • 8
2
votes
1 answer

dollar suggestions method in S3

I'm making right now a R package and I have to chose between returning lists and an object with S3 attributes. The good thing, as for the lists, is that it's very easy to use for beginners, due to the dollar sign making all the elements easy to…
Arnaud Feldmann
  • 761
  • 5
  • 17
2
votes
0 answers

How to extend the documentation of a package's S3 method?

In my package, I'm extending an S3 method as_bar from the package pcp_pack, as applied on an object foo from my package foo_pack. The ROxygen2 documentation is the following: #' Implementation of as_bar for foo objects #' #' @param x same as…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
2
votes
0 answers

"Registered S3 methods overwritten" message from imported package

Whenever I load my package, I get this message: Registered S3 methods overwritten by 'expss': method from [.labelled Hmisc as.data.frame.labelled base print.labelled Hmisc Indeed, I use functions…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
2
votes
1 answer

How to declare S3 method to default to loaded environment?

In a package, I would like to call an S3 method "compact" for object foobar. There would therefore be a compact.foobar function in my package, along with the compact function itself: compact = function(x, ...){ UseMethod("compact",…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
2
votes
1 answer

When creating new class with its own method in R, why doesn't it autoprint when entering just the object name?

I'm trying to build my first ever R package. I started with a simple project that takes a date and "humanizes" it into the format "X [time unit] ago" (e.g., "3 days ago", "4 years ago" etc). I wanted the result to have its own print method, so the…
iod
  • 7,412
  • 2
  • 17
  • 36