I am trying to define a coerce method in R including a replace function. Unfortunately, neither the documentation of setAs() nor further discussions on the internet clarify the way how to use the parameter replace in this function.
My target is to get a coerce method working the same in these two commands:
obj <- as(obj, "to-class")
as(obj) <- "to-class"
For instance, I set a method to coerce the class "table" to "data.frame", including a function for a replacement method:
setAs(from = "table", to = "data.frame",
def = function(from) {
return(as.data.frame(from))
},
replace = function(from, value) {
from <- as(from, value)
return(from)
})
Now the method in action
data(Titanic)
x <- Titanic
# two coerce alternatives
y <- as(x, "data.frame")
as(x) <- "data.frame"
While the first works perfect, the second retrieves me an error:
Error in .identC(.class1(value), Class) :
argument "Class" is missing, with no default
Am I doing something wrong or maybe expecting too much from setAs()?
asrequires object and class, so: