问题
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 ":="
回答1:
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 thanimport
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
.
来源:https://stackoverflow.com/questions/40888820/r-added-importdata-table-to-namespace-automatically-using-devtools