R语言自定义扩张包

3 篇文章 0 订阅

编写R的扩展包,包含C语言代码模块,for Windows\MacOS

C语言模块部分在Windows平台上编译成 dll文件, 在macos平台上编译成 so文件 

参照已发布的扩展包 fastICA https://cran.r-project.org/web/packages/fastICA/index.html

环境

R:  4.0.3(windows10) \  3.6.3(MacOS 10.15.7)

Windows上还需要官方的 Rtools 工具包里面有编译器等工具

 

 

Mac需要gcc, gfortran等

本文代码见

https://github.com/dclnet/guardq

代码结构和规范见官方文档 https://cran.r-project.org/doc/manuals/r-release/R-exts.html

和 https://r-pkgs.org/src.html

扩展包源码文件夹建议放在 src/library 目录(D:\Programs\R\R-4.0.3\src\library) 下

编译,Windows上需管理员权限运行 cmd 执行命令

R CMD INSTALL --build guardq  // guardq为你的扩展包
D:\Programs\R\R-4.0.3\src\library>R CMD INSTALL --build guardq
* installing to library 'D:/Programs/R/R-4.0.3/library'
* installing *source* package 'guardq' ...
** 成功将'guardq'程序包解包并MD5和检查
** using staged installation
** libs

*** arch - i386
"D:/Programs/rtools40/mingw32/bin/"gcc  -I"D:/Programs/R/R-4.0.3/include" -DNDEBUG          -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c guardq.c -o guardq.o
D:/Programs/rtools40/mingw32/bin/gcc -shared -s -static-libgcc -o guardq.dll tmp.def guardq.o -LD:/Programs/R/R-4.0.3/bin/i386 -lRlapack -LD:/Programs/R/R-4.0.3/bin/i386 -lRblas -lgfortran -lm -lquadmath -LD:/Programs/R/R-4.0.3/bin/i386 -lR
installing to D:/Programs/R/R-4.0.3/library/00LOCK-guardq/00new/guardq/libs/i386

*** arch - x64
"D:/Programs/rtools40/mingw64/bin/"gcc  -I"D:/Programs/R/R-4.0.3/include" -DNDEBUG          -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c guardq.c -o guardq.o
D:/Programs/rtools40/mingw64/bin/gcc -shared -s -static-libgcc -o guardq.dll tmp.def guardq.o -LD:/Programs/R/R-4.0.3/bin/x64 -lRlapack -LD:/Programs/R/R-4.0.3/bin/x64 -lRblas -lgfortran -lm -lquadmath -LD:/Programs/R/R-4.0.3/bin/x64 -lR
installing to D:/Programs/R/R-4.0.3/library/00LOCK-guardq/00new/guardq/libs/x64
** R
** byte-compile and prepare package for lazy loading
** help
No man pages found in package  'guardq'
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
*** arch - i386
*** arch - x64
** testing if installed package can be loaded from final location
*** arch - i386
*** arch - x64
** testing if installed package keeps a record of temporary installation path
* MD5 sums
packaged installation of 'guardq' as guardq_1.0.zip
* DONE (guardq)
Making 'packages.html' ... 好了

D:\Programs\R\R-4.0.3\src\library>

编译成功,在当前目录下生成 guardq_1.0.zip扩展包的压缩包,可以发给其他人安装了

里面的R代码也是经过字节编译的,看不到源代码的

install.packages("./guardq_1.0.zip", repos=NULL)

Mac系统上编译可能出现错误

* installing *source* package ‘guardq’ ...
** 成功将‘guardq’程序包解包并MD5和检查
** using staged installation
** libs
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o guardq.so guardq.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'
ld: warning: directory not found for option '-L/usr/local/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [guardq.so] Error 1
ERROR: compilation failed for package ‘guardq’
* removing ‘/Library/Frameworks/R.framework/Versions/3.6/Resources/library/guardq’



装一下gfortran就好了

从 https://gcc.gnu.org/wiki/GFortranBinaries 下载安装再编译即可

参考资料

https://www.info-emilio.net/programming/interfacing-c-in-r/option-3-with-r/

https://r-pkgs.org/src.html

https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Building-binary-packages

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值