如何在R包中使用用户定义的数据C结构

马克·鲍尔

当我“获取”文件时,将编译此最小示例:

/*
        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:

描述

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:

// 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时​​,所有文件都会被覆盖。当我在控制台中“获取”代码以及将它们构建到程序包中时,我为同一程序包编写的其他返回标准数据类型(例如“ std :: vector”)的C程序都可以毫无问题地进行编译。

我已经阅读了Rcpp文档和相关的插图(甚至买了Dirk的书!),但是我找不到如何告诉程序包生成器MY_HEADER_INFO的定义所在的位置。如何告诉程序包编译器此文件的定义在哪里?

谢谢!

德克·埃德比布特尔

这看起来像是一个并非很罕见的问题的另一个实例,对此我们确实有一个非常简单的答案,而这个答案却鲜为人知。

简而言之,对于包<pkg><pkg>包名是别名,请根据需要使用小写或小写。显然不是<>),请将此类struct(或在C ++情况下classtypedef或... ...定义放入文件中inst/include/<pkg>_types.h(替换<pkg>为您的包裹名称)。

如果存在这样的文件,它将自动包含在其中RcppExports.cpp,您可以使用。

详细信息在“ Rcpp属性”小插图中,并且还允许使用一些相关形式:

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

inst/include/<pkg>_types.h可能是最常用的一种。通过使用它的一个例子src/SRC / RSQLite_types.h在另一方面,在使用它的一个例子inst/include/安装/包含/ RcppQuantuccia_types.h和另一个,更大的一个在安装/包含/ RcppGSL_types.h

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在自定义数据验证中使用用户定义的功能?

如何在@DataJpaTest中使用用户定义的数据库代理

如何在熊猫数据框中使用用户定义的值填充NaN

如何在函数中使用用户定义类型?

如何在胡子PHP中使用用户定义的函数

如何在 R plotly 中使用用户定义的颜色填充箱形图?

如何在 SQL Server 中使用用户定义的表类型插入数据时避免重复记录

如何使用用户定义的参数/变量触发SSIS包的数据流元素?

如何在IF表达式中使用用户定义的变量?

如何在Linux命令中使用用户定义的变量名?

如何在Winform的构造方法中使用用户定义的属性?

你如何在 python 中使用用户定义的类支持混合算术?

如何在 rust-postgres client.execute 调用中使用用户定义的类型

如何在SQL中使用用户定义的函数返回记录数?

如何在Java中使用用户定义的名称创建变量?

如何在MySQL中使用用户定义的@variable作为线程安全

如何在Axios中使用用户名和密码访问数据库

Swift3:如何在NSArray中使用用户默认数据?

std :: find使用用户定义的结构

如何在c中使用自定义类型从节点列表中读取数据结构

如何在laravel中使用用户名登录

如何在反应导航中使用用户指定的主题?

如何在条件(MATLAB)中使用用户输入

如何在集群中使用用户输入(Node JavaScript)

如何在查询中使用用户@param

如何在Mysql If子句中使用用户变量?

如何在PHP / cURL中使用用户IP?

如何在海龟模块中使用用户输入语句?

如何在C ++中调用用户定义的函数?