我想在R中创建一个使用带有Rcpp
包的cpp代码的包。在cpp代码中,我需要使用带有命名参数的类和成员函数。
在构建软件包之后,Rcpp模块内部的函数似乎忽略了命名输入,而是按位置解析。
例如,使用test
包,如下所述,我得到以下输出
# test the library
library(test)
# foo1 is a function outside of a module
foo1(a = "a", b = "b") # works as expected
#> a is a, b is b
foo1(b = "b", a = "a") # works as expected
#> a is a, b is b
# function within module "ABCMODULE"
foo2(a = "a", b = "b") # works as expected
#> a is a; b is b
foo2(b = "b", a = "a") # switches arguments <-------
#> a is b; b is a
abc <- new(ABC)
abc$foo3(a = "a", b = "b") # works as expected
#> a is a; b is b
abc$foo3(b = "b", a = "a") # switches arguments <-------
#> a is b; b is a
我已经研究了许多Rcpp-vignettes,但可以找到任何解决方案。我是否忽略了文件中的某些内容?我在某处犯了错误吗?我该如何解决这个问题还是这个功能/错误?
在类似的说明中,当我尝试设置默认值,即所有代码中的std::string b = "b"
(下面更新)时,Rcpp-module-versions在尝试使用默认值时不起作用({{ 1}}结果foo1(a = "a")
和Error Expecting single string value
会产生abc$foo3(a = "a")
)
Error in ... could not find valid method
包我用
创建了包 test
然后我在Rcpp::Rcpp.package.skeleton("test", module = T)
test/
src/foo1.cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void foo1(std::string a, std::string b = "b") {
Rprintf("a is %s, b is %s", a.c_str(), b.c_str());
}
src/ABC.cpp
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
#include <Rcpp.h>
void foo2(std::string a, std::string b = "b") {
Rprintf("a is %s; b is %s\n", a.c_str(), b.c_str());
}
class ABC {
public:
ABC() {}
void foo3(std::string a, std::string b = "b");
};
void ABC::foo3(std::string a, std::string b) {
Rprintf("a is %s; b is %s\n", a.c_str(), b.c_str());
}
RCPP_MODULE(ABCMODULE){
using namespace Rcpp ;
function("foo2" , &foo2 , "documentation for foo2");
// with formal arguments specification
class_<ABC>("ABC")
// expose the default constructor
.constructor()
.method("foo3", &ABC::foo3 , "documentation for foo3")
;
}
我在文件底部添加了以下行
R/zzz.R