I'm trying to copy a data table within a reference class but I'm unable to do so. Here is a small example
library(data.table)
Example <- setRefClass("Example",
fields = list(
data1 = "data.table"
),
method = list(
tryToCopyData1 = function(){
data_temp <- copy(data1)
}
)
)
example <- Example$new()
example$data1 <- data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
example$tryToCopyData1()
When I do this I get the following error
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
Any ideas of how I can get around this? I ultimately just need to manipulate the data table within the function without changing the original field value.
Thank you!
CodePudding user response:
You should use data.table::copy
instead of the default reference class copy
method:
library(data.table)
Example <- setRefClass("Example",
fields = list(
data1 = "data.table"
),
method = list(
tryToCopyData1 = function(){
data_temp <- data.table::copy(data1)
}
)
)
example <- Example$new()
example$data1 <- data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
example$tryToCopyData1()