2013-05-15 51 views
1

我嘗試從ReferenceClass的方法返回的data.table S複製:返回的data.table副本從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, ])) 
    } 
) 
) 

但是,調用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) 

任何幫助表示讚賞。

回答

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