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

How to export new generic function for new S3 class?

I define a new function work_with_myS3 that is supposed to work with my new S3 class myS3: work_with_myS3 = function (x) { UseMethod("work_with_myS3", x) } work_with_myS3.myS3 = function(x) { some code } When I source this in my normal R…
jakub
  • 4,774
  • 4
  • 29
  • 46
2
votes
1 answer

Assigning S3 subset operator

I am writing a library to work with an S3 class called "Data" and I need to assign the subset operator, [.Data In the relevant R file, I have: `[.Data` <- function(x, condition) { ## body of function } There is no problem installing the library,…
Jon Claus
  • 2,862
  • 4
  • 22
  • 33
2
votes
1 answer

Subclass of integer64 staying after logical operation (R)

When I subclass the integer64 object from bit64 and then perform a equality test, the result contains logical data, but is still classed with my class rather than being logical. This doesn't happen with integer for example. Sample…
Corvus
  • 7,548
  • 9
  • 42
  • 68
1
vote
2 answers

How to set the additional arguments of a new generic to be the same as their default values in the default method

When implementing a generic method for a new class: scale.newclass <- function(x, center = TRUE, scale = TRUE) { ... } How to guarantee that the default values for additional arguments center and scale always stay consistent with their default…
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
1
vote
2 answers

How can I pass variables between ggplot personal functions?

I am trying to create some functions based on ggplot to create custom plots. The creation of functions is very easy and straight forward, but I cannot figure out the way to share information between the functions to help in the plot creation. Here…
teoten
  • 31
  • 1
  • 6
1
vote
1 answer

Define S3 Group generics for incompatible classes

Say, I am implementing a custom S3 class, called "myclass": myvec <- 1:5 class(myvec) <- "myclass" I define Group Ops generics so that the class is preserved after standard operations. Ops.myclass <- function(e1, e2) { if(is(e1, "myclass")) { …
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
1
vote
0 answers

Using S3 object to analyse other data.frames - noob level question

This was an attempt to understand OOP - taking a tutorial in it now. I'm very new at R and I would consider deleting the question as it is not well formulated. I would not use it for learning or guidance Part 1 - I want to build a class in S3 using…
iKcon
  • 71
  • 7
1
vote
0 answers

When I know the class of an R object, can I call the appropriate method directly?

Consider a simple R function: #' @importFrom BarPackage Bar Foo <- function (n, x) { replicate(1e6, Bar(numeric(n), x)) } If Bar() is a method, then calling Bar.numeric(numeric(n), x) saves R from having to look up the class of numeric(n); in a…
Martin Smith
  • 3,687
  • 1
  • 24
  • 51
1
vote
0 answers

R S3 generic methods set to visible FALSE

I have an R object lf which is an element of the class tbl_lazy: library(dbplyr) lf <- lazy_frame(a = TRUE, b = 1, c = 2, d = "z", con = simulate_hana()) >class(lf) [1] "tbl_HDB" "tbl_lazy" "tbl" With the help of the sloop package, I can see that…
jamoreiras
  • 315
  • 1
  • 14
1
vote
1 answer

Get internal R functions to use my S4 method

I've created a custom S4 class, and the idea is that it represents a vector that's always sorted, so I don't want sort() to actually do anything to it. So I defined a stub version of sort() for my class: MyClass <- methods::setClass("MyClass",…
Migwell
  • 18,631
  • 21
  • 91
  • 160
1
vote
2 answers

$ is an internal generic function, so how did the tibble package extend it?

Because it is on the list of Internal Generic Functions, I know that $ is an internal generic function. To my knowledge, this means that it cannot be extended using S3. Despite this, it is well-known that $ behaves differently for tibbles as it does…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
1
vote
1 answer

Does the documentation for rep tell us that it is an internal generic function?

Because it is on the list of Internal Generic Functions, I know that rep is an internal generic function. Could this fact have been derived by only reading the documentation for rep? I have located the following two relevant-looking sections: rep…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
1
vote
1 answer

How do fractions vectors (from MASS) have error checking?

Take the following code that generates a fractions vector: > example<-MASS:: as.fractions(c(0.2,0.4)) > example [1] 1/5 2/5 If we were to replace one of these fractions with a string, we get the following error: > example[1]<-"0/1" Error in…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
1
vote
2 answers

Custom classes for each element in a dataframe in R

I am very new to S3 and was wondering if it was possible given the following data frame: test <- tibble( elements = c("one", "two", "three"), S3 = c("foo", "bar", "foo") ) I could give each element in the elements column a custom class from the…
MayaGans
  • 1,815
  • 9
  • 30
1
vote
1 answer

Create an S4 class inheriting from a data frame

I am writing an R package. Within this package, I wish to have a special type of a data frame that some of the functions can recognize, with some extra attributes, say, but otherwise behaving exactly like a data frame. One way to achieve what I want…
January
  • 16,320
  • 6
  • 52
  • 74