[英]Linking C library to R
I recently found this C library ( http://libxlsxwriter.github.io/ ), and attempted to use it with R.我最近发现了这个 C 库( http://libxlsxwriter.github.io/ ),并尝试将它与 R 一起使用。
Getting the C library to work by itself was not difficult.让 C 库自己工作并不困难。 I downloaded zlib
and libxlsxwriter
using msys2
, and ran make
in the libxlsxwriter
folder.我使用msys2
下载了zlib
和libxlsxwriter
,并在libxlsxwriter
文件夹中运行make
。
Now I can run this Hello-World example, lets call it test.c
:现在我可以运行这个 Hello-World 示例,我们称之为test.c
:
#include "xlsxwriter.h"
void main() {
lxw_workbook *workbook = workbook_new("myexcel.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
int row = 0;
int col = 0;
worksheet_write_string(worksheet, row, col, "Hello me!", NULL);
workbook_close(workbook);
}
Now I can compile test.c
by running:现在我可以通过运行来编译test.c
:
cc test.c -o test -lxlsxwriter -lz
And then run the executable:然后运行可执行文件:
./test
And now I have a Hello-World excel document.现在我有一个 Hello-World excel 文档。
Getting it to work with R has been much trickier.让它与 R 一起工作要困难得多。 If I simply run:如果我只是运行:
R CMD SHLIB test.c
I get this error: ibxlsxwriter/include/xlsxwriter/common.h:19:42: fatal error: xlsxwriter/third_party/queue.h: No such file or directory #include "xlsxwriter/third_party/queue.h"
我收到这个错误: ibxlsxwriter/include/xlsxwriter/common.h:19:42: fatal error: xlsxwriter/third_party/queue.h: No such file or directory #include "xlsxwriter/third_party/queue.h"
Yet the file is clearly there when I check.然而,当我检查时,文件显然在那里。
Any advice on how to connect this C library with R?关于如何将这个 C 库与 R 连接的任何建议? At this point I am just trying to get the hello-world example to run from R.在这一点上,我只是想让 hello-world 示例从 R 运行。
Would it be a better approach to start out building a package, with xlsxwriter
in the inst
folder, and then try to write a makevars
that will get xlsxwriter
to compile correctly?难道是一个更好的办法,开始了建设一个包,与xlsxwriter
在inst
的文件夹,然后尝试写一个makevars
,将让xlsxwriter
正确编译? I know I would have to include PKG_CPPFLAGS = -I../inst/libxlsxwriter
but I am guessing I would need more than that.我知道我必须包含PKG_CPPFLAGS = -I../inst/libxlsxwriter
但我猜我需要的不止这些。
You may want to try Continuum's Anaconda R packages.您可能想尝试 Continuum 的 Anaconda R 软件包。 They use MSYS2 packages fairly directly.他们相当直接地使用 MSYS2 包。 The toolchain package is called m2w64-toolchain and the posix package is sometimes useful for building R packages too.工具链包称为 m2w64-toolchain,posix 包有时也可用于构建 R 包。
conda install -cr r-essentials m2w64-toolchain posix
Disclaimer: I work for Continuum, but I also work on MSYS2.免责声明:我为 Continuum 工作,但我也在 MSYS2 上工作。
It is not going to be elegant, but if nobody found an elegant solution for 4 years, you might aswell use:它不会很优雅,但如果 4 年没有人找到优雅的解决方案,你不妨使用:
shell("cc test.c -o test -lxlsxwriter -lz")
This is going to help with this part as well as with any minor need to istantly access xlsx functionality这将有助于这部分以及即时访问 xlsx 功能的任何次要需求
At this point I am just trying to get the hello-world example to run from R.在这一点上,我只是想让 hello-world 示例从 R 运行。
You probably knew this already, but just in case you didn't think of using the shell()
command.您可能已经知道这一点,但以防万一您没有想到使用shell()
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.