如何在 R 包命名空间文件中正确使用 useDynLib() [英] How to use useDynLib() correctly in an R package namespace file

查看:32
本文介绍了如何在 R 包命名空间文件中正确使用 useDynLib()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管互联网上有一些解决方案,但我发现没有一个适合我当前面临的问题(尽管可能我太笨了):

Although a few solutions exist on the internet, I found none of those suitable for the problem I'm curerntly facing (though maybe I'm simply too dumb):

我正在尝试构建一个 R 包,它广泛使用由 Makefile 编译的共享对象(是的,不好的做法,我知道,但是不能告诉 Makevars 文件将 C 和 Fortran 代码编译成一个共享对象)来自包的 src 目录中的代码.无论我将 .so 编译到何处(我尝试了 srclibs 和 package base 文件夹)或如何命名它(正如上面提到的解决方案之一所述)必须像它所在的包一样命名),R CMD check 退出

I'm trying to build an R package which makes extensive use of a shared object compiled by a Makefile (yes, bad practice, I know, but a Makevars file just can't be told to compile C and Fortran code into one shared object) from code in the package's src directory. No matter where I compile that .so to (I tried the src, libs and package base folders) or how I name it (as one of the solutions mentioned above states it must be named like the package it's contained in), R CMD check exits with

** testing if installed package can be loaded
Error in library.dynam(lib, package, package.lib) : 
shared object ‘SoMNibEN.R.so’ not found

由于我的 NAMESPACE 文件中的 useDynLib(SoMNibEN.R) 指令(其中 SoMNibEN.R 是我的包的名称,但它不适用于原始文件名字,或者)

due to the useDynLib(SoMNibEN.R) instruction in my NAMESPACE file (where SoMNibEN.R is my package's name, but it didn't work with the original name, either)

我的假设是我要么使用了错误的 useDynLib() 命令,要么我的 Makefile 有问题(尽管编译工作得很好,并且共享对象是在我的项目文件夹——就是不知道是否成功复制到包安装目录).

My assumption is that I'm either using that useDynLib() command wrong or I'm doing something wrong with my Makefile (although the compilation works pretty well and the shared object is created in my project folder - I just don't know whether it gets copied to the package installation directory successfully).

所以,如果有人知道我在这里可能做错了什么,请告诉我!

So, if anyone knows what I might be doing wrong here, please let me know!

推荐答案

你想要包的名字作为参数,因为那是共享对象的名字 由 R 构建,例如 useDynLib("chron").引号是可选的(因为它们用于 library() 等).

You want the name of the package as the argument, as that is the name of the shared object built by R, eg useDynLib("chron"). The quotes are optional (as they are for library() etc).

我还建议不要使用 Makefile,而只是将 C 和 Fortran 文件放到 src/ 目录中.R 通常足够聪明,知道要做什么.如果你需要 -I 等开关,你可以在那里设置.

I also recommend not using a Makefile, but simply dropping the C and Fortran files in the src/ directory. R is generally smart enough to know what to. If you need -I etc switches you can set them there.

最后,使用 CRAN.有数百个带有编译源的包,其中一些肯定在结构上与您的问题相似.

Lastly, use CRAN. There are hundreds of packages with compiled sources, and some are bound to be similar in structure to your question.

这篇关于如何在 R 包命名空间文件中正确使用 useDynLib()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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