How do I return a const data table from an R function?

How to return const data.table

from a function R

?

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()

    in data.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()

    and unlockBinding()

    . However, this does not work with data.table

    as it is still possible to change the locked one data.table

    . (This is similar to the difference between int * const

    and int const * const

    in C ++.)

+3


source to share





All Articles