在R包中设置动态链接库的目录路径 [英] Setting a directory path to a dynamically linked library in an R package
问题描述
编写R程序包时,装入动态链接库(即 .so
文件)的正确方法是什么?到目前为止,对我而言唯一有效的解决方案是指定 .so
文件的完整路径,例如:
What is the proper way to load in a dynamically linked library (i.e., a .so
file) when writing an R package? The only solution that has worked for me so far has been to specify the full path to the .so
file, e.g.:
dyn.load('/ FULL / PATH / TO / MY / R_PACKAGE / src / my_file.so')
显然,此方法不适用于CRAN /生物导体提交,因为不会找到.so文件。因此,我(未成功)尝试了以下替代方法:
Obviously, this approach will not work for a CRAN/Bioconductor submission since the .so file will not be located. As such, I have (unsuccessfully) tried the following alternatives:
1) library.dynam()
2) library.dynam('my_file.so')
3) library.dynam('my_file.so','R_PACKAGE')
4) system.file( src, my_file.so,package = R_PACKAGE)
相关链接: R:C符号不在加载表中, R:C符号名称不在负载表中-错误与外部.c文件链接。
我的R软件包的用户显然可以在他们的计算机上设置任何任意的工作目录。全路径方法(如上所示)唯一可行的方法是将工作目录设置为 / FULL / PATH / TO / MY / R_PACKAGE / src
, (当然)是不切实际的。
Just to be crystal clear, users of my R package may obviously set any arbitrary working directory on their computers. The only way a full path approach (as shown above) would ever work is if they set their working directory to /FULL/PATH/TO/MY/R_PACKAGE/src
, which is (of course) impractical.
推荐答案
做到这一点的标准方法,如编写R扩展,是:
The standard way to do this, as described in Writing R Extensions, is:
1.5.4 useDynLib
1.5.4 useDynLib
一个 NAMESPACE
文件可以包含一个或多个 useDynLib
指令,允许需要加载共享对象。*指令
A NAMESPACE
file can contain one or more useDynLib
directives which allows shared objects that need to be loaded.* The directive
useDynLib(foo)
注册共享对象 foo
**以便通过 library.dynam $ c $加载c>。在装入程序包代码之后和运行装入挂钩函数之前,将装入已注册的对象。只需使用加载挂钩函数来加载共享库的软件包就可以使用
useDynLib
指令。
registers the shared object foo
** for loading with library.dynam
. Loading of registered object(s) occurs after the package code has been loaded and before running the load hook function. Packages that would only need a load hook function to load a shared object can use the useDynLib
directive instead.
*
注意:仅当软件包在R目录中包含R代码时,才在R的所有版本中读取。
**
请注意,这是共享库的基本名称,并将添加适当的扩展名(.so或.dll)。
*
NB: this will only be read in all versions of R if the package contains R code in a R directory.
**
Note that this is the basename of the shared object, and the appropriate extension (.so or .dll) will be added.
这篇关于在R包中设置动态链接库的目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!