我尝试data.table
从 ReferenceClass 的方法返回 s 的副本:
dummy <- setRefClass(
"dummy",
fields = list(
dt = "data.table"
),
methods = list(
initialize = function( df ){
if( !missing( df ) ){
dt <<- data.table( df , key = "a" )
}
},
getTab = function( ix ){
return( copy(dt[ ix, ]) )
}
)
)
但是,调用dummy$getTab()
会产生一个我不明白的错误:
d <- dummy$new( data.frame( a = 1:10, b = 1:10 ) )
d$getTab( 2:5 )
Error in if (shallow) assign(field, get(field, envir = selfEnv), envir = vEnv) else { :
argument is not interpretable as logical
In addition: Warning message:
In if (shallow) assign(field, get(field, envir = selfEnv), envir = vEnv) else { :
the condition has length > 1 and only the first element will be used
我不知道它是什么意思以及它来自哪里。另外,以下两个程序可以正常工作:
copy( d$dt[ 2:5 ] )
mycopy <- function( dt, ix ) {
return( copy(dt[ ix, ]) )
}
mycopy( d$dt, 2:5 )
任何帮助表示赞赏。