如何制作内联 C++ R 中的函数调用?

发布于 2024-12-11 09:07:37 字数 1649 浏览 1 评论 0原文

好吧,我正在用 R 编程,我想创建一个 C++ 函数。我已经导入了 Rcpp 和内联库。现在,我只是想创建一个简单的函数来添加 2 个数字,但无论我尝试什么,我都会遇到错误。

这是我的代码:

cppstring = 'double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();'
hi <- cfunction(body=cppstring, signature(s="double"), Rcpp = TRUE)

当我输入第二行时,我发现

file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1: #include <Rcpp.h>
2: 
3: 
4: extern "C" {
5:   SEXP file628a34ce ( SEXP s );
6: }
7: 
8: SEXP file628a34ce ( SEXP s ) {
9: double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();
10:   Rf_warning("your C program does not return anything!");
11:   return R_NilValue;
12: }
Error in compileCode(f, code, language, verbose) : 
Compilation ERROR, function(s)/method(s) not created! file628a34ce.cpp: In function    ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1

我已经尝试了我能想到的一切,从强制转换,到移动代码,到#include RcppSexp,到简单的返回,每次我得到一些错误,无论是

cannot convert ‘double’ to ‘SEXPREC*’ in return

还是

invalid use of undefined type ‘struct SEXPREC’

什么

forward declaration of ‘struct SEXPREC’

...我很困惑:(我在网上看了几个例子,我目前所拥有的似乎是其他人正在做的事情,而且它对他们来说神奇地有效...

是我一直看到这个 SEXPREC* 的东西它所做的 extern "C" 函数是什么?为什么它在我的 return 语句之后生成语句并告诉我我的函数没有返回任何内容,即使它返回了?

All right, so I'm programming in R and I want to make a C++ function. I've imported the Rcpp and inline libraries. For right now, I'm just trying to make a simple function that adds 2 numbers, but no matter what I try, I get errors.

Here's my code:

cppstring = 'double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();'
hi <- cfunction(body=cppstring, signature(s="double"), Rcpp = TRUE)

and when I enter that second line, I get

file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1: #include <Rcpp.h>
2: 
3: 
4: extern "C" {
5:   SEXP file628a34ce ( SEXP s );
6: }
7: 
8: SEXP file628a34ce ( SEXP s ) {
9: double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();
10:   Rf_warning("your C program does not return anything!");
11:   return R_NilValue;
12: }
Error in compileCode(f, code, language, verbose) : 
Compilation ERROR, function(s)/method(s) not created! file628a34ce.cpp: In function    ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1

I've tried everything I could possibly think of, from casting, to moving code around, to #including RcppSexp, to just plain returning s, and every time I get some error, whether it's

cannot convert ‘double’ to ‘SEXPREC*’ in return

or

invalid use of undefined type ‘struct SEXPREC’

or

forward declaration of ‘struct SEXPREC’

...I'm so confused :( I've looked at several examples online, and what I currently have seems to be what everyone else is doing, and it magically works for them...

What is this SEXPREC* thing I keep seeing everywhere? And what is that extern "C" function thing that it makes? And why does it generate statements AFTER my return statement and tell me that my function doesn't return anything, even though it does?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-12-18 09:07:37

您是否有理由不从(字面意思!!)数十个使用内联的 Rcpp 示例开始?

另外,RcppSexp 到底是什么?您关注哪些文档?

这是我昨晚为 rcpp-devel 上的某人所做的一个示例(您可能应该加入):

library(Rcpp)
library(inline)

xorig <- c(1, -2, 3, -4, 5, -6, 7)

code <- '
    Rcpp::NumericVector x(xs);
    Rcpp::NumericVector xa = sapply( x, ::fabs );
    return(xa);
    '

xabs <- cxxfunction(signature(xs="numeric"),
                    plugin="Rcpp",
                    body=code)

xabs(xorig)

这是一个更高级的示例,因为它使用 Rcpp Sugar 为我们提供矢量化表达式 a C++ 中的 R,我们在这里使用 Rcpp Sugar 中的简单 sapply() 进行演示:

R> library(Rcpp)
R> library(inline)
R> 
R> xorig <- c(1, -2, 3, -4, 5, -6, 7)
R> 
R> code <- '
+     Rcpp::NumericVector x(xs);
+     Rcpp::NumericVector xa = sapply( x, ::fabs );
+     return(xa);
+     '
R> 
R> xabs <- cxxfunction(signature(xs="numeric"),
+                     plugin="Rcpp",
+                     body=code)
R> 
R> xabs(xorig)
[1] 1 2 3 4 5 6 7
R> 

这最清楚地演示了您的两个请求:我们使用隐式模板转换器 as<>() 从 R 给出的 SEXP 转换为初始向量,然后使用隐式模板转换器wrap() 返回转换后的第二个向量。

所有这些都在 Rcpp-introduction vignette 和 Rcpp 文档中的其他 vignettes 中进行了详细解释。

Is there a reason you are not starting from the (literally!!) dozens of Rcpp examples using inline?

Also, what on earth is RcppSexp? What documentation are your following?

Here is an example I did last night for someone on the rcpp-devel (which you should probably join):

library(Rcpp)
library(inline)

xorig <- c(1, -2, 3, -4, 5, -6, 7)

code <- '
    Rcpp::NumericVector x(xs);
    Rcpp::NumericVector xa = sapply( x, ::fabs );
    return(xa);
    '

xabs <- cxxfunction(signature(xs="numeric"),
                    plugin="Rcpp",
                    body=code)

xabs(xorig)

This is a more advanced example as it uses Rcpp sugar to give us vectorised expression a la R in C++, which we demonstrate here with a the simple sapply() from Rcpp sugar:

R> library(Rcpp)
R> library(inline)
R> 
R> xorig <- c(1, -2, 3, -4, 5, -6, 7)
R> 
R> code <- '
+     Rcpp::NumericVector x(xs);
+     Rcpp::NumericVector xa = sapply( x, ::fabs );
+     return(xa);
+     '
R> 
R> xabs <- cxxfunction(signature(xs="numeric"),
+                     plugin="Rcpp",
+                     body=code)
R> 
R> xabs(xorig)
[1] 1 2 3 4 5 6 7
R> 

This most clearly demonstrates two of your requests: we use the implicit template converters as<>() to go from a SEXP given from R to the initial vector, and then use the implicit template converter wrap() to return the transformed second vector.

All this is explained in detail in the Rcpp-introduction vignette and the other vignettes in the Rcpp documentation,.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文