r - Returning copy of data.table from ReferenceClass method -
i try return copies of data.tables methods of 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 error don't understand:
d <- dummy$new( data.frame( = 1:10, b = 1:10 ) ) d$gettab( 2:5 ) error in if (shallow) assign(field, get(field, envir = selfenv), envir = venv) else { : argument not interpretable logical in addition: warning message: in if (shallow) assign(field, get(field, envir = selfenv), envir = venv) else { : condition has length > 1 , first element used i have no clue, means , comes from. plus, following 2 procedures work without problems:
copy( d$dt[ 2:5 ] ) mycopy <- function( dt, ix ) { return( copy(dt[ ix, ]) ) } mycopy( d$dt, 2:5 ) any appreciated.
ok sorry, dumb error, had overseen method envrefclass$copy(). solution 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, ]) ) } ) )
Comments
Post a Comment