如何在 R 包中使用用户定义的数据 C 结构 [英] How to use a user-defined data C structure into an R package

查看:61
本文介绍了如何在 R 包中使用用户定义的数据 C 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个最小的例子在我source"时编译文件:

This minimal example compiles when I "source" the file:

/*
        read_header.c
*/

#include <stdio.h>
#include <stdlib.h>

#include <RcppCommon.h>

typedef struct {
    int my_data;
} MY_HEADER_INFO;

namespace Rcpp {
  template <> SEXP wrap(const MY_HEADER_INFO& x);
}

#include <Rcpp.h>
namespace Rcpp {
 template <>
 SEXP wrap(const MY_HEADER_INFO& x) {
   std::vector<std::string> names;
   std::vector<SEXP> elements(1);
   // do something with the elements and names
   names.push_back("my_data");
   elements[0] = Rcpp::wrap( x.my_data );
 };
}

//' @export
// [[Rcpp::export]]
MY_HEADER_INFO read_header() {
    MY_HEADER_INFO *header;
  
  header = (MY_HEADER_INFO*)malloc(sizeof(MY_HEADER_INFO));
  memset(header, 0, sizeof(MY_HEADER_INFO));
  
    return *header;
}

当我尝试在 RStudio 中将它构建到一个包 (CMD + SHIFT + B) 中时,我收到以下很长的错误消息,其中清楚地列出了问题是我的用户定义的返回结构 MY_HEADER_INFO:

When I try to build it in RStudio into a package (CMD + SHIFT + B), I get the following, long, error message, that clearly lists the problem is my user-defined return structure, MY_HEADER_INFO:

描述

Package: myPackage
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Depends: R (>= 4.0.0)
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
Imports:
    Rcpp
Suggests: 
    knitr,
    rmarkdown,
    testthat
VignetteBuilder: knitr
LinkingTo: 
    Rcpp

命名空间

# Generated by roxygen2: do not edit by hand

export(read_header)

我收到以下错误:

==> Rcpp::compileAttributes()

* Updated src/RcppExports.cpp
* Updated R/RcppExports.R

==> devtools::document(roclets = c('rd', 'collate', 'namespace'))

Updating myPackage documentation
Loading myPackage
Re-compiling myPackage
─  installing *source* package ‘myPackage’ ...
   ** using staged installation
   ** libs
   clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include   -fPIC  -Wall -g -O2  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
   RcppExports.cpp:9:1: error: unknown type name 'MY_HEADER_INFO'
   MY_HEADER_INFO read_header();
   ^
   1 error generated.
   make: *** [RcppExports.o] Error 1
   ERROR: compilation failed for package ‘myPackage’
─  removing ‘/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpLKsw0o/devtools_install_1f333d54be59/myPackage’

