Я создал два класса в R:
library(methods)
Foo <- setClass(
# Set the name for the class
"Foo",
# Define the slots
slots = c(
# THIS IS PROBABLY WRONG
bar = "S4"
),
)
А также
Bar <- setClass(
# Set the name for the class
"Bar",
# Define the slots
slots = c(
a = "character"
),
# Set the default values for the slots. (optional)
prototype=list(
a = "qwerty"
),
)
Я хочу сказать что-то вроде Foo.bar <- Bar()
. Я думаю, что это должно быть сделано как-то так:
# create a method to assign the value of a coordinate
setGeneric(name="addBar",
def=function(theObject)
{
standardGeneric("addBar")
}
)
setMethod(f="addBar",
signature="Foo",
definition=function(theObject)
{
theObject@bar<- Bar
}
)
Затем я вызываю это, используя:
if (!interactive()) {
result <- tryCatch({
foo <- Foo()
foo <- addBar(Foo)
}
,
warning = function(war) {
print('A warning occurred')
print(war)
},
error = function(err){
print('An error occurred')
print(err)
}
)
print(result)
}
Однако, если я запускаю это, меня встречают:
присвоение объекта класса «Бар» недопустимо для @«бар» в объекте класса «Фу»; is(значение, "S4") НЕ ИСТИНА>
Однако, когда я печатаю тип Bar, я получаю S4. Я пробовал несколько разных типов, но у меня нет идей.
Как присвоить объект класса переменной?