如何在R包中加载依赖项? [英] How can I load dependencies in an R package?

查看:223
本文介绍了如何在R包中加载依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个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)
}

当我Build and Reload包并运行该函数时,它会could not find function "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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