One of the methods of object oriented programming in the R language.
Questions tagged [r-s4]
647 questions
0
votes
0 answers
R - How to import S4 classes from another file?
R beginner here. Let's say I have two R-files, one of them, "classFile.R" containing the S4 class "X". How can I import the code to use the X class in the other file?
I tried a simple source("classFile.R") but the classes are not known in my other…

jakobus
- 1
- 4
0
votes
1 answer
R documenting S4 generic does not show usage
This question was already asked in this stackoverflow question but the accepted answer (download the S4 branch from the author's repository) does not work for me and I think there might be a better way to achieve the same.
I have the following in my…

Pablo
- 328
- 2
- 16
0
votes
0 answers
S4 Method Documentation
I am getting a build warning:
Undocumented S4 methods:
generic 'plot' and siglist 'cd.fit,missing'
and can't figure out what isn't documented. Here is what the method documentation looks like:
##' Something here
##' @param a bunch of paramter…

scottyaz
- 722
- 7
- 18
0
votes
0 answers
Can not load R package when exporting methods which is not s4 generic "as.vector"
I am trying to load package "Matrix" and get this error:
Error : Function found when exporting methods from the namespace ‘Matrix’ which is not S4 generic: ‘as.vector’
In addition: Warning message:
package ‘Matrix’ was built under R version 3.3.0…

Adam Sanders
- 125
- 11
0
votes
1 answer
How do I define the behaviour of functions `match` and `%in%` for a defined S4 class in R?
I have created a package with several R classes that map integers to various structures that often come up in combinatorics and vice versa. Although the classes only encapsulate mappings (the number of structures grows very quickly, easily reaching…

Richard Ambler
- 4,816
- 2
- 21
- 38
0
votes
1 answer
Dispatching to overloaded standardGeneric methods in R
Consider the following complete R example:
setClass("foo", representation(data = "data.frame"))
if (!isGeneric("plot"))
setGeneric("plot", function(x, y, ...) standardGeneric("plot"))
setMethod(f = "plot",
signature(x = "foo", y =…

rimorob
- 624
- 1
- 5
- 16
0
votes
1 answer
writing names(object) <- assignment method in R
I'm writing methods for an S4 class. I have written a "names" method as it follows:
setMethod("names","markovchain",
function(x) {
out <- x@states
return(out)
}
)
thus returning the slot x@states composing the S4…

Giorgio Spedicato
- 2,413
- 3
- 31
- 45
0
votes
0 answers
Inplace modification of S4 object: pro and cons
Using eval.parent and substitute, it is possible to write a function that modifies its argument; for example:
> setClass("foo", representation(bar="vector"))
> f <- function(x) eval.parent(substitute(x@bar <- x@bar+1))
> a <- new("foo", bar = 0)
>…

Elvis
- 548
- 2
- 14
0
votes
1 answer
Subset an S4 object in R
I have an S4 object 'DATA' with three slots - the structure looks like this (sorry, I don't know how to create an example, so this a simplified structure of what I'm working with)
---Coordinates: @coo (e.g. 'Name_A') is a 2x20 matrix where, there…

user2175481
- 147
- 1
- 8
0
votes
1 answer
Make `==` a generic funciton in R
I would like to make == a generic function.
When I run: setGeneric("=="), the definition does not appear to change:
> `==`
function (e1, e2) .Primitive("==")
> setGeneric("==")
[1] "=="
> `==`
function (e1, e2) .Primitive("==")
And when I call…

Leif Andersen
- 21,580
- 20
- 67
- 100
0
votes
1 answer
Use .Call() to send an s4 object to a C struct
I would like to pass a simple s4 object into C and turn it into an simple struct. I'm picturing R code like
setClass("MyClass", slots = list(x = "numeric", y = "integer"))
r_instance = new("MyClass", x = rnorm(5), y =…

landau
- 5,636
- 1
- 22
- 50
0
votes
1 answer
How to call standardGeneric with a variable function name
The following code throws a warning:
test_slot = "testslot"
setGeneric(name = test_slot,
def = function(object){
standardGeneric(test_slot)
},
where = globalenv()
)
[1] "testslot"
Warning…

Alex
- 15,186
- 15
- 73
- 127
0
votes
1 answer
R - Can't convert fPORTFOLIO object to matrix or data frame
My code is the following :
require("fPortfolio")
lppData <- 100 * LPP2005.RET[, 1:6]
ewSpec <- portfolioSpec()
nAssets <- ncol(lppData)
setWeights(ewSpec) <- rep(1/nAssets, times = nAssets)
ewPortfolio <- feasiblePortfolio(lppData,ewSpec)
I want to…

user1627466
- 411
- 5
- 14
0
votes
1 answer
S4 Object Error when Trying to Access Tree
I get this error:
> mod1 <- ctree(Age ~ Kyphosis, data = kyphosis)
> tree.size(mod1@tree)
Error in tree.size(mod1@tree) :
trying to get slot "tree" from an object (class "constparty") that is not an S4 object
Here, tree.size is defined as:
>…

goldisfine
- 4,742
- 11
- 59
- 83
0
votes
0 answers
Download xml file in R and how to write a R package in S5 class?
Two questions have confused me:
How do I download a 'xml' file in R console?
I want to make a R pcakage by 'S4 class' , but many functions I
still don't know,such as how to set the variables environment 'new.env' et
al;what I grasped are some…

fly bird
- 1
- 3