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
0
votes
0 answers

what the difference between call R S3 method and directly call the function from global environment

I create a S3 method for printing the object name: printc = function(...) { UseMethod("printc") } #' @export add_dataset.data.frame printc.data.frame = function(...){ xs <- rlang::quos(..., .named = TRUE) return(rlang::names2(xs)) } and…
Qqing Zou
  • 1
  • 1
0
votes
1 answer

Why is my method as.character.haven_labelled() not working in my package?

I am trying to handle classed vectors in my package without importing another package to do it. I have written an as.character method for class haven_labelled. It is for internal use only, so I'm not exporting it: as.character.haven_labelled <-…
DuckPyjamas
  • 1,539
  • 13
  • 17
0
votes
1 answer

Why are the rounding functions S4 generics rather than S3?

I was surprised to discover in the documentation for the rounding functions, e.g. round, that they are all S4 generics. What benefits does this grant them over being S3 generics? As best as I can tell, everything that they do can already be done…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
0
votes
1 answer

Does R's S3 OOP system have a distinction between classes and types?

Big encapsulation-style languages, such as Java, Kotlin, or C#, have a distinction between classes and types. S3, being a generic-function style OOP system, is fundamentally different. But does it still have a class/type distinction of any kind?
J. Mini
  • 1,868
  • 1
  • 9
  • 38
0
votes
1 answer

S3 template which can take different formulas and numeric vectors as arguments

Please help me to make my code work. Here I'm trying to create an S3 template which can take different formulas and numeric vectors as arguments. Also I want to add a plot method, drawing the resulting figure. For now it throws an error: Error in…
Aella
  • 63
  • 5
0
votes
1 answer

How can I apply custom-formatting to tibble list-columns using vctrs?

I am creating a new vctrs S3 class using new_list_of() but I can't find a way to control the printing of this class when used in tibbles. Here is a minimal example using a toy 'fruit_bowl' class. Ideally I'd like the columns to show the output of…
wurli
  • 2,314
  • 10
  • 17
0
votes
1 answer

R: modifying property of an existing S3 object with a generic method

Let's say I have a constructor of a class with two properties with one initiated and the other set to NULL: myclass <- function(data) { structure( list(data1 = data, data2 = NULL), class = "myclass") } And a generic: myadd <-…
mattek
  • 903
  • 1
  • 6
  • 18
0
votes
1 answer

Do any generic functions in the base library dispatch on matrices?

I'm having some trouble writing some code that dispatches on matrices. To assist me, I'd like to see what generic functions in the base library dispatch on matrices. Is there any way to get R to give me a list of them? Failing that, does anyone know…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
0
votes
2 answers

R package check stuck on "checking use of S3 registration ..."

I am developing an R package which I plan to upload to CRAN. Upon checking the tarball created with R CMD BUILD, I try to use R CMD CHECK as follows: R CMD CHECK my_package.tar.gz --as-cran This normally works smoothly. However, in this case, it is…
EB2127
  • 1,788
  • 3
  • 22
  • 43
0
votes
1 answer

How do I create methods for a class?

In an assignment I've written the function below (learnvq) that calculates the proximity of some data points to other labelled data points and then assigns those labels to the first set based on the closest point. I return the output from this…
Dpac
  • 1
  • 1
0
votes
0 answers

s3 method definition messing up with another package's s4 method

this question is related to my previous question. I figured out that question has something with S3 summary methods I am programming to a class I am defining. I understand summary is already a S3 method. And I was able to specify it without…
hamagust
  • 728
  • 2
  • 10
  • 28
0
votes
0 answers

Which are the differences between UseMethod and .S3method()?

I am new in package development. And I am joining in a package some of my functions I use a lot for personal use... Studying how package is developed, I consider S3 object types interesting... But I am really new on S3 and object-oriented languages…
hamagust
  • 728
  • 2
  • 10
  • 28
0
votes
1 answer

Discrepancy between 'UseMethod' and 'methods'

Since R 3.6, I got an error when calling S3 functions from an attached environment (see below). Am I missing something? Thanks in advance for any tip. Best, G env <- namespace::makeNamespace('testspace') de <- 'doit <- function (x, ...)…
8QB
  • 1
0
votes
2 answers

Method dispatch when mixing S3 and S4

I'd like to understand the steps R goes through to find the appropriate function when mixing S3 and S4. Here's an example: set.seed(1) d <- data.frame(a=rep(c('a', 'b'), each=15), b=rep(c('x', 'y', 'z'), times=5), …
bobbel
  • 1,983
  • 6
  • 21
0
votes
1 answer

Get Methods Inherited from S3 Class and its Parent

If I have an S3 class that inherits from a parent, how can I return a list of generic methods that I can call - both from the S3 class and the parent. So the class of the created objects is: c("my_s3_class", "parent_s3_class") The following works…
Phil
  • 592
  • 6
  • 15
1 2 3
9
10