Skip to content
Prev 55771 / 63421 Next

True length - length(unclass(x)) - without having to call unclass()?

Hi Tomas,
On 09/03/2018 11:49 AM, Tomas Kalibera wrote:
Please note that data.table::setattr is an exported function of a widely 
used package (available from CRAN), which also has a description in 
?data.table::setattr why it might be useful.

Of course one has to use set* functions from data.table with extreme 
care, but if one does it in the right way, they can help a lot. For 
example there is no real danger of using them in internal functions 
where one can control what is get passed to the function or created 
within the function (so when one knows that the refcount==0 condition is 
true).

(Notwithstanding the above, but also supporting you argumentation, it 
took me hours to debug a particular problem in one of my internal 
packages, see https://github.com/Rdatatable/data.table/issues/1281)

In the present case, an important and unanswered question is (cited from 
Henrik):
 >>> However, I'm concerned that calling unclass(x) may trigger an
 >>> expensive copy internally in some cases.  Is that concern unfounded?

If no copy is made, length(unclass(x)) beats length(setattr(..)) in all 
scenarios.
Getting the physical address of the object was done exclusively for 
demonstration purposes. I totally agree that is should not be used for 
the purpose you described and I have never ever done so.

Regards,
Denes