سؤال

I try to return copies of data.tables from methods of a ReferenceClass:

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, ]) )
    }
  )
)

However, calling dummy$getTab() yields an error which I don't understand:

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

I have no clue, what it means and where this comes from. Plus, the following two procedures work without any problems:

copy( d$dt[ 2:5 ] )

mycopy <- function( dt, ix ) {
  return( copy(dt[ ix, ]) )
}
mycopy( d$dt, 2:5 )

Any help is appreciated.

هل كانت مفيدة؟

المحلول

Ok sorry, this was a dumb error, I simply had overseen the method envRefClass$copy(). So the solution is to call data.table::copy explicitly:

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, ]) )
    }
  )
)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top