Debug: Using data.table Bracket [] in Own Package

Bug: Using data.table inside own package messes up bracket [] operator

In a data.table object, the bracket [] operator can be used to extract a row.

dt = data.table::data.table(a = 1:3, b = 10:12)
data.table::setkey(dt, a)
dt[2]$b = 20
print(dt[2])
#>    a  b
#> 1: 2 20

This is different than the usual bracket in a data.frame object. Here, the above idiom gives an error:

df = data.frame(a = 1:3, b = 10:12)
print(dt[2])
#> Error in dt[2]: object of type 'closure' is not subsettable

But when using that idiom inside my own package, the behaviour of the bracket [] operator is different. Instead of returning a row, it returns a column then:

dt = data.table::data.table(a = 1:3, b = 10:12)
data.table::setkey(dt, a)
dt[2]$b = 20
print(dt[2])
#>     b
#> 1: 20
#> 2: 20
#> 3: 20

To solve the problem, put import(data.table) line into NAMESPACE or put the following lines into any R script file in the package, so that devtools::document puts import into NAMESPACE.

#' @import data.table
NULL

 Tech    21 Dec, 2017

Any work (images, writings, presentations, ideas or whatever) which I own is always provided under
Creative Commons License Creative Commons Attribution-Share Alike 3.0 License

Mert Nuhoglu is a Trabzon-born programmer and data scientist.

You may also like...