如何在 R 包中加载依赖项? [英] How can I load dependencies in an R package?
问题描述
我正在开发一个 R 包,可以在 DESCRIPTIONS
文件中找到它
I am developing an R package where this is available in the DESCRIPTIONS
file
Imports:
dplyr,
ggplot2,
ncdf4
我有一个使用第三个依赖项的示例函数
And I have an example function where I use the third dependency
testFun <- function(file, lat, long){
ncfname <- file.path(file,fsep = .Platform$file.sep)
xfile <- nc_open(ncfname) #Opens the NetCDF file
lat <- ncvar_get(xfile, 'lat') #Extracts all latitudes
...Calculations
return(XYZ)
}
当我构建并重新加载
包并运行该函数时,它找不到函数nc_open"
.
When I Build and Reload
the package, and I run the function, it could not find function "nc_open"
.
但是,当我用 ncdf4::nc_open
我是否应该为代码中使用的每个依赖项添加 packagename::
前缀?还是我错过了什么?
Am I supposed to prefix packagename::
to every dependency I use in the code? or am I missing something?
通常,我希望从 DESCRIPTIONS
安装所有依赖项,并且它的功能无需每次都需要包前缀即可使用.
Ordinarily, I would like all the dependencies to be installed from the DESCRIPTIONS
and it's functions available for use without requiring the package prefix everytime.
推荐答案
要么:
- 明确地在函数前面加上它来自的包:
ncdf4::nc_open(...)
或者:
- 在您的 NAMESPACE 文件
importFrom(ncdf4, nc_open)
中添加一行,然后在您的代码中,调用不带包的函数:nc_open(...)
- add a line in your NAMESPACE file
importFrom(ncdf4, nc_open)
and then in your code, call the function without the package:nc_open(...)
除了为您要导入的每个函数添加 importFrom
行之外,您还可以使用 import(ncdf4)
从该包中获取所有内容.
Rather than adding an importFrom
line for every function you want to import, you can also use import(ncdf4)
to snarf everything from that package.
这篇关于如何在 R 包中加载依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!