Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Added import(data.table) to NAMESPACE automatically using devtools

How do I correctly add import(data.table) to the NAMESPACE file automatically using devtools?

Generally if my packages use data.table I just write it in manually, but then I can't use @export and devtools::document to create the NAMESPACE file properly, as it either overwrites the addition I've made, or doesn't update the file at all.

Plus, it says not edit it manually...

Thanks


Sample package/R/function.R code

#' @export
#' @import data.table
test_data_table = function(dt) {
  dt[, a := 3]
}

Call example

> test_data_table
function(dt) {
  dt[, a := 3]
}
<environment: namespace:package>

> test_data_table(dt)
 Show Traceback

 Rerun with Debug
 Error in `[.data.frame`(x, i, j) : could not find function ":=" 
like image 315
Akhil Nair Avatar asked Feb 25 '26 05:02

Akhil Nair


1 Answers

You probably shouldn't use import(*) at all, unless you really need every exported object from a package. Instead, use importFrom(pkg, obj1, obj2, ...) to import only those objects you need.

From the Writing R Extensions manual, S1.5.1:

Using importFrom selectively rather than import is good practice and recommended notably when importing from packages with more than a dozen exports.

Nonetheless, if you do need to import everything, use #' @import data.table.

like image 117
Hong Ooi Avatar answered Feb 26 '26 18:02

Hong Ooi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!