Error: System command 'R' failed, exit status: 1, stdout + stderr:
E> * installing *source* package ‘myPackage’ ...
E> ** using staged installation
E> ** libs
E> clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include   -fPIC  -Wall -g -O2  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
E> RcppExports.cpp:9:1: error: unknown type name 'MY_HEADER_INFO'
E> MY_HEADER_INFO read_header();
E> ^
E> 1 error generated.
E> make: *** [RcppExports.o] Error 1
E> ERROR: compilation failed for package ‘myPackage’
E> * removing ‘/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpLKsw0o/devtools_install_1f333d54be59/myPackage’

 Stack trace:

 1. base:::suppressPackageStartupMessages({ ...
 2. base:::withCallingHandlers(expr, packageStartupMessage = function(c) tryInv ...
 3. devtools::document(roclets = c("rd", "collate", "namespace"))
 4. withr::with_envvar(r_env_vars(), roxygen2::roxygenise(pkg$path,  ...
 5. base:::force(code)
 6. roxygen2::roxygenise(pkg$path, roclets, load_code = load_code)
 7. roxygen2:::load_code(base_path)
 8. pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)
 9. pkgbuild::compile_dll(path, quiet = quiet)
 10. withr::with_makevars(compiler_flags(TRUE), assignment = "+=",  ...
 11. withr:::with_envvar(c(R_MAKEVARS_USER = makevars_file), { ...
 12. base:::force(code)
 13. base:::force(code)
 14. pkgbuild:::install_min(path, dest = install_dir, components = "libs",  ...
 15. pkgbuild:::rcmd_build_tools("INSTALL", c(path, paste("--library=",  ...
 16. pkgbuild:::with_build_tools(callr::rcmd_safe(..., env = env,  ...
 17. callr::rcmd_safe(..., env = env, spinner = FALSE, show = FALSE,  ...
 18. callr:::run_r(options)
 19. base:::with(options, with_envvar(env, do.call(processx::run,  ...
 20. base:::with.default(options, with_envvar(env, do.call(processx::run,  ...
 21. base:::eval(substitute(expr), data, enclos = parent.frame())
 22. base:::eval(substitute(expr), data, enclos = parent.frame())
 23. callr:::with_envvar(env, do.call(processx::run, c(list(bin, args = real_cmd ...
 24. base:::force(code)
 25. base:::do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 26. (function (command = NULL, args = character(), error_on_status = TRUE,  ...
 27. throw(new_process_error(res, call = sys.call(), echo = echo,  ...

 x System command 'R' failed, exit status: 1, stdout + stderr:
E> * installing *source* package ‘myPackage’ ...
E> ** using staged installation
E> ** libs
E> clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include   -fPIC  -Wall -g -O2  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
E> RcppExports.cpp:9:1: error: unknown type name 'MY_HEADER_INFO'
E> MY_HEADER_INFO read_header();
E> ^
E> 1 error generated.
E> make: *** [RcppExports.o] Error 1
E> ERROR: compilation failed for package ‘myPackage’
E> * removing ‘/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpLKsw0o/devtools_install_1f333d54be59/myPackage’ 

Execution halted

Exited with status 1.

以下内容写入 Rcpp_exports:

The following is written to Rcpp_exports:

// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <Rcpp.h>

using namespace Rcpp;

// read_header
MY_HEADER_INFO read_header();
RcppExport SEXP _myPackage_read_header() {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    rcpp_result_gen = Rcpp::wrap(read_header());
    return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
    {"_myPackage_read_header", (DL_FUNC) &_myPackage_read_header, 0},
    {NULL, NULL, 0}
};

RcppExport void R_init_myPackage(DllInfo *dll) {
    R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
    R_useDynamicSymbols(dll, FALSE);
}

然而,如果我尝试修改这个文件,当我调用 CMD+SHIFT+B 时,它都会被覆盖.我为相同的包编写的其他 C 程序返回标准数据类型(例如,std::vector")编译没有问题,无论是当我源"控制台中的代码以及我将它们构建到包中时的代码.

If I try to modify this file, however, it all gets written over when I call CMD+SHIFT+B. Other C programs I've written for this same package that return standard data types (e.g., "std::vector") compile without a problem, both when I "source" the code in the Console and when I build them into a package.

我已经通读了 Rcpp 文档和相关的小插曲(甚至还买了 Dirk 的书!),但是我找不到如何告诉包构建器 MY_HEADER_INFO 的定义所在的位置.我如何告诉包编译器这个文件的定义位于哪里?

I have read through the Rcpp documentation and related vignettes (and even bought Dirk's book!), but I cannot find how to tell the package builder where the definition of MY_HEADER_INFO is located. How do I tell the package compiler where the definition of this file is located?

谢谢!

推荐答案

这看起来像是一个并非完全不常见的问题的另一个实例,我们确实有一个非常简单的答案,但有点少众所周知.

That look like another instance of a not-entirely-uncommon problem for which we do have a wonderfully simple answer that is somewho less known than it should be.

简而言之,对于包 <pkg>(其中 <pkg> 是包名称的别名,您可以使用小写或小写.和显然没有 <>)请这样的 struct(或在 C++ 的情况下 class)或 typedef 或 ... 定义到文件 inst/include/_types.h(用你的包名替换 ).

In short, for a package <pkg> (where <pkg> is an alias for your package name, with lower or undercase as you please. and obviously no < or >) please such struct (or in the C++ case class) or typedef or ... definitions into a file inst/include/<pkg>_types.h (replacing <pkg> with your package name).

如果存在这样的文件,RcppExports.cpp 会自动包含它,您就可以开始使用了.

If such a file exists, it is automagically included by RcppExports.cpp and you are good to go.

详情在 Rcpp 属性小插图,也允许一些相关的形式:

Details are in the Rcpp Attributes vignette, and a few related forms are allowed as well:

src/<pkg>_types.h
src/<pkg>_types.hpp
inst/include/<pkg>_types.h
inst/include/<pkg>_types.hpp
inst/include/<pkg>.h

inst/include/_types.h 可能是最常用的.通过 src/ 使用它的一个例子是 src/RSQLite_types.h.另一方面,在 inst/include/ 中使用它的一个例子是 inst/include/RcppQuantuccia_types.h 和另一个更大的 inst/include/RcppGSL_types.h.

but inst/include/<pkg>_types.h may be the most commonly used one. One example of using it via src/ is src/RSQLite_types.h. On the other hand, an example of using it in inst/include/ is inst/include/RcppQuantuccia_types.h and another, much larger one in inst/include/RcppGSL_types.h.

这篇关于如何在 R 包中使用用户定义的数据 C 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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