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
1
vote
1 answer

Error in UseMethod when Method Dispatching

I have tried the following code to create a method but when I use the generic function named "tutu" I receive the following error while the other functions (tutu.num and tutu.ch) work. Please can you help me to understand where is the mistake? I…
Fabio
  • 27
  • 4
1
vote
1 answer

Function for columns to inherit custom class of data frame in R

I have a list of dataframes dd <- list() dd$dat <- list( one = data.frame(a = c(1), b = c(2)), two = data.frame(c = c(3), d = c(4)), three = data.frame(e = c(5), f = c(6)) ) and wrote a function to append a custom class to each dataframe: #…
MayaGans
  • 1,815
  • 9
  • 30
1
vote
1 answer

R Turning several functions to one object using Object Oriented Programming (S3 and S4)

I'm trying to understand how to turn functions into object oriented programming in R. So for example, how could the data and 2 functions below be turned into one object using S3 (and then S4)? (Perhaps some other simple data and functions would…
Oscar Kjell
  • 1,599
  • 10
  • 32
1
vote
2 answers

Replacement functions in R that don't take input

This seems very related to several other questions that have been asked (this one for example), but I can't quite figure out how to do exactly what I want. Maybe replacement functions are the wrong tool for the job, which would also be a perfectly…
seth127
  • 2,594
  • 5
  • 30
  • 43
1
vote
2 answers

Execute code after `UseMethod()` in generic function in R?

I would like to have the following generic function, which checks for thew allowedFormats (This works), than executes the generic function base on the type of argument x (works) evaluates the statements after the call of UseMethod() (does not…
Rainer
  • 8,347
  • 1
  • 23
  • 28
1
vote
1 answer

Define an S3 method for a generic class

I would like to create a + method to paste character objects. One possible approach is by doing it with an infix operator: `%+%` <- function(x, y) paste0(x, y) "a" %+% "b" # returns "ab" However, I'm wondering if it is possible to do the same with…
ebeneditos
  • 2,542
  • 1
  • 15
  • 35
1
vote
1 answer

Using S3 Constructors in R

I was looking at some code earlier today (https://www.r-bloggers.com/building-your-own-blockchain-in-r/) where someone was defining S3 functions in a way that I'd never seen before and liked the visual style, but have never seen it anywhere else in…
Alex Gold
  • 335
  • 1
  • 9
1
vote
0 answers

R: Rewriting the behaviour of "[" for matrices?

I am trying to append the "matrix" class and in turn overwrite the default behaviour of "[". Code examples below: annMatrix <- function(mat=NULL, rowAnn=NULL, colAnn=NULL) { if(is.null(mat)) mat <- matrix(nrow=0, ncol=0) mat <- as.matrix(mat) …
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
1
vote
1 answer

Generic print function for S3 class

I have an S3 class, and I'm trying to work out how to set up a print function for it. This part's good. print.webglobe <- function(wg, ...){ "it worked!" } But, if I run devtools::check() on it, I get the following ominous message: checking S3…
Richard
  • 56,349
  • 34
  • 180
  • 251
1
vote
1 answer

Print method with user classes

I'm experimenting with S3-class methods and generic functions, but I'm having an issue which I think highlights a misunderstanding in my thinking. Perhaps I'm getting confused with how printing works, or how storing values and attributes works…
Akhil Nair
  • 3,144
  • 1
  • 17
  • 32
1
vote
1 answer

Partial matching confusion when arguments passed through dots ('...')

I've been working on an R package that is just a REST API wrapper for a graph database. I have a function createNode that returns an object with class node and entity: # Connect to the db. graph = startGraph("http://localhost:7474/db/data/") #…
Nicole White
  • 7,720
  • 29
  • 31
1
vote
1 answer

Forcing package to attach without masking warning

Is there a way to specify that a library should not throw warnings regarding name clashes and masked objects whenever it is attached? I imagine a solution would involve editing the description or one of the special functions such as .onAttach but I…
Jon Claus
  • 2,862
  • 4
  • 22
  • 33
1
vote
1 answer

Functional interfaces for reference classes

I am trying to figure out the conceptual implications of providing a functional user interface to reference classes (or indeed S4/S3 classes). In short, I am concerned that if I write code that looks like as below, then it is equivalent to writing…
tchakravarty
  • 10,736
  • 12
  • 72
  • 116
1
vote
1 answer

Why does the default S3 method gets called when I pass a vector to the method and the vector S3 method is implemented?

I have defined the following in R: plotWaterfall <- function(x, ...) UseMethod("plotWaterfall") plotWaterfall.default <- function(x, ...) {print("Default method does nothing")} plotWaterfall.vector <- function(x, ...) {print("Vector method…
Dr. Mike
  • 2,451
  • 4
  • 24
  • 36
0
votes
1 answer

R convert XML in S3:List to dataframe

I’m trying to get data from the web into a data frame. It comes as XML, but I cannot convert it as usual. It seem to be in an "S3:list" (?) which I don’t know how to convert into something else. data_xml = read_xml(url) Display of Variable in…
tryhard
  • 1
  • 2