1

我尝试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 )

任何帮助表示赞赏。

4

1 回答 1

1

好吧,对不起,这是一个愚蠢的错误,我只是监督了这个方法envRefClass$copy()。所以解决方案是data.table::copy显式调用:

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( data.table::copy(dt[ ix, ]) )
    }
  )
)
于 2013-05-15T13:24:09.163 回答