我想寫一個包含一些依賴關係的包。我能夠使用library()
手動加載依賴關係,但我希望它們能夠與我的包一起自動加載。 我認爲DESCRIPTION
文件上的屬性Imports
可以用於這項工作,但它不適用於我。如何在編寫包時指定依賴關係?
所以我從頭開始一個完全新的會話:
sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.4 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.3.0
然後我打開我的包,進口
Imports:
dplyr,
zipcode,
readxl,
mosaic
事實上再次尋找到我的會議中,依賴我指定mypackage_0.1.0
正確加載:
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] mypackage_0.1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.4 nloptr_1.0.4 plyr_1.8.3 tools_3.3.0 mosaic_0.13.0 testthat_1.0.2 digest_0.6.9
[8] lme4_1.1-12 zipcode_1.0 memoise_1.0.0 gtable_0.2.0 nlme_3.1-127 lattice_0.20-33 mgcv_1.8-12
[15] Matrix_1.2-6 DBI_0.4-1 parallel_3.3.0 SparseM_1.7 ggdendro_0.1-20 gridExtra_2.2.1 withr_1.0.1
[22] dplyr_0.4.3 stringr_1.0.0 roxygen2_5.0.1 MatrixModels_0.4-1 devtools_1.11.1 grid_3.3.0 nnet_7.3-12
[29] R6_2.1.2 readxl_0.1.1 mosaicData_0.13.0 minqa_1.2.4 reshape2_1.4.1 ggplot2_2.1.0 car_2.1-2
[36] magrittr_1.5 scales_0.4.0 splines_3.3.0 MASS_7.3-45 assertthat_0.1 pbkrtest_0.4-6 colorspace_1.2-6
[43] quantreg_5.21 stringi_1.0-1 munsell_0.4.3 crayon_1.3.1
但是,無論何時我編寫了一個函數嵌套來自任何「通過命名空間加載」包的其他函數,我得到一個錯誤。例如:
f <- function(x) {tbl_df(x)}
> f(cars)
Error in f(cars) : could not find function "tbl_df"
我認爲問題是來自依賴關係的函數只能在我正在編寫的包的其他函數中使用。但是,當我將f()
定義爲我的包的新功能時,我得到了同樣的錯誤。
我建議仔細檢查NAMESPACE文件是否已保存並重新加載。
這是我的NAMESPACE文件,具有f()
函數。
# Generated by roxygen2: do not edit by hand
export(chain_spreadsheet)
export(f)
export(mapping)
export(stalary)
但是,即使在重新生成文檔並重新加載軟件包後,我仍然有同樣的問題。
你是否更新過NAMESPACE文件? – cdeterman
對不起,我再次檢查。是的,我更新了NAMESPACE。還是行不通。 – Dambo
您是指您正在展示的功能還是您的包裝內?一旦你更新了NAMESPACE,你需要重新編譯/重新安裝軟件包,它應該可以工作。如果這仍然是個問題,您應該向NAMESPACE添加問題。 – cdeterman