Tag: r

如何将R的情节运行到C / C ++中?

如何在不使用R扩展的情况下在C中执行R命令,例如: int main() { system(“R g<- graph(c(0,1,0,4,0,9,1,7,1,9,2,9,2,3,2,5,3,6,3,9,4,5,4,8,5,8,6,7,6,8,7,8),n=10,dir=FALSE) plot(g)") return(0) }

将Rh Rembedded.h与C代码链接

我包括一些头文件: #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 我可以使用以下命令链接blas和gsl库(-lm用于数学?): gcc -arch x86_64 myfile.c -o myfile -lgsl -lm -lgslcblas 但我得到错误: myfile.c:21:15: error: Rh: No such file or directory myfile.c:22:19: error: Rmath.h: No such file or directory myfile.c:23:23: error: Rembedded.h: No such file or directory myfile.c:24:22: error: Rdefines.h: […]

无法正确安装rpy2

我尝试使用以下命令在R 3.1.1的计算机上安装rpy2: pip install rpy2 –user 它输出成功,但是当我尝试使用它进行测试时 python -m rpy2.tests 它给我以下错误 File “/mnt/work1/software/python/2.7/lib/python2.7/runpy.py”, line 162, in _run_module_as_main “__main__”, fname, loader, pkg_name) File “/mnt/work1/software/python/2.7/lib/python2.7/runpy.py”, line 72, in _run_code exec code in run_globals File “/mnt/work1/users/home2/stamaki/.local/lib/python2.7/site-packages/rpy2-2.6.0-py2.7-linux-x86_64.egg/rpy2/tests.py”, line 23, in import rpy2.tests_rpy_classic File “/mnt/work1/users/home2/stamaki/.local/lib/python2.7/site-packages/rpy2-2.6.0-py2.7-linux-x86_64.egg/rpy2/tests_rpy_classic.py”, line 3, in import rpy2.rpy_classic as rpy File “/mnt/work1/users/home2/stamaki/.local/lib/python2.7/site-packages/rpy2-2.6.0-py2.7-linux-x86_64.egg/rpy2/rpy_classic.py”, line 5, in import rpy2.rinterface as […]

如何在C中调用R的顺序函数(通过R_orderVector())?

在一个在R包中调用的C函数中,我需要对一些数字进行排序。 为了与R做的一致,我想调用R使用的排序算法/函数,所以R_orderVector()。 我在R_orderVector()的调用时完全得到了一个分段错误。 下面是一个最小的工作示例(“最小工作包”的文件),它再现了分段错误。 我究竟做错了什么? ### DESCRIPTION ################################################################ Package: foo Version: 0.0-1 Encoding: UTF-8 Title: Sorting from C via R’s R_orderVector() Description: See title Authors@R: c(person(given = “Foo”, family = “Bar”, role = c(“aut”, “cre”), email = “foo@bar.com”)) Author: Foo Bar [aut, cre] Maintainer: Foo Bar Depends: R (>= 3.0.0) Imports: Suggests: Enhances: License: GPL-2 […]

我可以在R中加载第三方.dll吗?

我正在运行Windows 7,我知道DLL是使用Visual Studio C ++ 2010构建的。由于我没有源代码,因此在创建包(NAMESPACE)时无法在R中即时构建它。 在这个阶段,我不需要创建一个包 ,我只想使用dyn.load()在R中加载库。 因为它是一个DLL我认为它可以移植到任何客户端程序,但我只是确保,因为我知道它不是使用GNU gcc 。 编辑: 我想调用dll部署的所有函数。

使用`.Call`的R上下文,环境和调用堆栈

是否有可以从.Call调用的C代码直接调用的parent.frame或sys.calls或其他常规上下文函数的.Call ? 环顾Rinternals.h等,我似乎找不到任何类似的东西。 我能想到的最好的是: my_fun <- function(x) { .Call(my_fun_C, substitute(x), parent.frame(), sys.calls()) } 但理想情况下,我宁愿避免R调用substitute , parent.frame等,并直接从C做。为什么? 这些函数调用中的每一个都会增加300ns来评估my_fun ,虽然这并不能完全杀死我,但我想知道是否有办法避免它,或者我是否坚持使用上述函数。 函数do_sys来自main/context.c这些诱人的代码: RCNTXT *cptr; cptr = R_GlobalContext; t = cptr->sysparent; 但是RCNTXT在includes/Defn.h定义,而Defn.h看起来是一个内部的非导出头。

如何查看用C / C ++编写的函数的源代码?

通常,只需在R控制台上键入函数的名称,就可以查找用R编写的函数的源代码。 对于用C或C ++编写的函数,你是如何做到的? 例如,当我尝试查找lapply()函数的代码时,它显示了这个 – function (X, FUN, …) { FUN <- match.fun(FUN) if (!is.vector(X) || is.object(X)) X <- as.list(X) .Internal(lapply(X, FUN)) } 无论如何都无法理解这个函数到底在做什么。

R’s C API中的SEXP数据类型究竟是什么?为什么使用它?

我知道SEXP上的维基百科页面,我知道它代表符号表达。 我知道(含糊地)SEXP是在Lisp中引用树数据结构的符号,但我想知道是什么促使开发人员在C SEXP中调用R对象的数据类型。 为何选择SEXP? 我也很困惑,因为如果R是用C和Fortran制作的,为什么要使用Lisp的符号呢? 或者SEXP是一个更通用的术语? 也许我在这里遗漏了一些东西。

使用Visual Studio构建R包(C API)

我正在尝试使用Visual Studio构建一个简单的R包,这是我的代码: #include #include SEXP add(SEXP a, SEXP b) { SEXP result = PROTECT(allocVector(REALSXP, 1)); REAL(result)[0] = asReal(a) + asReal(b); UNPROTECT(1); return result; } 我安装了R运行时和RTools。 当我尝试编译它时,我收到以下链接错误: error LNK2019: unresolved external symbol REAL referenced in function “struct SEXPREC * __cdecl add(struct SEXPREC *,struct SEXPREC *)” (?add@@YAPEAUSEXPREC@@PEAU1@0@Z) error LNK2019: unresolved external symbol Rf_asReal referenced in function “struct […]

在C中运行嵌入式R.

我写了一段C代码,声明了一个大小为4×4的方阵。 然后它从R中的一个名为rgig的采样函数中进行采样。它使用来自gnu的gsl库反转矩阵并吐出结果。 这是从C调用R的练习。 #include #include #include #include #include // for gsl #include #include #include #include #include #include #include #include #include #include // for R embedding in C #include #include #include #include void gsl_square_matrix_inverse (gsl_matrix *, gsl_matrix *, int); SEXP get_rInvGauss(void); int main(void) { // Define the dimension n of the matrix // and the signum […]