UPD中定義時的行爲有所不同。感謝@RYoda,我發現我忘記了在我的NAMESPACE
文件中包含import(data.table)
。但是,我仍然不明白爲什麼所有其他data.table
函數調用即使沒有這個import()
也能正常工作,如果有人能解釋它,將不勝感激。data.table [, - c(...)]在程序包
在我的包中,我定義了一個函數,除其他外,它從data.table中排除了一些列。我可以用不同的方式進行定義:
# code in the package: \testexcl\R\test_exclude.R
test_exclude_col1 <- function(inpDT,col2excl){
output <- inpDT[, -c(col2excl), with=F]
invisible(output)
}
test_exclude_col2 <- function(inpDT,col2excl){
output <- inpDT[, setdiff(names(inpDT), col2excl), with=F]
invisible(output;
}
然後我可以使用函數如下:
require(data.table)
require(testexcl) # my package
dt.iris <- data.table(iris)
dt.1 <- test_exclude_col1(dt.iris, 'Species') # Error
dt.2 <- test_exclude_col2(dt.iris, 'Species') # OK
test_exclude_col2()
按預期工作,同時test_exclude_col1()
引發錯誤Error in -c(col2excl) : invalid argument to unary operator
。最讓我感到困惑的是,如果我在包中定義了完全相同的代碼test_exclude_col1()
,但是在我的會話中,它可以正常工作。爲什麼會發生?
我R上版本3.3.3(2017年3月6日),Win10 x86_64的-W64-的mingw32/64,data.table_1.10.4,tools_3.3.3
P.S.可以肯定的說,我與data.table的工作,我已經添加了這片到包:
test_exclude_col1a <- function(inpDT,col2excl){
print(class(inpDT));
inpDT <- data.table(inpDT);
output <- inpDT[, -c(col2excl), with=F]
invisible(output);
}
當我打電話test_exclude_col1a(dt.iris, 'Species')
,它打印[1] "data.table" "data.frame"
然後給出了同樣的錯誤。
'test_exclude_col1'對我的作品罰款?什麼是你的data.table版本? – agstudy
只有當我傳遞'data.frame'(而不是'data.table')時,我纔會得到這個錯誤, G。 'dt.3 < - test_exclude_col1(as.data.frame(iris),'Species')'所以我猜你正在轉換某處。您上面發佈的最小示例是否會在您的計算機上產生錯誤? –
對不起,我不知道什麼是共享包的正確方式,到目前爲止我只是把它放在這裏:https://www.dropbox.com/s/1zu4wv3bgu7wxzb/MRE.ZIP?dl=1 –