如何在replace方法中传递索引值
我试图为一个类创建一个replace方法,但是我很难理解如何传递替换的索引。以下是一个玩具示例: 以下是如何在replace方法中传递索引值,r,oop,s4,R,Oop,S4,我试图为一个类创建一个replace方法,但是我很难理解如何传递替换的索引。以下是一个玩具示例: 以下是示例类: # class definiton setClass('ExampleClass', representation(lists = 'list')) # class constructor ExampleClass <- function(lists){ ec <- new('ExampleClass', lists = lists) } 但是如果我想替换其中一
示例类
:
# class definiton
setClass('ExampleClass', representation(lists = 'list'))
# class constructor
ExampleClass <- function(lists){
ec <- new('ExampleClass', lists = lists)
}
但是如果我想替换其中一个,我如何向解释器指示哪个参数是I
newitem <- c(7:9)
seeLists(testObj)[[2]] <- newitem
Error in x@lists[[i]] <- value : [[ ]] with missing subscript
newitem当我们调用
foo(myObject)[[i]] <- value
foo(myObject)[[i]]谢谢你的回答,你是对的。有没有一个地方可以涵盖这些“行为”?我目前主要遵循哈德利·威克姆的指南。我不确定,它似乎不在任何地方的帮助文件中。我通过反复试验得出了这个结论,不过需要记住的一个有用的原则是R中没有“foo”这样的东西[[
newitem <- c(7:9)
seeLists(testObj)[[2]] <- newitem
Error in x@lists[[i]] <- value : [[ ]] with missing subscript
foo(myObject)[[i]] <- value
*tmp* <- {foo(myObject) <- value}
*tmp*[[i]] <- x
*tmp*
setMethod('seeLists<-', 'ExampleClass', function(x, i, value){
return(ExampleClass(value))
})
> testObj <- ExampleClass(list('a' = c(1:3), 'b' = c(4:6)))
> newitem <- c(7:9)
> seeLists(testObj)[[1]] <- newitem
> testObj
An object of class "ExampleClass"
Slot "lists":
$a
[1] 7 8 9
$b
[1] 4 5 6
> testObj <- ExampleClass(list('a' = c(1:3), 'b' = c(4:6)))
> newitem <- c(7:9)
> seeLists(testObj)[[2]] <- newitem
> testObj
An object of class "ExampleClass"
Slot "lists":
$a
[1] 1 2 3
$b
[1] 7 8 9