How do I return a const data table from an R function?
How to return
const data.table
from a functionR
?
A data.table
from the package data.table
is passed and returned from functions by reference.
Hence, the function returning data.table
allows the calling function to modify the returned one data.table
.
So the question is: how to return a constant data.table
from a function R
?
There are a few obvious solutions that don't work for us:
-
Call
copy()
indata.table
on return.constByCopy <- function() { dt <- ... # dt is a data.table not created in the current environment ... return(copy(dt)) }
It works; however ours
data.table
are large enough that this solution is not viable. -
Use
lockBinding()
andunlockBinding()
. However, this does not work withdata.table
as it is still possible to change the locked onedata.table
. (This is similar to the difference betweenint * const
andint const * const
in C ++.)
source to share
No one has answered this question yet
See similar questions:
or similar: